Multi-language support

This commit is contained in:
bergware
2020-03-12 08:19:13 +01:00
parent cbadf80e0f
commit 05eaadea74
5 changed files with 72 additions and 55 deletions

View File

@@ -78,6 +78,7 @@ $notes = "&nbsp;<a href='#' title='"._('View Release Notes')."' onclick=\"openBo
</style>
<script src="<?autov('/webGui/javascript/dynamix.js')?>"></script>
<script src="<?autov('/webGui/javascript/translations.js')?>"></script>
<script>
Shadowbox.init({skipSetup:true});

View File

@@ -59,7 +59,11 @@ case 'add':
if ($result) exit(implode(',',$installed));
case 'rm':
$path = "$docroot/languages/$file";
if ($result = is_dir($path)) {exec("rm -rf ".escapeshellarg($path)); @unlink("$boot/$file.lang.zip");}
if ($result = is_dir($path)) {
exec("rm -rf ".escapeshellarg($path));
@unlink("$docroot/webGui/javascript/translations.js");
@unlink("$boot/$file.lang.zip");
}
$installed = [];
foreach (glob("$docroot/languages/*",GLOB_ONLYDIR) as $dir) $installed[] = basename($dir);
if ($result) exit(implode(',',$installed));

View File

@@ -79,21 +79,33 @@ $language = [];
$locale = $_SESSION['locale'];
if ($locale) {
$uri = explode('/',explode('?',$_SERVER['REQUEST_URI'])[0]);
// split URI into translation levels
$uri = array_filter(explode('/',strtok($_SERVER['REQUEST_URI'],'?')));
$text = "$docroot/languages/$locale/translations.txt";
if (file_exists($text)) {
$data = "$docroot/languages/$locale/translations.dot";
if (!file_exists($data)) file_put_contents($data,serialize(parse_lang_file($text)));
$language = unserialize(file_get_contents($data));
$basis = "$docroot/languages/$locale/translations.dot";
$jsTxt = "$docroot/languages/$locale/javascript.txt";
$jsOut = "$docroot/webGui/javascript/translations.js";
// global translations
if (!file_exists($basis)) file_put_contents($basis,serialize(parse_lang_file($text)));
$language = unserialize(file_get_contents($basis));
if (!file_exists($jsOut)) {
// create javascript file with translations
$source = file_exists($jsTxt) ? parse_lang_file($jsTxt) : [];
$script = ['function _(t){var l={};'];
foreach ($source as $key => $value) $script[] = "l[\"$key\"]=\"$value\";";
$script[] ="return l[t.replace(/\&amp;|[\?\{\}\|\&\~\!\[\]\(\)\/\\:\*^\.\"']|<.+?\/?>/g,'').replace(/ +/g,' ')]||t;}";
file_put_contents($jsOut,implode('',$script));
}
}
foreach($uri as $add) {
if (!$add) continue;
$add = strtolower($add);
$text = "$docroot/languages/$locale/$add.txt";
foreach($uri as $more) {
$more = strtolower($more);
$text = "$docroot/languages/$locale/$more.txt";
if (file_exists($text)) {
$data = "$docroot/languages/$locale/$add.dot";
if (!file_exists($data)) file_put_contents($data,serialize(parse_lang_file($text)));
$language = array_merge($language,unserialize(file_get_contents($data)));
// additional translations
$other = "$docroot/languages/$locale/$more.dot";
if (!file_exists($other)) file_put_contents($other,serialize(parse_lang_file($text)));
$language = array_merge($language,unserialize(file_get_contents($other)));
}
}
}