Translation module¶
Note
The Translation module uses the static class TranslatorManager to manage translations.
Important
This module is under development.
It is operational for the use of existing translation files.
It still lacks the module to manage the translation files in the administration part (devtools or webtools).
Module structure¶
Translations are grouped by domain, within a locale :
In the translation root directory (default app/translations):
- Each locale corresponds to a subfolder.
- For each locale, in a subfolder, a domain corresponds to a php file.
translations
├ en_EN
│ ├ messages.php
│ └ blog.php
└ fr_FR
├ messages.php
└ blog.php
- each domain file contains an associative array of translations key-> translation value
- Each key can be associated with
- a translation
- a translation containing variables (between % and %)
- an array of translations for handle pluralization
return [
'okayBtn'=>'Okay',
'cancelBtn'=>'Cancel',
'deleteMessage'=>['No message to delete!','1 message to delete.','%count% messages to delete.']
];
Starting the module¶
Module startup is logically done in the services.php file.
1 2 | Ubiquity\cache\CacheManager::startProd($config);
Ubiquity\translation\TranslatorManager::start();
|
With no parameters, the call of the start method uses the locale en_EN, without fallbacklocale.
Important
The translations module must be started after the cache has started.
Defining the root translations dir¶
If the rootDir parameter is missing, the default directory used is app/translations
.
1 2 | Ubiquity\cache\CacheManager::startProd($config);
Ubiquity\translation\TranslatorManager::start('fr_FR','en_EN','myTranslations');
|
Make a translation¶
With php¶
Translation of the okayBtn key into the default locale (specified when starting the manager):
$okBtnCaption=TranslatorManager::trans('okayBtn');
With no parameters, the call of the trans method uses the default locale, the domain messages.
Translation of the message key using a variable:
$okBtnCaption=TranslatorManager::trans('message',['user'=>$user]);
In this case, the translation file must contain a reference to the user variable for the key message:
['message'=>'Hello %user%!',...];
In twig views:¶
Translation of the okayBtn key into the default locale (specified when starting the manager):
{{ t('okayBtn') }}
Translation of the message key using a variable:
{{ t('message',parameters) }}