Sourcefabric Manuals

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

Newscoop 4.2 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, create a configuration file and use the built-in smarty function {{ config_load }} (http://www.smarty.net/docs/en/language.function.config.load.tpl)

This way you achieve the complete separation of multilingual strings from template code. Every language can have it's own .conf file, for example English.conf, German.conf etc.

NOTE: these files MUST be inside the folder _conf which is inside your theme folder.

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

{{ config_load file="{{ $gimme->language->english_name }}.conf" }} 

To display the strings which are set in the config file, you are using the hash tags like this:

<p class="subtitle">{{ #articlePublishedUnder# }} <em>{{ $gimme->topic->name }}</em> {{ #category# }}</p>

You can see that you are not only using the $gimme command, but inside the # are the strings set in the configuration file.

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" }}
<body>
...
<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 }}
</p>
...
</body>
</html>
Whereas if we apply the new approach, the template looks like this:

{{ config_load file="{{ $gimme->language->english_name }}.conf" section="Category" }}
{{ include file="_tpl/_html-head.tpl" }}
<body>
...
<h2>{{ $gimme->topic->name }}</h2>
<p class="subtitle">{{ #articlePublishedUnder# }} <em>{{ $gimme->topic->name }}</em> {{ #category# }}</p>
...
</body>
</html>

Example configuration file

#this is the config file for English

# global strings
publishedOn = "Published on "
filedUnder = " Filed under "
comments = "Comments "
multiPOIs = "Map with geo-locations from listed articles"

#Strings for Category page
[Category]
articlePublishedUnder = "Articles published under"
category = "Category"

#Strings for Archive page
[Archive]
archive = "Archive"

Advantages of this approach:

  1. Using {{ #langVariable# }} style syntax in templates is easier to read when developing templates, because it distinguishes language strings from other template code
  2. No more "if" conditions inside your templates to display strings. This makes your code way more manageable.
  3. Scalable solution. The more languages you add to your publication, you only need to add a new configuration file. Simply name .conf files like English.conf, German.conf etc and load them as in the above example.
  4. Furthermore, .conf files can be structured in sections, so in a template you can decide to limit the strings loaded - for example {{ config_load file="English.conf" section="Archive" }}. In the case of a large number of strings, this is good for the server performance.
  5. Translators don't need to edit templates. Just send the .conf file to a translator and include once the work is done.

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

You should refresh this page.