This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit ezplatform.com

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

By

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 = ezpublish.siteaccess.name %}

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

ezpublish/config.yml:

twig:
    globals:
        siteaccess:
            ezdemo:
                language: %ezpublish.system.ezdemo.language%

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

ezpublish/parameter.yml:

parameters:
     ezpublish.system.ezdemo.language: fre-FR

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

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

or in a single line:

{% set current_language = siteaccess[ezpublish.siteaccess.name].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}}

or

{{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 :

twig:
    globals:
        siteaccess:
            default_siteaccess: ezdemo_eng
            ezdemo:
                language: %ezpublish.system.ezdemo.language%
            ezdemo_eng:
                language: %ezpublish.system.ezdemo_eng.language%

ezpublish/parameter.yml :

parameters:
     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[ezpublish.siteaccess.name].language %}
{% set default_language = siteaccess[siteaccess.default_siteaccess].language %}

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