Multi-lang support.

Let's learn how to enjoy the easy multi-lang functionality of Direct!


Explanations

Briefly : you will enter the line $Page->setLanguage("lang_you_want"), in which lang_you_want is the language you want in the ISO nomenclature (for example : english is "en", french is "fr"...) in your controller file, after declaring the Page class.

/* * For example in Controller/Index/indexController.php */

$Page = new Page(); // Declaring Page class
$Page->setLanguage("en"); // Setting the language

Translation files.

OK, you've selected a language, now you have to know where are translations stored and how translation files are working.

Create the translation repository

Translation files are stored under ./Components/langs/.

In this directory, you will have the choice to create directories depending on which language you want.

For example for the english language, you need to create an en/ directory. So the complete path to your translation directory will be ./Components/langs/en/.

Create the translation files

Create a new file inside ./Components/langs/en/. Here is the nomenclature to respect (facultative if you use the first way to call your translation file) :

FolderName.fileViewName.json

For example, we have the index view file under ./View/Index/indexView.php, the correct nomenclature for the translation file will be Index.index.json.

Content of the translation files

Translation files are json files. So syntax is simple. Here is an example.

Inside ./Components/langs/en/Index.index.json :

{
    "greetings":"Hello!",
    "framework_name":"My name is Direct Framework"
}

Calling the translations

Translations are used whether in views or in models. There are two ways to call the translations.


  • Query the translation with the file name. In any view or model, put :

    <?php

    echo $Page->getString("greetings","Index.index.php"); // Will output "Hello!" if the language set is "en".

    ?>

    Here, the file called is ./Components/langs/en/Index.index.json.


  • Directly query the string from a view (here from the Index/indexView.php view).

    The translation file name must respect the nomenclature given before.

    
    <?php

    echo $Page->getString("greetings"); // Will output "Hello!" if the language set is "en".

    ?>

    Here, the framework directly detects where is the translation called from, and attributes the translation file to look for as ./Components/langs/en/Index.index.json.

Now you are up to start building your website with any language! You can put as many languages as you want.

Need help ? Contact the developer !