Language switcher
A multi-language site usually has one public siteaccess per language. The language switcher makes the links between them: a link from a page in one siteaccess to the same page in another, at that page's address in the other language. It works even when the URL alias differs between the translations, or when the page has no translation there.
The links
A language switcher link has this form:
/switchlanguage/to/<siteaccess>/<node ID, URL alias or module path>
The view switchlanguage/to finds the page and redirects the browser to it in the destination siteaccess. Every visitor may use it: the module is in site.ini [RoleSettings] PolicyOmitList[]. A query string and view parameters are passed on.
| The link points to | The visitor is sent to |
|---|---|
| A node that has a translation in the destination's languages | The URL alias of that translation, in the destination siteaccess |
| A node without such a translation | The same URL alias in the destination siteaccess (the page is shown there if a language of its SiteLanguageList or the always-available flag allows it) |
| A module view, such as content/search | The same path in the destination siteaccess |
The translation is chosen with the destination's [RegionalSettings] SiteLanguageList[], and the address with its siteaccess matching: the siteaccess name in the path for URI matching, [SiteSettings] SiteURL of the destination for host matching. A path prefix (PathPrefix) of either siteaccess is taken into account.
Configuring the translation siteaccesses
List the siteaccesses to link to, with the text of their links, in site.ini [RegionalSettings] TranslationSA[].
# settings/override/site.ini.append.php [RegionalSettings] TranslationSA[] TranslationSA[eng]=English TranslationSA[ger]=Deutsch TranslationSA[fre]=Français
In a template
The template operator language_switcher returns one entry per siteaccess of TranslationSA[], keyed by the siteaccess name, with url (the switcher link), text (the name from the setting) and locale (the destination's ContentObjectLocale). Its input is a node ID, a URL alias, or the requested path of a module view.
{def $translations = language_switcher( $module_result.content_info.node_id )}
<ul class="languages">
{foreach $translations as $siteaccess => $lang}
<li><a href={$lang.url|ezurl} lang="{$lang.locale|wash}">{$lang.text|wash}</a></li>
{/foreach}
</ul>
{undef $translations}
To get the URL alias of a node in another language without redirecting, use the fetch function url_alias of the switchlanguage module. It returns false when there is no alias in that language.
{def $alias = fetch( 'switchlanguage', 'url_alias',
hash( 'node_id', $node.node_id, 'locale', 'ger-DE' ) )}
The parameters are locale (required) and either node_id or path.
A switcher of your own
The view uses the class named in site.ini [RegionalSettings] LanguageSwitcherClass (ezpLanguageSwitcher as shipped). A class of your own implements the interface ezpLanguageSwitcherCapable: the view sets the destination siteaccess, calls process(), and redirects to destinationUrl().
[RegionalSettings] LanguageSwitcherClass=myLanguageSwitcher
The related setting [RegionalSettings] LanguageSA[] maps a language to a siteaccess (LanguageSA[ger-DE]=site_ger) for code that needs the siteaccess of a language; without it the siteaccess is found by convention (ger-DE to ger).
More
Contributors
- Ester Heylen
- Andrea Melo Maintainer
- Geir Arne Waaler Maintainer
- Graham Brookins Updated Maintainer
Comments
Add a comment