vtm/source/helpers/translate.ts
2025-09-23 12:13:49 +02:00

175 lines
4.5 KiB
TypeScript

/*
* Verrückte Turing-Maschinen — A turing complete game
* Copyright (C) 2016-2018 kcf <vidofnir@folksprak.org>
*
* 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/>.
*/
namespace lib_translate
{
/**
* @author kcf <vidofnir@folksprak.org>
*/
var _languagestack : Array<string>;
/**
* @author kcf <vidofnir@folksprak.org>
*/
var _data : {[language : string] : {[key : string] : string}} = {};
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function setup(languages : Array<string>) : void
{
_languagestack = [];
languages.forEach
(
language =>
{
const strs_raw_ : lib_errormonade.type_errormonade<any> = mod_vtm.mod_data.read("strings-" + language);
if (lib_errormonade.filled(strs_raw_))
{
const strs_raw : {[key : string] : string} = (<{[key : string] : string}>(lib_errormonade.read(strs_raw_)));
_data[language] = {};
for (const key in strs_raw)
{
_data[language][key] = (<string>(strs_raw[key]));
}
_languagestack.push(language);
}
else
{
const message : string = ("Zeichenketten für Sprache '" + language + "' konnten nicht geladen werden");
console.warn(message);
}
}
)
;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function read(language : string, key : string) : lib_errormonade.type_errormonade<string>
{
if (language in _data)
{
let dataset : {[key : string] : string} = _data[language];
if (key in dataset)
{
return (lib_errormonade.create_just<string>(dataset[key]));
}
else
{
const message : string = ("keine Zeichenketten für Schlüssel '" + key + "' in Sprache '" + language + "'");
console.warn(message);
return (lib_errormonade.create_nothing<string>());
}
}
else
{
const message : string = ("keine Zeichenketten für Sprache '" + language + "'");
console.warn(message);
return (lib_errormonade.create_nothing<string>());
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function get(key : string, vars : {[name : string] : string} = {}) : string
{
let result : lib_errormonade.type_errormonade<string> = (lib_errormonade.create_nothing<string>());
const found : boolean = (
_languagestack
.some
(
language =>
{
const result_ : lib_errormonade.type_errormonade<string> = read(language, key);
if (lib_errormonade.filled(result_))
{
result = result_;
return true;
}
else
{
return false;
}
}
)
);
if (found)
{
return lib_string.stance(lib_errormonade.read(result), vars);
}
else
{
return ("{" + key + "}");
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function deploy(context : DocumentFragment) : void
{
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));
}
}
}