#6

Product support

Visit this product's website for support.

Categories

  • Developer Tools
  • Miscellaneous
  • Utility

Selecting a language

Different languages can be set up in the back-end area, with a single default language selected. This activates the use of the language on the front-end and in the back-end UI.

A visitor can select a language by prefixing the language code to the URL, this is then stored in the user's session as their chosen language. For example:

  • http://website/ru/ will display the site in Russian
  • http://website/fr/ will display the site in French
  • http://website/ will display the site in the default language or the user's chosen language.

The following themes use this plugin:

Language Picker Component

A visitor can select their chosen language using the LocalePicker component. This component will display a simple dropdown that changes the page language depending on the selection.

title = "Home"
url = "/"

[localePicker]
==

<h3>{{ 'Please select your language:'|_ }}</h3>
{% component 'localePicker' %}

If translated, the text above will appear as whatever language is selected by the user. The dropdown is very basic and is intended to be restyled. A simpler example might be:

[...]
==

<p>
    Switch language to:
    <a href="#" data-request="onSwitchLocale" data-request-data="locale: 'en'">English</a>,
    <a href="#" data-request="onSwitchLocale" data-request-data="locale: 'ru'">Russian</a>
</p>

Message translation

Message or string translation is the conversion of adhoc strings used throughout the site. A message can be translated with parameters.

{{ 'site.name'|_ }}

{{ 'Welcome to our website!'|_ }}

{{ 'Hello :name!'|_({ name: 'Friend' }) }}

A message can also be translated for a choice usage.

{{ 'There are no apples|There are :number applies!'|__(2, { number: 'two' }) }}

Themes can provide default values for these messages by defining a translate key in the theme.yaml file, located in the theme directory.

name: My Theme
# [...]

translate:
    en:
        site.name: 'My Website'
        nav.home: 'Home'
        nav.video: 'Video'
        title.home: 'Welcome Home'
        title.video: 'Screencast Video'

You may also define the translations in a separate file, where the path is relative to the theme. The following definition will source the default messages from the file config/lang.yaml inside the theme.

name: My Theme
# [...]

translate: config/lang.yaml

Content translation

This plugin activates a feature in the CMS that allows content files to use language suffixes, for example:

  • welcome.htm will contain the content in the default language.
  • welcome.ru.htm will contain the content in Russian.
  • welcome.fr.htm will contain the content in French.

Model translation

Models can have their attributes translated by using the RainLab.Translate.Behaviors.TranslatableModel behavior and specifying which attributes to translate in the class.

class User
{
    public $implement = ['RainLab.Translate.Behaviors.TranslatableModel'];

    public $translatable = ['name'];
}

The attribute will then contain the default language value and other language code values can be created by using the translateContext() method.

$user = User::first();

// Outputs the name in the default language
echo $user->name;

$user->translateContext('fr');

// Sets the name in French
$user->name = 'Giselle';

// Outputs the name in French
echo $user->name;

The lang() method is a shorthand version of translateContext() and is also chainable.

// Outputs the name in French
echo $user->lang('fr')->name;

This can be useful inside a Twig template.

{{ user.lang('fr').name }}

There are ways to get and set attributes without changing the context.

// Gets a single translated attribute for a language
$user->getTranslateAttribute('name', 'fr');

// Sets a single translated attribute for a language
$user->setTranslateAttribute('name', 'Jean-Claude', 'fr');

Conditionally extending plugins

Models

It is possible to conditionally extend a plugin's models to support translation by placing an @ symbol before the behavior definition. This is a soft implement will only use TranslatableModel if the Translate plugin is installed, otherwise it will not cause any errors.

/**
 * Blog Post Model
 */
class Post extends Model
{

    [...]

