Languages

Languages

A language class exists inside the system/Core folder, this class have 2 methods:

  • load - Loads the language file, can return the data and set the language to use
  • get - return a language string if it exists, else it will return the value passed

Inside the system/Core/Controller.php file the class is instantiated, resulting in $this->language being available to all controllers.

To use a language inside a controller, you may use the following method, passing the filename to be loadedlanguage/code/filename.php by default the language code will be 'En' for english translation.

$this->language->load('file/to/load');

The load method can also be passed if the data is to be returned with a true or false and the language code, useful to set a new language on the call:

$this->language->load('file/to/load', 'Nl');

The default language can be set in the Config.php file:

//set a default language
define('LANGUAGE_CODE', 'En');

Inside the language file set the text, each language should contain the same text in their own language for instance:

//En
$lang['welcomeMessage'] = 'Hello, welcome from the welcome controller!';  //Nl $lang['welcomeMessage'] = 'Hallo, welkom van de welcome controller!';

To use the language strings inside a controller, set a data array and call the get method passing the desired string to return:

$data['welcomeMessage'] = $this->language->get('welcomeMessage');

Then in the view echo $data['welcomeMessage'] to print the chosen language.

Welcome example

namespace App\Controllers; use Core\View; use Core\Controller; class Welcome extends Controller { /** * call the parent construct */ public function __construct() { parent::__construct(); $this->language->load('Welcome'); } /** * define page title and load template files */ public function index() { $data['title'] = 'Welcome'; $data['welcomeMessage'] = $this->language->get('welcomeMessage'); View::rendertemplate('header', $data); View::render('Welcome/Welcome', $data); View::rendertemplate('footer', $data); } }

Language Changer

Languages can be changed by settings the language code in app/Config.php that's a system-wide change, to enable a language change for a user a language changer can be used. A new route exists as of 3.3.0:

Router::any('language/(:any)', 'App\Controllers\Language@change');

This allows for routes like these:

<a href='<?=DIR;?>language/cs'>Czech</a> <a href='<?=DIR;?>language/en'>English</a> <a href='<?=DIR;?>language/de'>German</a> <a href='<?=DIR;?>language/fr'>French</a> <a href='<?=DIR;?>language/it'>Italian</a> <a href='<?=DIR;?>language/nl'>Dutch</a> <a href='<?=DIR;?>language/pl'>Polish</a> <a href='<?=DIR;?>language/ro'>Romanian</a> <a href='<?=DIR;?>language/ru'>Russian</a>

Then each link when clicked will change the language for that user, the language code is stored in a session which falls back to a cookie.

Alternative Language System

PR #833 introduced a new Language System. The new Language System uses two framework-wide functions for string translation, the first one being __(), which is designed to be used in the App folder and the second being __d(), designed to be used on Language Domains; those Language Domains are the Modules, the Templates or the System.

IMPORTANT: When a specified domain matches both a Module and Template name, the Module will take precedence. In other words, there should not be a Module and a Template with the same name, for the Language System to work properly.

The usage of both translation functions is very simple, they contain the string to be translated and optional parameters. For example

// Get a translation for App Domain (default)
$translated = __('Hello, {0}', $username);  // Get a translation for Users Module Domain $translated = __d('users', 'Welcome back, {0}!', $username);  // Get a translation for System Domain $translated = __d('system', 'File not found: {0}', $filename);

Please note that the Domains are specified in snake_case transformation and that the translation string's parameters are using the ICU notation, which is used also by the PHP's INTL extension.

There is also a utility script that has been introduced, called scripts/makelangs.php, and it should be used to update the Framework's Language files, after introducing new translation strings in your application.

The Language files used by this new Language System are called messages.php and will be found in the usual locations, i.e. app/Language/En/messages.php or app/Modules/Users/Language/En/messages.php

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值