fdevs/locale

library for working with different languages

3.0.0 2016-02-05 11:22 UTC

This package is auto-updated.

Last update: 2024-02-28 16:27:06 UTC


README

Build Status

SensioLabsInsight

Documentation

Installation and usage

Installation and usage is a quick:

  1. Download Locale using composer
  2. Use the library
  3. Customize Data Provider

You can "fdevs/locale-bridge" for use with other libraries/components

Step 1: Download Locale library using composer

Download the bundle by running the command:

$ php composer.phar require fdevs/locale

Composer will install the bundle to your project's vendor/fdevs directory.

Step 2: Use the library

####Basic setup:

<?php

require DIR . '/../vendor/autoload.php';

use FDevs\Locale\Model\LocaleText;
use FDevs\Locale\Translator;

// The same text in different languages
$englishText = new LocaleText('I am a programmer', 'en');
$chineseText = new LocaleText('我是程序员', 'zh');
$russianText = new LocaleText('Я программист', 'ru');

$supportedTexts = [
    $englishText,
    $russianText,
    $chineseText,
];

$trans = new Translator();

####Set current locale:

$trans->setLocale('zh');

####Get text for current locale:

// 1. Get text for current locale - ch (Chinese)
$text = $trans->trans($supportedTexts);
echo $text?$text->getText():'';
// Output: "我是程序员"

####Get text for locale - ru:

$text = $trans->trans($supportedTexts, 'ru');
echo $text?$text->getText():'';
// Output: "Я программист"

####Get text for a locale for which we don't have translation:

$text = $trans->trans($supportedTexts, 'kk');
echo $text?$text->getText():'';
// Output: ""

####Get text using a set of prioritized locales: Here you can treat this as locales fallback, first found locale from your list will be chosen.

use FDevs\Locale\Model\PriorityLocale;
use FDevs\Locale\TranslatorPriority;

$priorityLocale = [
    new PriorityLocale('uk',['en','ru']),
    new PriorityLocale('en',['uk']),
    new PriorityLocale('fa',['zh','en']),
];

$trans = new TranslatorPriority('en',$priorityLocale);
$text = $trans->trans($supportedTexts, 'uk');
echo $text?$text->getText():'';
// Output: "I am programmer"

Step 3: Customize Data Provider

create your awesome provider

use FDevs\Locale\DataProvider\DataProviderInterface;

class MyProvider implements DataProviderInterface
{
//implement interface
}

add your provider

use FDevs\Locale\DataProvider\DataProviderRegistry;
use FDevs\Locale\Translator;
use FDevs\Locale\TranslatorPriority;

$registry = new DataProviderRegistry([new MyProvider()]);
$translator = new Translator('en',$registry);
//or
$translator = new TranslatorPriority('en', $priorityLocale, $registry);

License

This library is under the MIT license. See the complete license in the Library:

LICENSE

Reporting an issue or a feature request

Issues and feature requests are tracked in the Github issue tracker.