    /**
     * Softly implement the TranslatableModel behavior.
     */
    public $implement = [[email protected]'];

    /**
     * @var array Attributes that support translation, if available.
     */
    public $translatable = ['title'];

    [...]

}

The back-end forms will automatically detect the presence of translatable fields and replace their controls for multilingual equivalents.

Messages

Since the Twig filter will not be available all the time, we can pipe them to the native Laravel translation methods instead. This ensures translated messages will always work on the front end.

/**
 * Register new Twig variables
 * @return array
 */
public function registerMarkupTags()
{
    // Check the translate plugin is installed
    if (!class_exists('RainLab\Translate\Behaviors\TranslatableModel'))
        return;

    return [
        'filters' => [
            '_' => ['Lang', 'get'],
            '__' => ['Lang', 'choice'],
        ]
    ];
}
  • Found the plugin useful on 21 Apr, 2017

    Nice work :)

  • Found the plugin useful on 5 Mar, 2017

    F - A - N - T - A - S - T - I - C

  • Found the plugin useful on 10 Feb, 2017

    i download the theme vega pro and i have an error because i don't have the layout clean.https://octobercms.com/theme/vojtasvoboda-vega-pro

  • Found the plugin useful on 27 Jan, 2017

    is there a local language file for welsh if not how do I go about creating one

  • Found the plugin useful on 19 Jan, 2017

    Hello, good plugin but how translate static menu?

  • Found the plugin useful on 30 Oct, 2016

    Really useful, specially with the new url translation feature! Thank you very much!

  • Found the plugin useful on 5 Oct, 2016

    if you need to access the active language from the code, use the Translator class: $this-&gt;translator = Translator::instance(); $this-&gt;locale = $this-&gt;translator-&gt;getLocale();

  • Found the plugin useful on 17 Jul, 2016

    Hey i didnt find any option to identify the lang in process. i want to change RTL for hebrew lang, for custom css. anyway i can do it? thanks.

  • Found the plugin useful on 30 Jun, 2016

    I find this plugin really useful!!! Some Bug... Hebrew not work! I Changed he to heb but steel Translate Messages not works! You may Look at the my site www.ghiml.co.il. Content in hebrew header and footer in english.

  • Found the plugin useful on 28 Jun, 2016

    It is amazing!!!!

  • Found the plugin useful on 10 Jun, 2016

    Thanks! That's what I was looking for! But not figure out possibility to use partials files as content files.

  • Found the plugin useful on 28 May, 2016

    Very nice plugin! I there an option to convert cyrillic characters to latin only? Like on button click

  • Found the plugin useful on 12 Apr, 2016

    Love this plugin! @Hessel, it will be perfect real soon! It already works in our testing website with the option for "Bleeding edge updates" turned on. I think it all wil be part of the upcoming version 1

  • Found the plugin not useful on 21 Mar, 2016

    It would be perfect if it worked with Static Pages!! But unfortunatelly it doesn't =( Rainlab doesn't respond to any questions or feedback from users since 2014

  • Found the plugin useful on 16 Feb, 2016

    It is the most useful translation system. Thanks ans sorry for my english :D

  • Found the plugin useful on 5 Feb, 2016

    Cool!

  • Found the plugin useful on 18 Oct, 2015

    This is quite good plugin and the methods are easy enough but unfortunately without url translation it negatively infects the SEO so cannot be used for paying customers.

  • Found the plugin useful on 11 Oct, 2015

    very awosem loving october deep down!

  • Found the plugin useful on 5 Oct, 2015

    Awesome plugin, thanks for that!

  • Found the plugin useful on 17 Aug, 2015

    This is a great plugin but I am having a hard time with caching.

    For example if I navigate to the Pirate's language URL then switch back to English I still view the Pirate language messages. I tried clearing the cache thousand times in my console, in the plugin interface, using a cache cleaner plugin /disabling cache and settings English as default language!

    I'd like to know if this is a bug and if not why is this happening. Am I missing something ?

    Again Thank you Samuel for this life-saver CMS / plugins.

  • Found the plugin useful on 1 Aug, 2015

    Thank you! This plugin is life-saver! ;)

  • Found the plugin useful on 31 May, 2015

    it's very useful for my project

  • Found the plugin useful on 4 May, 2015

    Hi, many thanks for this quality work. I live in Belgium and here, any site must be multilingual, we have three national languages. How to directly detect the browser language?

  • Found the plugin not useful on 30 Apr, 2015

    Hello. This not working in home page

  • Found the plugin useful on 29 Apr, 2015

    cool...thanks :D

  • Found the plugin useful on 5 Mar, 2015

    Amazing plugin! Thanks for all the hard work! :)

  • Found the plugin useful on 15 Feb, 2015

    Спасибо! :)

  • Found the plugin useful on 22 Dec, 2014

    Really helpful. @VoipDeploy Why don't you just open an issue in github?

  • Found the plugin not useful on 5 Nov, 2014

    because of the reported issues, plugin is useless for me. i ended up putting translations directly to the database as i gone tired and really frustrated with messages not saving from the GUI.

  • Found the plugin useful on 3 Nov, 2014

    How about translating the url itself? it would be nice to translate the path when creating new pages...

  • Found the plugin useful on 22 Oct, 2014

    how to handle content translation inside blog post? such as wordpress plugin qtranslate.

  • Found the plugin useful on 12 Aug, 2014

    Oh yeas I find this plugin really useful. Thank you very much, it's awesome.

