Sourcefabric Manuals

 English |  Español |  Français |  Italiano |  Português |  Русский |  Shqip

Newscoop 4.4 Cookbook

Multilingual strings in the templates

If you are running a site with more than one language, all content inside the articles can be translated by the editors. However, there are always strings in the templates which also need to switch according to the language.

This is the most straight forward way of handling this:

{{ if $gimme->language->english_name == "English" }}Search{{ /if }}
{{ if $gimme->language->english_name == "Spanish" }}Búsqueda{{ /if }}

You can also make the decision based on the language code in the system. This does not make any difference. The following example would work for a bilingual site and checks for Arabic first, else will load the other language:

{{ if $gimme->language->code == 'ar' }}
{{ else }}
{{ /if }}

This works well if you have few strings in the template. In order to translate all strings across all templates, there is translations mechanism introduced firstly with Newscoop 4.3.

Basically, Newscoop theme should have folder called translations; there we put .yml file for every needed language, for example theme_translations.en.yml,, etc. This way you achieve the complete separation of multilingual strings from template code. 

Translation .yml files have standard structure, this is how it may look:

'Email sent' : "E-mail sent"
'Check inbox' : "Check your inbox in %val% minutes and follow link in e-mail to set your password."
'New password' : "New password"
'Pass not changed' : "Your password can not be changed. Follow instructions and try again."
'Enter new password' : "Enter your new password (min. 6 characters)"
'Pass again' : "Enter new password again"
'Do not match' : "Entered passwords do not match"
'Save password' : "Save new password"
'Reset password' : "Reset your password"
'Wrong email' : "E-mail is wrong"

Depending on the language of the context, we load the appropriate string translation like this:

{{ 'String_handle'|translate:'translation_domain':['%variable%' => 'some value'] }}

for example, it can be:

{{ 'Check inbox'|translate:'myprojectname':['%val%' => 2] }}

based on myprojectname.en.yml file, in English language context this would generate

Check your inbox in %val% minutes and follow link in e-mail to set your password.

and in context of for example Serbian language, based on file it would generate

Proverite vaš inbox u narednih %val% minuta i sledite link u e-mailu da biste poništili lozinku.

And how does this improve your template? By separating the content from the template, it makes everything easier to maintain and scale. Take this tempate for example - category.tpl. As the example publication is localized into four languages (en, ru, es, de), it has 4 'if' conditions for every string.

{{ include file="_tpl/_html-head.tpl" }}
<h2>{{ $gimme->topic->name }}</h2>
<p class="subtitle">
  {{ if $gimme->language->english_name == "English" }}Article published under <em>{{ $gimme->topic->name }}</em>Category{{ /if }}
  {{ if $gimme->language->english_name == "Russian" }}Статьи, опубликованные под <em>{{ $gimme->topic->name }}</em>темы{{ /if }}
  {{ if $gimme->language->english_name == "Spanish" }}Los artículos publicados en <em>{{ $gimme->topic->name }}</em>tema{{ /if }}
  {{ if $gimme->language->english_name == "German" }}Artikel zum Thema <em>{{ $gimme->topic->name }}</em>{{ /if }}
Whereas if we apply the new approach, the template looks like this:

{{ include file="_tpl/_html-head.tpl" }}
<h2>{{ $gimme->topic->name }}</h2>
<p class="subtitle">{{ 'articlePublishedUnder'|translate:'myprojectname' }} <em>{{ $gimme->topic->name }}</em>{{ 'category'|translate:'myprojectname' }}</p>
-- or using variable in trnaslated link --
<p class="subtitle">{{ 'articlePublishedUnder'|translate:'myprojectname':'%val%'=>$gimme->topic->name }}</p>

And in order to fully divide templates from editorial part of work, to allow editors of the site to independently translate strings, translation files can be translated using Transifex (or other platform that supports community options for translating and works with standard .yml language files)

There has been error in communication with Booktype server. Not sure right now where is the problem.

You should refresh this page.