mirror of
https://github.com/openvk/openvk
synced 2024-11-11 01:19:53 +03:00
L10n: tr function for JS scripts
This commit is contained in:
parent
4048f21f59
commit
958846a86c
6 changed files with 65 additions and 0 deletions
|
@ -2,6 +2,7 @@
|
|||
namespace openvk\Web\Presenters;
|
||||
use openvk\Web\Themes\Themepacks;
|
||||
use openvk\Web\Models\Repositories\{Users, Managers};
|
||||
use openvk\Web\Util\Localizator;
|
||||
use Chandler\Session\Session;
|
||||
|
||||
final class AboutPresenter extends OpenVKPresenter
|
||||
|
@ -66,6 +67,17 @@ final class AboutPresenter extends OpenVKPresenter
|
|||
}
|
||||
}
|
||||
|
||||
function renderExportJSLanguage($lg = NULL): void
|
||||
{
|
||||
$localizer = Localizator::i();
|
||||
$lang = $lg;
|
||||
if(is_null($lg))
|
||||
$this->throwError(404, "Not found", "Language is not found");
|
||||
header("Content-Type: application/javascript");
|
||||
echo "window.lang = " . json_encode($localizer->export($lang)) . ";"; // привет хардкод :DDD
|
||||
exit;
|
||||
}
|
||||
|
||||
function renderSandbox(): void
|
||||
{
|
||||
$this->template->languages = getLanguages();
|
||||
|
|
|
@ -9,8 +9,10 @@
|
|||
<link rel="shortcut icon" href="/assets/packages/static/openvk/img/icon.ico" />
|
||||
<meta name="application-name" content="{$instance_name}" />
|
||||
<meta n:ifset="$csrfToken" name="csrf" value="{$csrfToken}" />
|
||||
<script src="/language/{php echo getLanguage()}.js" crossorigin="anonymous"></script>
|
||||
{script "js/node_modules/jquery/dist/jquery.min.js"}
|
||||
{script "js/node_modules/umbrellajs/umbrella.min.js"}
|
||||
{script "js/l10n.js"}
|
||||
{script "js/openvk.cls.js"}
|
||||
|
||||
{ifset $thisUser}
|
||||
|
|
|
@ -60,5 +60,13 @@ class Localizator
|
|||
return $array[$id] ?? "@$id";
|
||||
}
|
||||
|
||||
function export($lang = NULL): ?array
|
||||
{
|
||||
$lang = is_null($lang) ? static::DEFAULT_LANG : $lang;
|
||||
$array = @self::parse(dirname(__FILE__) . "/../../locales/$lang.strings");
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
use TSimpleSingleton;
|
||||
}
|
||||
|
|
|
@ -25,6 +25,8 @@ routes:
|
|||
handler: "Support->delete"
|
||||
- url: "/language"
|
||||
handler: "About->language"
|
||||
- url: "/language/{text}.js"
|
||||
handler: "About->exportJSLanguage"
|
||||
- url: "/donate"
|
||||
handler: "About->donate"
|
||||
- url: "/kb/{slug}"
|
||||
|
|
36
Web/static/js/l10n.js
Executable file
36
Web/static/js/l10n.js
Executable file
|
@ -0,0 +1,36 @@
|
|||
function tr(string, ...arg) {
|
||||
let output = window.lang[string];
|
||||
if(arg.length > 0) {
|
||||
if(typeof arg[0] == 'number') {
|
||||
let numberedStringId;
|
||||
let cardinal = arg[0];
|
||||
switch(cardinal) {
|
||||
case 0:
|
||||
numberedString = string + '_zero';
|
||||
break;
|
||||
case 1:
|
||||
numberedString = string + '_one';
|
||||
break;
|
||||
default:
|
||||
numberedString = string + (cardinal < 5 ? '_few' : '_other');
|
||||
}
|
||||
|
||||
let newoutput = window.lang[numberedString];
|
||||
if(newoutput === null) {
|
||||
newoutput = window.lang[string + '_other'];
|
||||
if(newoutput === null) {
|
||||
newoutput = output;
|
||||
}
|
||||
}
|
||||
|
||||
output = newoutput;
|
||||
}
|
||||
}
|
||||
|
||||
let i = 1;
|
||||
arg.forEach(element => {
|
||||
output = output.replace(RegExp('(\\$' + i + ')'), element);
|
||||
i++;
|
||||
});
|
||||
return output;
|
||||
}
|
|
@ -115,6 +115,11 @@ function setLanguage($lg): void
|
|||
trigger_error("The language '$lg' is not available", E_USER_NOTICE);
|
||||
}
|
||||
|
||||
function getLanguage(): string
|
||||
{
|
||||
return Session::i()->get("lang", "ru");
|
||||
}
|
||||
|
||||
function getLanguages(): array
|
||||
{
|
||||
return chandler_parse_yaml(OPENVK_ROOT . "/locales/list.yml")['list'];
|
||||
|
|
Loading…
Reference in a new issue