I agree to the use of cookies in accordance with the Sourcefabric Privacy Policy.

Support our media development efforts

Please note: due to the quarantine measures required by the coronavirus outbreak, we are unable to answer the phone in our Prague office. Please send an email to contact@sourcefabric.org and someone will get back to you as soon as possible.

Who, what, when, where and why

Get the latest news about Sourcefabric software, solutions and ideas.

BACK TO BLOG OVERVIEW

Newscoop recipe: handling multilingual strings in templates

Managing multilingual websites is one of the strengths of Newscoop. If you are running a site with more than one language in Newscoop, all content inside, articles, sections, article fields, can be translated by the editors in the admin interface. However, there are always strings in the templates which also need to switch according to the language.

There are many ways to manage these strings. This article will point you towards the most efficient way by placing all strings in a separate file. If you like the article and / or have some ideas and tweaks for the Newscoop template engine, feel free to add them in our online Cookbook Manual at FLOSS Manuals or comment to this article.

This is the most straight forward way of handling this:

{{ if $gimme->language->english_name == "English" }}Search{{ /if }}
{{ if $gimme->language->english_name == "Spanish" }}Bsqueda{{ /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. For more complex sites it is easier to manage multilingual strings by assigning them to variables in the header and then use these variables in the body. Here's a simple example:

{{ if $gimme->language->english_name == "English" }}
{{ assign var="lang_search" value="Search"}}
{{ assign var="lang_cancel" value="Cancel"}}
{{ else }}
{{ assign var="lang_search" value="Suche"}}
{{ assign var="lang_cancel" value="Abbrechen"}}
{{ /if }}

Later on, inside the template you can call the values simply like this:

{{ $lang_cancel }}
{{ $lang_search }}

 

Handling multilingual strings in separate language templates

This only gets better: you can also create language files, containing all strings of all templates and call them once in the header of any template. The advantage here is that you mange your strings in one place. For each template you would only include a piece of text like this:

{{ if $gimme->language->english_name == "English" }}
{{ include file="_lang/lang_english.tpl" }}
{{ else }}
{{ include file="_lang/lang_german.tpl" }}
{{ /if }}

In this case, all language files are placed inside the folder _lang for easier management. Inside these templates, you collect all strings and assign the variables from the entire publication set. These variables need to be assigned "globally" - the template engine usually only uses variables inside the template where they are assigned, not in other templates called from a parent template.

Let's take a look inside the template lang_english.tpl. To assign variables with a global scope you need to add that scope inside the assign statement. The file will look like this:

{{ assign var="lang_search" value="Search" scope="global" }}
{{ assign var="lang_cancel" value="Cancel" scope="global" }}

Now you are set. The advantages of this set up are:

  • All multilingual strings live in one file
  • Adding a new string is being added in one place and can be used across all templates
  • Adding a new language to your publication only requires to translate one file

Sub-template for language selection

One last tweak to make this really easy to handle: place the language decision "if gimme language..." in an extra file which you call from each template. This way, adding a new language only requires to change one file, the language selection template. Otherwise you would have to go into each template where the language files are being called from.

  • The latest edition of Newscoop features new search with Solr, more information can be found on our website here. 
BACK TO TOP