PHP Booosta 3 Tutorial

PHP Booosta 3 Tutorial

Languages

Booosta has a module that deals with different languages named translator. If it is installed there is the possibility to write multi language applications. Per default the language is always set to english (en). The current language can be changed with the configuration setting "language" in the config file. The value must be set to the 2-letter code of the language. At runtime the language can be changed with the session variable "LANG" ($_SESSION['LANG']).
So how do we tell the application what the translations for different languages are? There are two differnt parts concerning this:
  • The templates: For each template that has text that has to be translated you need to create an addtional language file. This has to have the extension that represents the 2-letter code of the language. The german version of the template tpl/hello.tpl would be tpl/hello.tpl.de. The translator first looks for a template named tpl/hello.tpl.de and then for tpl/hello.tpl when the language is set to "de". So if you have a template that does not need to be translated you can simply omit it and not copy it.
  • Text in the PHP code. If you use text strings in the PHP code that will be displayed on the page, you can use the method t() for translation. All the translations have to be in a file in the webroot named lang.xx where xx is the 2-letter code of the language. For german for example there has to be the file lang.de . If this file is not found or there is no translation for the text inside this file, t() just returns the text in the parameter. Here is an example file lang.de:
<?php

$this->map = [
'This is english' => 'Das ist deutsch',
'You have set the language to english' => 'Sie haben die Sprache auf deutsch gestellt',
];

?>

With this tranlations you have the following result of t():

$text = $this->t('This is english');     //  $text == 'Das ist deutsch'
$text = $this->t('This is not translated'); // $text == 'This is not translated'

Look at the previous page where we use the t() method. If you want to translate your application in an other language then create a lang.xx file and translate the template files in copies with .xx extension. Then change the "language" setting in local/config.incl.php and it should work!



Previous -