This is a simple class to load and give translated strings, for localization., (*1)
It was formerly used in the Jelix Framework., (*2)
installation
You can install it from Composer. In your project:, (*3)
composer require "jelix/simplelocalization"
Usage
First create lang files. You can store strings of all lang into a single file, or
in one file per lang. Each file is a PHP array, and each strings have a key., (*4)
Example. Here is a file translation.en.php
for english strings:, (*5)
<?php
return array(
'welcome' => 'Hello world',
'thank.you' => 'Thank you'
);
Now translation.fr.php
for french strings:, (*6)
<?php
return array(
'welcome' => 'Bonjour le monde',
'thank.you' => 'Merci'
);
Or if you choose to have a single file translation.php
:, (*7)
<?php
return array(
'en' => array(
'welcome' => 'Hello world',
'thank.you' => 'Thank you'
),
'fr' => array(
'welcome' => 'Bonjour le monde',
'thank.you' => 'Merci'
)
);
Now in your PHP code, instanciate a container:, (*8)
// with multiple files
$locales = new \Jelix\SimpleLocalization\Container('translation.%LANG%.php');
// note that %LANG% in the path will be replace by a lang code
// with a single file
$locales = new \Jelix\SimpleLocalization\Container('translation.php');
// with an array of file name. You can mix path with `%LANG%` tag and path without it
$locales = new \Jelix\SimpleLocalization\Container(array('translation.%LANG%.php', 'other_translation.php'));
// with an array of strings
$locales = new \Jelix\SimpleLocalization\Container(array(
'en' => array(
'welcome' => 'Bonjour le monde',
'thank.you' => 'Merci'
),
'fr' => array(
'welcome' => 'Bonjour le monde',
'thank.you' => 'Merci'
)
));
As second parameter to the constructor, you can give a lang code. If not given
the current lang will be guessed from $_SERVER['HTTP_ACCEPT_LANGUAGE']
;, (*9)
Now, to retrieve a string from its key:, (*10)
$str = $locales->get('welcome');
That's it..., (*11)