Plugin Development: Languages
Previous Section: Routes
Translate your plugin for several different languages with ease.
Translations are stored in the Lang folder, and within this directory there should be a subdirectory for each language supported. The files within each subdirectory must have the same names, as these are used as keys when calling the language strings. An example file structure where English and French are supported:
/Lang
/en
lang.php
other.php
/fr
lang.php
other.php
The file should contain an array of language key to language string, each key must be unique. Plurals can be defined by using a pipe (|
) character between the words, and placeholders can be defined by starting a word with ':
'. Below is an example language file for the HelloWorld plugin.
<?php
return [
"hello_world" => "Hello World",
"setting" => "Setting",
"setting_desc" => "A description of the setting.",
"worlds" => "World|Worlds",
"placeholder_for" => "This is a placeholder for :item",
];
The language strings are then made available in the plugin namespace and can be called in the following way:
Lang::get('HelloWorld::lang.setting'); // Setting
Lang::choice('HelloWorld::lang.world', 1); // World
Lang::choice('HelloWorld::lang.world', 2); // Worlds
Lang::get('HelloWorld::lang.placeholder_for', [ 'item' => 'Item' ]); // This is a placeholder for Item
{{ Lang.get('HelloWorld::lang.setting') }} // Setting
{{ Lang.choice('HelloWorld::lang.world', 1) }} // World
{{ Lang.choice('HelloWorld::lang.world', 2) }} // Worlds
{{ Lang.get('HelloWorld::lang.placeholder_for', {'item': 'Item'}) }} // This is a placeholder for Item