1.2.12

Add support for translating the new grouped repeater feature. Pending

Apr 23, 2017

1.2.11

Fixes translation not working with partials loaded via AJAX

Apr 10, 2017

1.2.10

Minor UI fix to the multilingual repeater.

Feb 02, 2017

1.2.9

Fixes context for translated strings when used with Static Pages.

Jan 10, 2017

1.2.8

Improvements to code for latest October build compatibility.

Dec 13, 2016

1.2.7

Fixes placeholder usage with static pages plugin.

Nov 11, 2016

1.2.6

Fixes repeater usage with static pages plugin.

Nov 04, 2016

1.2.5

Adds new multilingual repeater form widget.

Oct 22, 2016

1.2.4

Translatable attributes can now be declared as indexes.

Aug 19, 2016

1.2.3

Fixes Rich Editor when inserting a page link.

Aug 12, 2016

1.2.2

Static Pages now support translating the URL.

Aug 05, 2016

1.2.1

Minor update in the rich editor and code editor language control position.

Jul 31, 2016

1.2.0

CMS pages now support translating the URL.

Jul 29, 2016

1.1.7

Fixes bug not showing content when first loading multilingual textarea controls.

Jul 12, 2016

1.1.6

Minor improvements in the message editor.

Jul 11, 2016

1.1.5

Minor update to the multilingual control API.

Jul 04, 2016

1.1.4

Adds new multilingual markdown editor.

May 25, 2016

1.1.3

Improved support for the multilingual rich editor.

May 23, 2016

1.1.2

Add support for translating Static Content files.

May 06, 2016

1.1.1

Minor UI fix to the language picker.

Apr 20, 2016

1.1.0

Introduce compatibility with RainLab.Pages plugin.

Apr 01, 2016

1.0.11

Fix multilingual rich editor when used in stretch mode.

May 15, 2015

1.0.10

Minor fix for latest build.

Apr 28, 2015

1.0.9

Fixes an issue where newly created models lose their translated values.

Mar 13, 2015

1.0.8

Minor bug fixes

Mar 04, 2015

1.0.7

Fixes major bug where translations are skipped entirely!

Feb 26, 2015

1.0.6

Minor improvements to the code.

Feb 13, 2015

1.0.5

Add spanish language and fix config plugin .

Nov 21, 2014

1.0.4

Locale cache will clear when updating a language.

Oct 10, 2014

1.0.3

Minor updates for latest October release.

Sep 28, 2014

1.0.2

Languages and Messages can now be deleted.

Aug 05, 2014

1.0.1

First version of Translate

Aug 02, 2014