Localization

Localization is the process of adapting internationalized software for a specific region or language by translating text and adding locale-specific components.

Quantum comes with built in `Lang` library which provides a convenient way to retrieve strings in various languages.

Language strings are stored in files located under Views/lang of specific module and should correspond to the languages defined in config file, which has the following structure.


return array(
    'langs' => ['en', 'ru', 'am'],
    'lang_default' => 'en',
);

        

The lang_default is the default language and should be one of the values in langs array.

The folder structure in the Views/lang directory, to be appropriate to config.php file, would be the following:

Views/lang/
  • en/
    • translations_one.php
    • translations_two.php
    • ...
  • ru/
    • translations_one.php
    • translations_two.php
    • ...
  • am/
    • translations_one.php
    • translations_two.php
    • ...

A language file is a key-value based array having similar structure:


return array(
    'topic_title' => 'The topic title',
    'topic_description' => 'The topic description',
    'ps_text' => 'Some post scriptum text',
);

        

To start using the Lang library first make it available in your controller.


use Quantum\Libraries\Lang\Lang;

        

In the constructor set the language.


public function __construct() {
    Lang::set('en');
}

        

At any point you want to get the current language you can use get() method of Lang library.

Retrieving Language Translations

The Quantum provides two helper functions to retrieve language translations in your view files.

  • t() - Returns the translated text
  • _t() - Outputs the translated text

You can output the translations in view files like following (where the messages is the filename of translations file):


<h1><?php _t('messages.topic_title') ?></h1>
<p><?php _t('messages.topic_description') ?></p>
<small><?php _t('messages.ps_text') ?></small>

        

Or if you using Twig templates:


<h1>{{ t('messages.topic_title') }}</h1>
<p><{{ t('messages.topic_description') }}</p>
<small>{{ t('messages.ps_text') }}</small>

        
<< Prev Next >>