Installation and usage is a quick:
- Download Locale using composer
- Use the library
- Customize Data Provider
You can "fdevs/locale-bridge" for use with other libraries/components
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.
####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"
use FDevs\Locale\DataProvider\DataProviderInterface;
class MyProvider implements DataProviderInterface
{
//implement interface
}
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);
This library is under the MIT license. See the complete license in the Library:
LICENSE
Issues and feature requests are tracked in the Github issue tracker.