Skip to content

ganti/i18next-php

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

i18next-php

PHP class for basic i18next functionality.

Features

Missing Features

Usage

basic usage

// init i18next instance
i18next::init('en');

// get translation by key
echo  i18next::getTranslation('animal.cat');

Output: cat

substitution of variables

"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

plural forms

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

Context

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

Methods

i18next::init( string $languageKey [, string $path ] );

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.

mixed i18next::getTranslation( string $key [, array $variables ] );

Returns translated string by key.

i18next::getTranslation('common.cat', array('count' => 2, 'lng' => 'fi'));

boolean i18next::existTranslation( string $key );

Checks if translated string exists.

void i18next::setLanguage( string $language [, string $fallback ] );

Changes language.

array i18next::getMissingTranslations();

Gets an array of missing translations.

array(1) {
	[0]=> array(2) {
	["language"]=> string(2) "en"
	["key"]=> string(14) "common.unknown"
	}
}

Multilines in JSON-arrays

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"]
       }
  }
}

Development

Run Tests

composer test

About

PHP class for basic i18next functionality

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%