Translation - Puzzlout/DevGuidelines GitHub Wiki
###Setup###
Symfony uses translator service for any localization of strings.
Since translator is a service we can fetch it like any other registered service in our controller like this:
$this->get('translator');
which equals to this $translator;
If we want to use translator as a service we must register it first!
In /app/config/config.yml find framework: keyword any make new entry for translator service.
framework:
translator: { fallbacks: [en] }
While we are here, take a look at parameters.locale: keyword, it is set to en
by default. That means if we can't fetch user's locale for some reason, app will set it to en by default.
That's it, we can now use Translator component as a service across our app.
###Basic usage###
Basic usage of translator service is to fetch string based on user's locale:
$translation = $translator->trans("New Page");
$translation
variable will contain translated message for user's locale or default message if there is no translation found for that locale.
Translations are stored in one of three directories (ordered by priority):
app/Resources/translations
app/Resources/{bundle}/translations
src/{bundle}/Resources/translations
Maybe it's little counter-intuitive since global translations will override bundle translations, but that's the way it is.
Specific translations are stored in one of those directories in files with this name format:
domain.locale.format
. Where domain is something like a group of translations (i.e. product), locale is shorthand format (i.e. en or fr) and format is for file format used by Symfony (xlf,yml,php).
For example if we want to create translation for admin section we can do it like this, create admin.fr.yml file inside src/{bundle}/Resources/translations
directory and put this inside of it:
New Page: Nouvelle page
If we then run some controller with this code: dump($translator->trans("New Page");
we will get Nouvelle page
output if our locale is set to fr (either fr_FR or fr_BE).
Or we can do it with keywords like this:
new.page: Nouvelle page
and type in controller dump($translator->trans('new.page'));
Downside with keywords is that we have to specify string in each locale so we will have to create admin.en.yml with new.page: New page
. Great thing is that if we decide to change the string altogether on each language we can only change it in translations instead in every controller and view. Also we can nest our translation keywords so we don't repeat itself so much in translation files.
For example we want to have translations in admin domain for both 'New Product' and 'New Page' which is very common occurrence.
new:
page: New Page
product: New Product
This is the same as specifying two separate keywords like this new.page: New page
and new.product: New Product
We can also use message placholders if we want to include some variable in our translation.
We want to translate this string: Welcome, {User}
Just call trans method like this: $translator->$trans('welcome',['%user%'=>$user]);
With translation file looking like this: welcome: Welcome, %user%
Full trans method syntax looks like this trans($id,$params,$domain,$locale)
, so we can force specific domain file to be read from and we can also force locale to translate to.
###Twig and translations###
We can also translate stuff in twig files instead.
{% trans %}New Page{% endtrans %}
or with keyword {% trans %}new.page{% endtrans %}
Or we can use trans filter while outputing some data {{ stringtotrasnlate|trans }}
if not using variable: {{ 'new.page'|trans }}
We can also send all arguments like trans method does like this:
{% trans with {'%user%': user.name } from "domain" into "fr" %}welcome{% endtrans %}
where user.name is variable or we can use instead literal string by replacing user.name
with 'Username'
###Using DB to store translations### Lexik provides really good bundle which manages translations on different languages. We should only create default locale translation and install the bundle which provides whole system of fetching translations based on locale and management system for creating new translations. It comes with built in GUI for working with it and it can even export translations into regular resource files if we don't want to enable modifying translations on live website. Since we only have to configure it once I'll just link their github documentation with guidelines: https://github.com/lexik/LexikTranslationBundle/blob/master/Resources/doc/index.md
###References###
- http://symfony.com/doc/current/book/translation.html - Intro to Symfony translations
- http://symfony.com/doc/current/components/translation/usage.html - Translator component
- https://github.com/lexik/LexikTranslationBundle - Lexik Translation Bundle