This page shows an example of how translation can be used.
In this example we are displaying a single translation for key hathoora_hello_world.
In the controller, the code looks like this:
$helloTranslation = $this->getService('translation')->t(
'hathoora_hello_world', array('name' => 'World')
);
In this example we are passing token 'name' and the output is:
Hello, World
Above example will make one call to get one translation, if you need to fetch multiple translations you can associate translations to a route and fetch them. This example is using hathoora_route_example_title and hathoora_route_example_body. The controller code looks like this:
$routeTranslations = $this->getService('translation')->getRouteTranslations('hathoora_translation_route',
array(
'hathoora_route_example_title' => array(
'date' => date('m/d/y H:i:s')
),
'hathoora_route_example_body' => array(
'link' => 'http://hathoora.org'
)
)
);
In this example we are getting all translations associated with route hathoora_translation_route
. We are also passing tokens date
and link
to keys hathoora_route_example_title and hathoora_route_example_body respectively.
The output is shown below.
hathoora_route_example_title : Today is: 02/16/19 08:22:11
hathoora_route_example_body: hathoora_route_example_body
Now click here to toggle language to see the difference.
Using translation inside template and using filters. In this example we are using key hathoora_hello_world_filter.
echo $this->getService('translation')->t(
'hathoora_hello_world_filter', array('name' => ' Hathoora PHP Framework '));
The translation key has the following translation for en_US
Name is trimmed: "{{name|trim}}"<br/>
<br/>
Custom filter: "{{name|customFilter(3)|trim}}"
In this example we are using builtin trim
filter and a custom filter called and customFilter
which takes one parameter.
And we added a custom filter class that contains a static
customFilter
like so in config.
# File admin/config/config_prod.yml
hathoora:
translation:
....
# filter used in translation helper
detokenizerFilters:
- \admin\helper\translationFilter
The output of result is shown below.
Name is trimmed: "Hathoora PHP Framework"
Custom filter: "hathoora php framework ---- hathoora php framework ---- hathoora php framework ----"