Plugin Development: Languages

Previous Section: Routes


Translate your plugin into different languages with ease.

Contents

File Structure

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 ':'.

Lang/en/lang.php

    <?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",

    ];

Usage

The language strings are available in the plugin namespace and can be called in the following way:

Controller

    Lang::get('Plugins#HelloWorld::lang.setting');                                // Setting
    Lang::choice('Plugins#HelloWorld::lang.world', 1);                            // World
    Lang::choice('Plugins#HelloWorld::lang.world', 2);                            // Worlds
    Lang::get('Plugins#HelloWorld::lang.placeholder_for', [ 'item' => 'Item' ]);  // This is a placeholder for Item
View

    {{ Lang.get('Plugins#HelloWorld::lang.setting') }}                            // Setting
    {{ Lang.choice('Plugins#HelloWorld::lang.world', 1) }}                        // World
    {{ Lang.choice('Plugins#HelloWorld::lang.world', 2) }}                        // Worlds
    {{ Lang.get('Plugins#HelloWorld::lang.placeholder_for', {'item': 'Item'}) }}  // This is a placeholder for Item

Overriding Translations

It is possible to override built-in language file translations. The example below overrides the addons/Languages/English/Lang/en/core.php submit_ticket translation and replaces it with the addons/Plugins/HelloWorld/Lang/en/lang.php Plugins#HelloWorld::lang.world translation.


    \Illuminate\Support\Facades\Lang::setParsedKey('core.submit_ticket', ['Plugins#HelloWorld', 'lang', 'world']);

The second argument is an array list of each individual component of the key. The : and . characters are emitted.


Next Section: Views