2018-03-20 13:30:00 +01:00
|
|
|
/*
|
|
|
|
|
* Verrückte Turing-Maschinen — A turing complete game
|
2018-03-29 22:00:42 +02:00
|
|
|
* Copyright (C) 2016-2018 kcf <vidofnir@folksprak.org>
|
2018-03-20 13:30:00 +01:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-03-27 19:21:40 +02:00
|
|
|
|
2025-09-23 12:13:49 +02:00
|
|
|
namespace lib_translate
|
2018-03-20 13:30:00 +01:00
|
|
|
{
|
|
|
|
|
|
2018-03-27 19:21:40 +02:00
|
|
|
/**
|
|
|
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
|
|
|
*/
|
2018-03-28 13:59:29 +02:00
|
|
|
var _languagestack : Array<string>;
|
2018-03-27 19:21:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
|
|
|
*/
|
2018-03-28 13:59:29 +02:00
|
|
|
var _data : {[language : string] : {[key : string] : string}} = {};
|
2018-03-27 19:21:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
|
|
|
*/
|
2018-03-28 13:59:29 +02:00
|
|
|
export function setup(languages : Array<string>) : void
|
2018-03-20 13:30:00 +01:00
|
|
|
{
|
2018-03-28 13:59:29 +02:00
|
|
|
_languagestack = [];
|
|
|
|
|
languages.forEach
|
2018-03-27 19:21:40 +02:00
|
|
|
(
|
2018-03-28 13:59:29 +02:00
|
|
|
language =>
|
2018-03-26 22:14:10 +02:00
|
|
|
{
|
2025-09-23 12:13:49 +02:00
|
|
|
const strs_raw_ : lib_errormonade.type_errormonade<any> = mod_vtm.mod_data.read("strings-" + language);
|
2018-03-28 13:59:29 +02:00
|
|
|
if (lib_errormonade.filled(strs_raw_))
|
2018-03-20 13:30:00 +01:00
|
|
|
{
|
2025-09-23 12:13:49 +02:00
|
|
|
const strs_raw : {[key : string] : string} = (<{[key : string] : string}>(lib_errormonade.read(strs_raw_)));
|
2018-03-28 13:59:29 +02:00
|
|
|
_data[language] = {};
|
2025-09-23 12:13:49 +02:00
|
|
|
for (const key in strs_raw)
|
2018-03-26 22:14:10 +02:00
|
|
|
{
|
2018-03-28 13:59:29 +02:00
|
|
|
_data[language][key] = (<string>(strs_raw[key]));
|
2018-03-20 13:30:00 +01:00
|
|
|
}
|
2018-03-28 13:59:29 +02:00
|
|
|
_languagestack.push(language);
|
2018-03-20 13:30:00 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-09-23 12:13:49 +02:00
|
|
|
const message : string = ("Zeichenketten für Sprache '" + language + "' konnten nicht geladen werden");
|
2018-03-28 13:59:29 +02:00
|
|
|
console.warn(message);
|
2018-03-20 13:30:00 +01:00
|
|
|
}
|
|
|
|
|
}
|
2018-03-27 19:21:40 +02:00
|
|
|
)
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
|
|
|
*/
|
2018-03-28 13:59:29 +02:00
|
|
|
function read(language : string, key : string) : lib_errormonade.type_errormonade<string>
|
2018-03-27 19:21:40 +02:00
|
|
|
{
|
2018-03-28 13:59:29 +02:00
|
|
|
if (language in _data)
|
2018-03-27 19:21:40 +02:00
|
|
|
{
|
2018-03-28 13:59:29 +02:00
|
|
|
let dataset : {[key : string] : string} = _data[language];
|
|
|
|
|
if (key in dataset)
|
2018-03-27 19:21:40 +02:00
|
|
|
{
|
2018-03-28 13:59:29 +02:00
|
|
|
return (lib_errormonade.create_just<string>(dataset[key]));
|
2018-03-27 19:21:40 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-09-23 12:13:49 +02:00
|
|
|
const message : string = ("keine Zeichenketten für Schlüssel '" + key + "' in Sprache '" + language + "'");
|
2018-03-28 13:59:29 +02:00
|
|
|
console.warn(message);
|
|
|
|
|
return (lib_errormonade.create_nothing<string>());
|
2018-03-27 19:21:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-09-23 12:13:49 +02:00
|
|
|
const message : string = ("keine Zeichenketten für Sprache '" + language + "'");
|
2018-03-28 13:59:29 +02:00
|
|
|
console.warn(message);
|
|
|
|
|
return (lib_errormonade.create_nothing<string>());
|
2018-03-27 19:21:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
|
|
|
*/
|
2018-03-28 13:59:29 +02:00
|
|
|
export function get(key : string, vars : {[name : string] : string} = {}) : string
|
2018-03-27 19:21:40 +02:00
|
|
|
{
|
2018-03-28 13:59:29 +02:00
|
|
|
let result : lib_errormonade.type_errormonade<string> = (lib_errormonade.create_nothing<string>());
|
2025-09-23 12:13:49 +02:00
|
|
|
const found : boolean = (
|
2018-03-28 13:59:29 +02:00
|
|
|
_languagestack
|
2018-03-27 19:21:40 +02:00
|
|
|
.some
|
|
|
|
|
(
|
2018-03-28 13:59:29 +02:00
|
|
|
language =>
|
2018-03-27 19:21:40 +02:00
|
|
|
{
|
2025-09-23 12:13:49 +02:00
|
|
|
const result_ : lib_errormonade.type_errormonade<string> = read(language, key);
|
2018-03-28 13:59:29 +02:00
|
|
|
if (lib_errormonade.filled(result_))
|
2018-03-27 19:21:40 +02:00
|
|
|
{
|
2018-03-28 13:59:29 +02:00
|
|
|
result = result_;
|
2018-03-27 19:21:40 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
2018-03-28 13:59:29 +02:00
|
|
|
if (found)
|
2018-03-27 19:21:40 +02:00
|
|
|
{
|
2018-03-29 01:19:49 +02:00
|
|
|
return lib_string.stance(lib_errormonade.read(result), vars);
|
2018-03-27 19:21:40 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-03-28 13:59:29 +02:00
|
|
|
return ("{" + key + "}");
|
2018-03-27 19:21:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
|
|
|
*/
|
2018-03-28 13:59:29 +02:00
|
|
|
export function deploy(context : DocumentFragment) : void
|
2018-03-27 19:21:40 +02:00
|
|
|
{
|
2025-09-23 12:13:49 +02:00
|
|
|
const pattern : RegExp = (new RegExp("^!translate:([a-z0-9\.]*)$"));
|
|
|
|
|
const adjust = (
|
|
|
|
|
element =>
|
|
|
|
|
{
|
|
|
|
|
if (element.childElementCount > 0)
|
|
|
|
|
{
|
|
|
|
|
// nichts tun
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
const content : string = element.textContent;
|
|
|
|
|
const fund : any = pattern.exec(content);
|
|
|
|
|
if (fund === null)
|
|
|
|
|
{
|
|
|
|
|
// nichts tun
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
const key : string = fund[1];
|
|
|
|
|
const content_ : string = get(key);
|
|
|
|
|
element.textContent = content_;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
const old : boolean = true;
|
|
|
|
|
const nodelist : NodeListOf<Element> = document.querySelectorAll("*");
|
|
|
|
|
if (old)
|
|
|
|
|
{
|
|
|
|
|
for (let index : int = 0; index < nodelist.length; index += 1) adjust(nodelist[index])
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
nodelist.forEach(element => adjust(element));
|
|
|
|
|
}
|
2018-03-20 13:30:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|