Localization
On the client side class Lang public/js/app/lang.js
is used to provide localization.
The main purpose of this class to get from the server-side
(if needed) the translation data for the current language and save them to
local storage. Organization of local storage is provided the library public/js/lib/jstorage/jstorage.js
.
Library details jStorage available here.
Get the translation data
The data in the local storage are stored a limited time. Time to store
data in the local storage set by the variable ttl_jstorage
in the initialization function init
of class System public/js/app/system.js
.
Sample code is shown below:
...
init: function () {
this.settings = {
message_box: 'msg-box', // 'alert-box', 'alert-block-box'
ttl_jstorage: 3600000 // 1h=3600000
};
...
Data for translation we get by getTransData
metod of class Lang public/js/app/lang.js
.
If the data on the server has changed compared with the data in the local storage,
then we get the modified data from the server to local storage.
Example check is shown below:
...
// Get translation data
translate = $.jStorage.get("trans");
if (translate) {
hash = translate.hash;
if (this.lang_hash === hash) {
return;
}
}
...
Example of a request to the server is shown below:
...
onAjaxSuccess = function (jsonData) {
// Get language data
if (jsonData.hash) {
// Set data for TTL
$.jStorage.set("trans", jsonData);
$.jStorage.setTTL("trans", ttl_jstorage);
}
};
// Send ajax request to the server
url = this['urlBase'] + '/lang';
this.sys.post(url, {hash: hash}, onAjaxSuccess, false);
...
Server request handling happening via the method langAction
of class IndexController app/Controllers/IndexController.php
Sample code is shown below:
...
if ($this->isAjaxRequest()) {
// get hash for translation messages
$hash = $this->getLangHash();
if (isset($this->params['hash']) && $this->params['hash'] !== $hash) {
$arTrans = $this->getLangMsgs();
$data['hash'] = $hash;
$data['values'] = $arTrans;
}
return $this->sendJson($data);
}
...
Use
The translation function is implemented by the method trans of class Lang public/js/app/lang.js
.
On the client side, you can use trans method as:
remaining == 1 ? app.lb.trans('task').capitalize() : app.lb.trans('tasks').capitalize()