This site has been archived. To learn more about our current products Ibexa Content, Ibexa Experience, Ibexa Commerce head over to the Ibexa Developer Portal

eZ Community » Blogs » Olivier PORTIER » Multi-language support in eZ Publish...


Multi-language support in eZ Publish 5.1 without Twig helpers from 5.2

Thursday 07 November 2013 2:48:31 pm

  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Introduction to Twig global variables and to by pass multi-language and multi-siteaccess issue in eZ 5.1.

We just launch a new website in eZ Publish version 5.1 (Community Project 2013-06). This project is based on native eZ Publish features:

  • multi-language (french, english, and more in the future),
  • multi-sites (one siteaccess per language).

In eZ 5.1 the Twig helpers functions are missing. This helpers are available in the next 5.2 version (2013-09). So the title of your content may not display correctly in your french or english site.

Here is a solution to force language in your eZ 5.1's Twig view templates .

Declare Twig global variable

First of all the current siteaccess can be recovered with object ezpublish available in all view templates in Twig :

{% set current_siteaccess = %}

Then to get the language locale, we will declare Twig global variables in file ezpublish/config.yml.


                language: %ezpublish.system.ezdemo.language%

The value of siteaccess.ezdemo.language is defined in file ezpublish/parameter.yml.


     ezpublish.system.ezdemo.language: fre-FR

Finally, we get the language code from the view template like this:

{% set current_siteaccess = %}
{% set current_language = siteaccess[current_siteaccess].language %}

or in a single line:

{% set current_language = siteaccess[].language %}

Display a field in the proper language

The display of a field does not change, simply add the new parameter current_language:

{{content.getField('title', current_language ).value}}


{{content.getFieldValue('title', current_language )}}

The content title will be displayed in the correct language.

Same thing with the controle function ez_render_field() :

{{ ez_render_field( content, 'title',{ 'lang': current_language } ) }}

Fallback in case of missing translation

Incase your content has no translation, the call content.getField will fail. A fallback on a default language could be useful. Here is an example:

{{content.getFieldValue('title', current_language ) | default( content.getFieldValue('title', default_language ) ) }}

The variable default_language is configured the same way as current_language in files ezpublish/config.yml and ezpublish/parameter.yml :

ezpublish/config.yml :

            default_siteaccess: ezdemo_eng
                language: %ezpublish.system.ezdemo.language%
                language: %ezpublish.system.ezdemo_eng.language%

ezpublish/parameter.yml :

     ezpublish.system.ezdemo.language: fre-FR
     ezpublish.system.ezdemo_eng.language: eng-GB

In conclusion without Twig helpers functions, we have an alternative solution:

{% set current_language = siteaccess[].language %}
{% set default_language = siteaccess[siteaccess.default_siteaccess].language %}

{{content.getFieldValue('title', current_language ) | default( content.getFieldValue('title', default_language ) ) }}
Proudly Developed with from