Download the PHP package rainlab/translate-plugin without Composer
On this page you can find all versions of the php package rainlab/translate-plugin. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rainlab/translate-plugin
More information about rainlab/translate-plugin
Files in rainlab/translate-plugin
Package translate-plugin
Short Description Translate plugin for October CMS
License MIT
Homepage https://octobercms.com/plugin/rainlab-translate
Informations about the package translate-plugin
Translation plugin
Enables multi-lingual sites.
Selecting a Language
Different languages can be set up in the admin panel using the Settings → Sites area. Each site should use a different locale to be considered a language.
The visitor can select a language by prefixing the language code to the URL or using a dedicated hostname. For example:
http://website.tld/
will display the site in the default languagehttp://website.tld/ru/
will display the site in Russianhttp://website.tld/fr/
will display the site in French
License
This plugin is an official extension of the October CMS platform and is free to use if you have a platform license. See EULA license for more details.
Installation
To install using October CMS v3.1 or above:
To install using October CMS v3.0 and below:
Upgrading from v1 to v2
If you are upgrading from version 1 of this plugin, view the upgrade guide.
Language Picker Component
A visitor can select their chosen language using the native SitePicker
component that is included in the October CMS core. This component will display a simple dropdown that changes the page language depending on the selection.
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:
Message Translation
Message or string translation is the conversion of adhoc strings used throughout the site. A message can be translated with parameters.
A message can also be translated for a choice usage.
Or you set a locale manually by passing a second argument.
Themes can provide default values for these messages by defining a translate
key in the theme.yaml
file, located in the theme directory.
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.
This is an example of config/lang.yaml file with two languages:
You may also define the translations in a separate file per locale, where the path is relative to the theme. The following definition will source the default messages from the file config/lang-en.yaml inside the theme for the english locale and from the file config/lang-fr.yaml for the french locale.
This is an example for the config/lang-en.yaml file:
In order to make these default values reflected to your frontend site, go to Settings -> Translate messages in the backend and hit Scan for messages. They will also be loaded automatically when the theme is activated.
The same operation can be performed with the translate:scan
artisan command. It may be worth including it in a deployment script to automatically fetch updated messages:
Add the --purge
option to clear old messages first:
Content & Mail Template Translation
This plugin activates a feature in the CMS that allows content & mail template files to use language suffixes, for example:
- welcome.htm will contain the content or mail template in the default language.
- welcome-ru.htm will contain the content or mail template in Russian.
- welcome-fr.htm will contain the content or mail template 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.
The attribute will then contain the default language value and other language code values can be created by using the translateContext()
method.
You may use the same process for setting values.
The lang()
method is a shorthand version of translateContext()
and is also chainable.
This can be useful inside a Twig template.
There are ways to get and set attributes without changing the context.
Theme Data Translation
It is also possible to translate theme customisation options. Just mark your form fields with translatable
property and the plugin will take care about everything else:
Fallback Attribute Values
By default, untranslated attributes will fall back to the default locale. This behavior can be disabled by calling the noFallbackLocale
method when reading the value.
When writing the value, the fallback value is determined when the translated value matches the default value. In these cases, the translated value is considered untranslated and not stored.
Locale | Attribute | Value | Is Stored |
---|---|---|---|
en | title | Hello World | Yes (Default) |
fr | title | Hello World | No |
de | title | Hallo Welt | Yes |
For example, if the en
default locale stores the message as "Hello World" and the fr
locale value is also "Hello World", then the fr
value is not stored. The fr
value is accessed using the fallback value taken from en
.
You may disable this behavior by passing the $transatable
attribute value as an array. The first value is the attribute name, the other values represent options, in this case setting the option fallback
to false
.
This above definition will force the title
attribute value to be duplicated and stored across all locales.
Indexed Attributes
Translatable model attributes can also be declared as an index by passing the $transatable
attribute value as an array. The first value is the attribute name, the other values represent options, in this case setting the option index
to true
.
Once an attribute is indexed, you may use the transWhere
method to apply a basic query to the model.
The transWhere
method accepts a third argument to explicitly pass a locale value, otherwise it will be detected from the environment.
URL Translation
Pages in the CMS support translating the URL property. Assuming you have 3 languages set up:
- en: English
- fr: French
- ru: Russian
There is a page with the following content:
The word "Contact" in French is the same so a translated URL is not given, or needed. If the page has no URL override specified, then the default URL will be used. Pages will not be duplicated for a given language.
- /fr/contact - Page in French
- /en/contact - Page in English
- /ru/контакт - Page in Russian
- /ru/contact - 404
Translating URLs in Twig
The localeUrl
method will replace the route prefix on a URL from one locale to another. For example, converting the current request URL from en
to de
.
The localePage
will return a translated URL for a CMS page. It takes a locale (first argument) and page parameters (second argument).
URL Parameter Translation
It's possible to translate URL parameters by listening to the cms.sitePicker.overrideParams
event, which is fired when discovering language URLs.
In MyModel
, one possible implementation might look like this:
Query String Translation
It's possible to translate query string parameters by listening to the cms.sitePicker.overrideQuery
event, which is fired when switching languages.
For a possible implementation of the MyModel::translateParams
method look at the example under URL parameter translation
from above.
Extend Theme Scan
Settings Model Translation
It's possible to translate your settings model like any other model. To retrieve translated values use:
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.
The back-end forms will automatically detect the presence of translatable fields and replace their controls for multilingual equivalents.
User Interface
Switching Locales
Users can switch between locales by clicking on the site selection menu in the backend panel. This will add a _site_id
query value to the URL, allowing for multiple browser tabs to be used.