Skip to content

Latest commit

 

History

History
126 lines (77 loc) · 3.5 KB

translate.rst

File metadata and controls

126 lines (77 loc) · 3.5 KB

Translate an application

Each application possess the lang directory where are located translation files, that will be called dictionaries.

This directory include as many subdirectories as languages we want to translate the application into. Dictionaries will be define in these subdirectories.

These language directories are named following locales, as for example fr (French), or en (English).

image

Dictionaries are PHP files returning a array, similarly as configuration file.

I18n class API <api:php/classes/i18n>

File metadata.config.php

metadata are a particular case, since they are cached. They need a translation file on their own. First step is to define which dictionaries metadata need to be translated.

<?php

return array(
    'name' => 'My app',
    'namespace' => 'My\App',
    'i18n_file' => 'my_app::metadata',
    // ... other keys
);

As all change on metadata, don't forget to apply changes in the application manager.

Next, you need to create the my_app::lang/fr/metadata.lang.php dictionary:

<?php

return array(
    'My app' => 'My application',
);

Novius OS automatically knows which keys has to be translated in the metadata file and will get corresponding translations.

Other files

Elsewhere, you need to use the __() function, which will retrieve (by default) the translations from the my_app::default dictionary.

<?php

// Translation will be retrieve from my_app::lang/<lang>/default.lang.php
__('Translate this');

Advanced mode: configure your own dictionaries

If you don't want to put all your translations in the default.lang.php file, you can configure in which dictionary the translations will be retrieved, in each file which uses the __() function.

It is quite simple for view and configuration files:

<?php

// Configure the __() function
Nos\I18n::current_dictionary('my_app::common');

__('Translate this'); // Translation will be collected from my_app::lang/<lang>/common.lang.php

It is a little more complicated for admin controllers, because language depends on the user and is known only after authentication, which happens in before().

prepare_i18n() has been implemented to solve this problem:

<?php

namespace Nos\Form;

class Controller_Admin_Form extends \Nos\Controller_Admin_Crud
{
    public function prepare_i18n()
    {
        // Configure language file depending on user
        parent::prepare_i18n();
        // Configure the __() function
        \Nos\I18n::current_dictionary('noviusos_form::common');
    }

    // Other methods using __()
}

It is possible to use many dictionaries in only one file ; just use an array instead of a string. Translation will be choose from the first file containing required key.

<?php

Nos\I18n::current_dictionary(array('my_app::dictionary', 'my_app::common'));

// Translation will be collected from my_app::lang/<lang>/dictionary.lang.php if it exists
// Otherwise in my_app::lang/<lang>/common.lang.php
__('Translate this');