ganti / i18next-php
该包最新版本(dev-master)没有提供许可信息。
PHP类,用于基本i18next功能。
dev-master
2019-09-22 10:15 UTC
Requires
- php: >=5.5.0
- phpunit/phpunit: ^7.5
This package is auto-updated.
Last update: 2024-09-22 21:08:44 UTC
README
PHP类,用于基本i18next功能。
特性
缺少的特性
- 缺少间隔复数
- 格式化日期、日期时间、时间、字符串
用法
基本用法
// init i18next instance i18next::init('en'); // get translation by key echo i18next::getTranslation('animal.cat');
输出: cat
变量的替换
"common": { "name_age" : "{{name}} is {{age}} years old" }
echo i18next::getTranslation('common.name_age', array('name' => "Elisa", "age" => 32));
输出:
Elisa is 32 years old
复数形式
在JSON中存储复数形式有不同的方法
"animal":{ "dog": "dog", "dog_plural": "dogs", "cat": "{{count}} cat", "cat_plural": "{{count}} cats", "elephant": "{{count}} elephant", "elephant_0": "no elephants", "elephant_2": "{{count}} elephants", "spiderWithCount" : "{{count}} spider", "spiderWithCount_plural" : "{{count}} spiders", "spiderWithCount_plural_0" : "no spiders" }
// get translation by key with plural forms echo i18next::getTranslation('animal.cat', array('count' => 2)); echo i18next::getTranslation('animal.cat', 2);
输出:
2 cats
2 cats
echo i18next::getTranslation('animal.elephant', array('count' => 0)); echo i18next::getTranslation('animal.elephant', array('count' => 1)); echo i18next::getTranslation('animal.elephant', array('count' => 2)); echo i18next::getTranslation('animal.elephant', array('count' => 100));
输出:
no elephants
1 elephant
2 elephants
100 elephants
上下文
通过提供上下文可以区分翻译。例如,提供特定性别的翻译很有用。
"people":{ "friend" : "A friend", "friend_female" : "A girlfriend", "friend_female_plural" : "{{count}} girlfriends", "friend_male" : "A boyfriend", "friend_male_0" : "no boyfriend", "friend_male_plural" : "{{count}} boyfriends" }
echo i18next::getTranslation('people.friend'); echo i18next::getTranslation('people.friend', array('context' => 'female')); echo i18next::getTranslation('people.friend', array('count' => 2, 'context' => 'female')); echo i18next::getTranslation('people.friend', array('count' => 0, 'context' => 'male')); echo i18next::getTranslation('people.friend', array('count' => 1, 'context' => 'male')); echo i18next::getTranslation('people.friend', array('count' => 33, 'context' => 'male'));
输出:
A friend
A girlfirend
2 girlfriends
no boyfriends
A boyfriend
33 boyfriends
方法
i18next::init( string $languageKey [, string $path ] );
从给定路径加载翻译文件。默认情况下,查找translation.json
。
i18next::init('en', 'my/path/'); // loads my/path/translation.json
您还可以使用变量,并将命名空间和语言分配到不同的文件。
i18next::init('en', 'languages/__lng__/__ns__.json'); // loads languages/en/common.json, languages/fi/common.json, etc...
如果没有找到文件或JSON无法解析,方法会抛出异常。
mixed i18next::getTranslation( string $key [, array $variables ] );
通过键返回翻译后的字符串。
i18next::getTranslation('common.cat', array('count' => 2, 'lng' => 'fi'));
boolean i18next::existTranslation( string $key );
检查翻译字符串是否存在。
void i18next::setLanguage( string $language [, string $fallback ] );
更改语言。
array i18next::getMissingTranslations();
获取缺少的翻译数组。
array(1) { [0]=> array(2) { ["language"]=> string(2) "en" ["key"]=> string(14) "common.unknown" } }
JSON数组中的多行
您可以在JSON文件中使用多行来编写HTML内容
{ "en": { "common": { "thedoglovers":["The Dog Lovers by Spike Milligan", "So they bought you", "And kept you in a", "Very good home"] } } }
开发
运行测试
composer test