Internationalization
, (*1)
A PHP package to manage i18n: translations, pluralization, date and number formatting according to
a localization., (*2)
Usage
Object creation and PHP usage
To create a new I18n instance, you need to pass it a I18n\Loader object:, (*3)
// Creation of the I18n Loader
$i18n_loader = new \I18n\Loader(array(
// this is the directory where your language strings are defined
'language_directory' => __DIR__.'/i18n',
// this is the list of available languages
'available_languages' => array(
'en' => 'en_US_USD',
'gb' => 'en_GB_UKP',
'fr' => 'fr_FR_EUR'
),
'default_language' => 'en',
// this is the tag construction used for replacements in strings
// by default, "%arg%" will be replacement by the argument "arg" value
// as this will be passed to a 'sprintf' PHP function, literal percent is written '%%'
'arg_wrapper_mask' => "%%%s%%",
));
// Creation of the I18n instance (statically) passing it the Loader
$translator = \I18n\I18n::getInstance($i18n_loader);
For a full list of possible Loader options, please have a look in source code., (*4)
Any option value defining a directory path or a filename construction can contains a %s
tag that will be replaced by the current language., (*5)
// for instance:
'language_directory' => __DIR__.'/i18n/%s'
// will render, for the EN language:
'language_directory' => __DIR__.'/i18n/EN'
As you can see, the I18n class is defined as a Singleton object: any future call of
\I18n\I18n::getInstance() will refer to the first created object instance., (*6)
Then, to actually use the translated value of a string, use the translate method:, (*7)
$translator->translate( 'string_index' [, array( arguments )] [, language code] )
You can use the pluralize method to choose a translated string depending on a number of items:, (*8)
$indexes = array(
0=>'test_item_zero',
1=>'test_item_one',
2=>'test_item_two',
3=>'test_item_multi'
);
$translator->pluralize( $indexes, $number_of_items [, array( arguments )] [, language code] )
Translation strings definition
By default (this can be over-write in the Loader), the I18n object will load the strings
defined as a PHP array like:, (*9)
$i18n_en = array (
'datetime_mask' => '%a %e %m %Y %H:%M:%S',
'test' => 'Test in english',
'test_args' => 'I received arguments : « %arg1% » and « %arg2% »',
'test_item_zero' => 'No item',
'test_item_one' => 'Just one item',
'test_item_two' => 'Two items',
'test_item_multi' => 'There are %nb% items',
);
This may be defined in a file called i18n.CODE.php where CODE is the two letter reference
of the language. These files will be searched and loaded from the language_directory loader
option value., (*10)
Load multiple language files
The I18n object is designed to be able to load multiple language files easily with:, (*11)
$i18n->loadFile( my file path )
Each file loaded is stored in the internal cache system of the object (a simple PHP array)., (*12)
Package aliases
A set of aliases functions, defined in the global namespace, are available and auto-loaded
by Composer:, (*13)
function _T(...) = $i18n->translate(...)
# or
function translate(...) = $i18n->translate(...)
function _P(...) = $i18n->pluralize(...)
# or
function pluralize(...) = $i18n->pluralize(...)
function _D(...) = $i18n->getLocalizedDateString(...)
# or
function datify(...) = $i18n->getLocalizedDateString(...)
Extensions
The package embeds an extension to use the class in the Twig template engine.
See the Twig Extension page for more infos., (*14)
Installation
For a complete information about how to install this package and load its namespace,
please have a look at our USAGE documentation., (*15)
If you are a Composer user, just add the package to the
requirements of your project's composer.json manifest file:, (*16)
"atelierspierrot/internationalization": "@stable"
You can use a specific release or the latest release of a major version using the appropriate
version constraint., (*17)
Please note that this package depends on the externals PHP Patterns
and PHP Library., (*18)
Author & License
Internationalization, (*19)
http://github.com/atelierspierrot/internationalization, (*20)
Copyright (c) 2010-2016, Pierre Cassat and contributors, (*21)
Licensed under the Apache 2.0 license., (*22)
http://www.apache.org/licenses/LICENSE-2.0, (*23)
Les Ateliers Pierrot - Paris, France, (*24)
http://www.ateliers-pierrot.fr/ - contact@ateliers-pierrot.fr, (*25)