- Index : Dot notation index key
- Delimiter : Index delimiter (dot)
- Dictionary : Language translation file (php array)
Light weight, simple and performance oriented composer internationalization package that provides translation tools for web applications. Converts dot notation index keys to strings in the requested language and logs unknown keys. Language dictionaries are stored in human readable/editable php array files.
First include the repository in your composer.json
by:
"repositories": [
{
"type": "vcs",
"url": "https://github.com/stamoulohta/i18n"
}
]
Then require the package:
composer require stamoulohta/i18n:dev-master
Configuration is done via php constants. You can refer to def/conf.php for extended documentation. The required language and the delimiter can also be provided as arguments to method calls at run time.
- I18N_NOTATION_DELIMITER : Customizes the notation delimiter
- I18N_LOG_UNKNOWN : Whether to log unknown indices.
true
forSTDERR
andstring
filepath for file log - I18N_DICTIONARY_PATH : Path to dictionary files
- I18N_LANGUAGE_CODE : Requested language. It can also be provided in instantiation as a variable
After instantiation use either get()
or echo()
methods to retrieve or print the translated stings respectively.
use Stamoulohta\i18n\Translator;
$translator = Translator::get_instance($language);
echo $translator->get('buttons.submit');
$translator->echo('buttons.submit');
Optionally, you can include the i18n
script in your composer.json
for command line testing.
"scripts": {
"i18n": ["vendor/bin/i18n.php"]
}
Then you can test your configuration in the command line.
composer i18n -- --language en --path res/lang --delimiter _ buttons.submit buttons.cancel
composer i18n -- -l nl -p res/lang buttons.submit buttons.cancel
composer i18n buttons.submit buttons.cancel
The script loads the generated autoload.php
so if you want it to respect the configuration you can include it in composer.json
.
"autoload-dev": {
"files": [
"def/conf.php"
]
}