mod_translation

This module provides support for dealing with multiple languages.

How content and static strings are translated is explained in full in Translation.

Language as part of the URL

By default, mod_translation prefixes each URL (using URL rewriting) in your website with the code of the current language. The idea behind this is that each language version of a resource gets its own URL, and is as such indexable for Google.

This behaviour is enabled by default, but can be switched off in the admin, by going to Structure, Translation. There is a checkbox labelled “Show the language in the URL”.

Alternatively you can set the config key mod_translation.rewrite_url to false.

Default language fallback

By default, the site default language is added as the first fallback after the selected languages. This makes untranslated content use the site default language before other fallback languages are tried. Set i18n.default_language_is_fallback to false to keep the fallback language order based only on the selected request language and the previous context language fallbacks.

This can be changed in the admin by going to Structure, Translation. There is a checkbox labelled “Use the default language for untranslated texts”.

Programmatically switching languages

In a template, you can use mod_translation’s postback hook to switch between languages:

{% button text="Dutch" postback={set_language code="nl"} delegate=`mod_translation` %}

Creates a button which switches to Dutch. And another one for english:

{% button text="English" postback={set_language code="en"} delegate=`mod_translation` %}

Supporting right-to-left languages

For basic use you don’t need to do anything. Zotonic base site adds a lang attribute to the html tag, and when a right-to-left language is selected (for instance Arabic), the browser will interpret lang="ar" and automatically adapt the content to right-to-left.

Custom right-to-left content

If you write your own templates, you can add the lang tag in the html or body tag, for instance:

<body {% include "_language_attrs.tpl" id=id %} >

This will generate the following, when Zotonic selected Arabic for the page with id id:

<body xml:lang="ar" lang="ar" dir="rtl" class="rtl">

When you want to add an extra class added to the rtl or ltr class you can use:

<body {% include "_language_attrs.tpl" id=id class="my-body-class" %} >

To create individual right-to-left elements, you can use the same principle:

<div {% include "_language_attrs.tpl" %}></div>

And when you want to force a specific language:

<div {% include "_language_attrs.tpl" language=`en` %} >This is English content</div>

Accepted Events

This module handles the following notifier callbacks:

  • observe_admin_menu: Add translation and language administration entries to the admin menu.
  • observe_dispatch_rewrite: Removes the language from the path parts and sets it as the page language.
  • observe_language_detect: Detect the preferred request language from URL, user context, and request headers.
  • observe_request_context: Check if the user has a preferred language (in the user's config) using z_context:get_cookie.
  • observe_scomp_script_render: Add translation runtime variables to rendered client-side script blocks.
  • observe_set_user_language: Persist a user language change to the authenticated user profile when allowed.
  • observe_url_rewrite: Rewrite URLs to include/remove language prefixes according to translation settings.
  • observe_user_context: Set user context language/timezone defaults based on the user profile resource.

Delegate callbacks:

  • event/2 with postback messages: language_default, language_delete, language_status, set_language, toggle_url_rewrite, translation_generate, translation_reload.
  • event/2 with submit messages: language_add, language_list.

Edit on GitHub

Observes

Notifications

observe_user_context/3

Set #context fields depending on the user and/or the preferences of the user.

Notifications

observe_scomp_script_render/2

Add extra javascript with the {% script %} tag. (map) Used to let modules inject extra javascript depending on the arguments of the {% script %} tag. Must…

Notifications

observe_language_detect/2

Try to detect the language of a translation. Set is_editable_only to false to detect any language, even if the language is not enabled for the site. Return…

Models

Models

translation

The m_translation model gives easy access to language and translation related information.

Controllers

Controllers

controller_language_set

Controller which sets the language as given in the code argument, and redirects the user back to the page given in the p argument.

Dispatch rules

Filters

Filters

is_rtl

Check if the given language is a rtl or ltr language.

Filters

language

Return the language the resource (or translated text) will be displayed in.

Filters

language_dir

Return rtl or ltr depening on the direction of the language.

Filters

language_sort

Sort a list of language codes or map with languages on their sort key. Return a list of {Code, LanguageProps} pairs.

Filters

language_sort_localized

Sort a list of language codes or map with languages on their localized name in the currently selected language. This is useful for editorial interfaces where…

Filters

set_url_language

Change the language of an URL to another language. Useful to generate alternative URLs for a page.

Filters

translate

Translates a (English) value to the current language or the given language.

Filters

translated_texts

Check all properties of a map, list or resource. Returns a list of all properties that are translated (have a #trans{} record) and their translations.

Referred by

Developer guide

Translation

Many sites need to support content and templates in multiple languages. Luckily, Zotonic is completely multilingual, out of the box. mod_translation does all…