PHP class for basic i18next functionality.
- Support for variables
- Support for context
- Support for basic sprintf
- Support for basic plural forms
- Support for multiline in JSON
- Missing interval plurals
- Formatting date, datetime, time, strings
// init i18next instance
i18next::init('en');
// get translation by key
echo i18next::getTranslation('animal.cat');
Output: cat
"common": {
"name_age" : "{{name}} is {{age}} years old"
}
echo i18next::getTranslation('common.name_age', array('name' => "Elisa", "age" => 32));
Output:
Elisa is 32 years old
There are diffrent ways on how to store plural forms in 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);
Output:
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));
Output:
no elephants
1 elephant
2 elephants
100 elephants
By providing a context you can differ translations. Eg. useful to provide gender specific translations.
"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'));
Output:
A friend
A girlfirend
2 girlfriends
no boyfriends
A boyfriend
33 boyfriends
Loads translation files from given path. Looks for translation.json
by default.
i18next::init('en', 'my/path/');
// loads my/path/translation.json
You can also use variables and split namespaces and languages to different files.
i18next::init('en', 'languages/__lng__/__ns__.json');
// loads languages/en/common.json, languages/fi/common.json, etc...
Method throws an exception if no files are found or the json can not be parsed.
Returns translated string by key.
i18next::getTranslation('common.cat', array('count' => 2, 'lng' => 'fi'));
Checks if translated string exists.
Changes language.
Gets an array of missing translations.
array(1) {
[0]=> array(2) {
["language"]=> string(2) "en"
["key"]=> string(14) "common.unknown"
}
}
You can have html content written with multilines in JSON File
{
"en": {
"common": {
"thedoglovers":["The Dog Lovers by Spike Milligan",
"So they bought you",
"And kept you in a",
"Very good home"]
}
}
}
composer test