#163

Product support

Get help in the plugin support forum.

  • Added on May 10, 2017
  • Current version: 1.0.7
  • License: MIT
  • Created by

Categories

  • Utility

This plugin provides some additional logic to the Translate plugin.

It provides you with tools to keep URLs in the default locale without a prefix, whilst prefixing all other secondary locales with their own prefix.

Translate Extension

The Rainlab.Translate plugin is awesome. Period.

For a project we wanted to have a distinct url-structure with for non-standard locales a prefix in the url. The Rainlab.Translate plugin provides the higher-level support for this, we developed some sweet around it to make it work like we want to:

  • domain.ext/ => default locale homepage
  • domain.ext/en/ => en-locale homepage.
  • etc

What does it add:

Middleware

This middleware checks wether the root of a domain is requested. Normal behaviour of Rainlab.Translate is to show you the version of the homepage in the last known locale when requesting domain.ext/. The middleware in this plugin checks the requested path; If its the root it sets the locale to be the default and continues. Done.

rel="alternate" hreflang

To serve your potential visitors the proper language-variant of your website, Google uses the rel="alternate" hreflang element on your pages to index your website. See Google's help page for more information. You'll get a new component which you'll need to place inside your <head> section of your page, eg in your layout file or head-partial. The component will retrieve all the (enabled) locale-specific URL's of the current page and list an link-element for each one of them. Eg, when having a default NL language and an English counter part:

<link rel="alternate" hreflang="nl" href="https://example.com/mijn-gave-pagina" />
<link rel="alternate" hreflang="en" href="https://example.com/en/my-fancy-page" />

The component will automatically prefix the url with the locale by means of the twig function (described somewhere else in this documentation). The only thing you'll need to make sure is to specify an URL in the static page or CMS page. Ow didn't I mention that before, the component works both with CMS pages and Static Pages.

Route to switch locale

The Translate plugin gives you some means to switch to a different locale, but we needed a 'solid' url to switch to a different url.
The /switch_locale/{locale} route will test your given locale; if valid it assesses it to be needing a prefix or not. If not valid, it wil redirect to the default locale.

Twig function

The function takes the URL, active locale and the default locale as arguments. If the active locale === the defaultLocale it will strip the locale from the url. If not, the url will be prefixed with the locale.

<a href="{{ asses_menu_item_url(item.url, locale, defaultLocale) }}">item.label</a>

To retrieve the locale and defaultLocale you should add the following to your layout in the code section.

use RainLab\Translate\Classes\Translator;

function onStart()
{
    $trans = Translator::instance();
    $this['locale'] = $trans->getLocale();
    $this['defaultLocale'] = $trans->getDefaultLocale();
}

TODO:

  • After switching locale by means of the route, look for the referer and redirect to the locale-spcific page (if available) instead of the root-page.
1.0.7

Locale switcher eventlistener only when select is available

Aug 22, 2017

1.0.6

Locale switcher No dropdown is displayed when only one locale is present.

Aug 22, 2017

1.0.5

added locale switcher

Aug 10, 2017

1.0.4

Bug Capitalize Middleware is not working.

Jun 20, 2017

1.0.3

Added the AlternateHrefLang component

Jun 06, 2017

1.0.2

Added middleware to check the root of the domain, and set the default locale accordingly.

Jun 06, 2017

1.0.1

Initial commit

May 10, 2017