i18n
Internationalization library for PHP
This library allows you to translate your website with ini files, Auto Detecting the language and much more!, (*1)
, (*2)
Documentation
i18n will create a folder named "i18n" please make sure it is readable, (*3)
Installation
- Via composer:
composer require stantabcorp/i18n
- Via manually download and install
Initialization
In the i18n constructor your must-have 3 parameters:
1. The language you want to display your website or true to use AutoDetect
2. The default language
3. An array of available languages
4. An array of options, (*4)
Example:
Without AutoDetect:, (*5)
$i18n = new i18n("en", "en", ["en", "fr"])
This will set the language to en
(English), set the default language to en
(English) and set that en
(English) and fr
(French) are available, (*6)
With AutoDetect:, (*7)
$i18n = new i18n(true, "en", ["en", "fr"])
This will set the language to AutoDetect, set the default language and set available language for AutoDetect, (*8)
AutoDetect, use the Accept-Language HTTP Header to determine the best matching locale to use., (*9)
Usage
In order to get a translation you just need to:, (*10)
$i18n->get("index_in_your_file");
If you want to use sections in your ini file use the following syntax:, (*11)
$i18n->get("section_name[index_in_your_file]");
// OR
$i18n->get("section_name.index_in_your_file");
If you want to get the current language:, (*12)
$i18n->getLang();
You want to change the language?, (*13)
$i18n->setLang("language");
You don't want to use sprintf
?! No problem:, (*14)
$i18n->replace("string", [
"string" => "strong"
]);
Want to change the translation folder on the fly?, (*15)
$i18n->setFolder("path/to/the/new/folder");
Want to get the active folder?, (*16)
$i18n->getFolder();
Want to set the available languages?, (*17)
$i18n->setAvailableLanguages(array);
Want to get the available languages?, (*18)
$i18n->getAvailableLanguage();
Now, let set and get the default language, (*19)
$i18n->setDefaultLanguage("string");
$i18n->getDefaultLanguage();
File syntax
Sections are supported by i18n, see options to enable it, (*20)
Example file:, (*21)
word1 = "Some word"
word2 = "Some other word"
[section_name]
word1 = "A word in a section"
word2 = "Another word in the same section"
Options
The fourth parameter when initializing the i18n class is an array.
Accepted values are:
* error
, a boolean to enable or disable error throwing
* sections
, a boolean to enable or not sections in the ini files
* path
, to set a path for the translations, (*22)
Twig Extension
i18n provide a simple Twig extension.
To enable it, simply add a new extension to twig:, (*23)
$i18nTwig = new i18nTwigExtension($i18n); // $i18n should be an instance of i18n\i18n
$twig->addExtension($i18nTwig);
In order to use it in a twig template, simply call (with the same syntax for section):, (*24)
{{ i18n('index in your file') }}