Added support for lang (localized strings)

This commit is contained in:
Niko 2022-02-15 19:26:43 +01:00
parent 35145b9677
commit 873bfbf6fe
4 changed files with 48 additions and 2 deletions

View File

@ -1,7 +1,36 @@
<?php
function s($name) {
echo $name;
function strings() {
$lang = client_lang();
if (isset($GLOBALS['lang_'.$lang]))
return $GLOBALS['lang_'.$lang];
$file = 'config/lang/'.$lang.'.php';
if (!file_exists($file))
return [];
require_once $file;
if (!isset($strings))
return [];
$GLOBALS['lang_'.$lang] = $strings;
return $strings;
}
function s($name, $return=false) {
$strings = strings();
$key = "['".str_replace('.', "']['", $name)."']";
$val = eval('echo $strings'.$key.' ?? $name;');
if ($return)
return $val;
echo $val;
}
function client_lang() {
if (!isset($GLOBALS['cfg_lang'])) {
require_once 'config/lang.php';
$GLOBALS['cfg_lang'] = [ $default_lang, $supported_langs ];
}
$lang = substr(trim($_SERVER['HTTP_ACCEPT_LANGUAGE']), 0, 2);
return in_array($lang, $GLOBALS['cfg_lang'][1]) ?
$lang : $GLOBALS['cfg_lang'][0];
}
function urlargs($from = 0) {

7
web/src/config/lang.php Normal file
View File

@ -0,0 +1,7 @@
<?php
$default_lang = 'en';
$supported_langs = [
'en',
'es',
];

View File

@ -0,0 +1,5 @@
<?php
$strings = [
];

View File

@ -0,0 +1,5 @@
<?php
$strings = [
];