dev-master
9999999-dev https://github.com/Philipp15b/php-i18nSimple i18n class for PHP
CC By-Sa 3.0
by Philipp Schröer
v3.0
3.0.0.0 https://github.com/Philipp15b/php-i18nSimple i18n class for PHP
CC By-Sa 3.0
by Philipp Schröer
Wallogit.com
2017 © Pedro PelĂĄez
Simple i18n class for PHP
This is a simple i18n class for PHP. Nothing fancy, but fast, because it uses caching and it is easy to use. Try it out!, (*1)
Some of its features:, (*2)
.ini/.properties, .json or .yaml formatL::category_stringname
L::name($par1)
There's a usable example in the example.php file. You just have to follow these easy five steps:, (*3)
To use this class, you need to create translation files with your translated strings. They can be .ini/.properties, .json or .yaml files. This could look like this:, (*4)
lang_en.ini (English), (*5)
greeting = "Hello World!" [category] somethingother = "Something other..."
lang_de.ini (German), (*6)
greeting = "Hallo Welt!" [category] somethingother = "Etwas anderes..."
Save both files in the directory you will set in step 4. The files must be named according to the filePath setting, where '{LANGUAGE}' will be replaced by the user's language, e.g. 'en' or 'de'., (*7)
<?php
require_once 'i18n.class.php';
?>
<?php
$i18n = new i18n();
?>
The possible settings are:, (*8)
./lang/lang_{LANGUAGE}.ini)./langcache/)en)L)_abc_ you could access your localized strings via L::category_abc_stringname if you use categories in your ini. (default: _)<?php
$i18n->setCachePath('./tmp/cache');
$i18n->setFilePath('./langfiles/lang/lang_{LANGUAGE}.ini'); // language file path
$i18n->setFallbackLang('en');
$i18n->setPrefix('I');
$i18n->setForcedLang('en') // force english, even if another user language is available
$i18n->setSectionSeperator('_');
$i18n->setMergeFallback(false); // make keys available from the fallback language
?>
There is also a shorthand for that: you can set all settings in the constructor., (*9)
<?php
$i18n = new i18n('lang/lang_{LANGUAGE}.ini', 'langcache/', 'en');
?>
The (all optional) parameters are:, (*10)
init() method to load all files and translationsCall the init() file to instruct the class to load the appropriate language file, load the cache file or generate it if it doesn't exist and make the L class available so you can access your localizations., (*11)
<?php
$i18n->init();
?>
To call your localizations, simply use the L class and a class constant for the string., (*12)
In this example, we use the translation string seen in step 1., (*13)
<?php
echo L::greeting;
// If 'en' is applied: 'Hello World'
echo L::category_somethingother;
// If 'en' is applied: 'Something other...'
echo L::last_modified("today");
// Could be: 'Last modified: today'
echo L($string);
// Outputs a dynamically chosen static property
echo L($string, $args);
// Same as L::last_modified("today");
?>
As you can see, you can also call the constant as a function. It will be formatted with vsprintf., (*14)
Also, like in the two last examples, a helper function with the same name as the class makes it easier to dynamically access the constants if ever needed., (*15)
Thats it!, (*16)
This class tries to detect the user's language by trying the following sources in this order:, (*17)
$_GET['lang'])$_SESSION['lang'])$_SERVER['HTTP_ACCEPT_LANGUAGE'])php-i18n will remove all characters that are not one of the following: A-Z, a-z or 0-9 to prevent arbitrary file inclusion.
After that the class searches for the language files. For example, if you set the GET parameter 'lang' to 'en' without a forced language set, the class would try to find the file lang/lang_en.ini (if the setting langFilePath was set to default (lang/lang_{LANGUAGE}.ini)).
If this file doesn't exist, php-i18n will try to find the language file for the language defined in the session variable and so on., (*18)
You can change the user detection by extending the i18n class and overriding the getUserLangs() method:, (*19)
This very basic extension only uses the GET parameter 'language' and the session parameter 'userlanguage'. You see that this method must return an array., (*20)
Note that this example function is insecure: getUserLangs() also has to escape the results or else i18n will include arbitrary files. The default implementation is safe., (*21)
Contributions are always welcome., (*22)
Simple i18n class for PHP
CC By-Sa 3.0
Simple i18n class for PHP
CC By-Sa 3.0