vtm/quelldatein/helfer/uebersetzung.ts
2018-03-28 11:19:46 +02:00

174 lines
4.6 KiB
TypeScript

/*
* Verrückte Turing-Maschinen — A turing complete game
* Copyright (C) 2016 Christian Fraß <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/>.
*/
module lib_uebersetzung
{
/**
* @author kcf <vidofnir@folksprak.org>
*/
var _sprachstapel : Array<string>;
/**
* @author kcf <vidofnir@folksprak.org>
*/
var _daten : {[sprache : string] : {[schluessel : string] : string}} = {};
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function einrichten(sprachen : Array<string>) : void
{
_sprachstapel = [];
sprachen.forEach
(
sprache =>
{
let zeichenketten_roh_ : lib_fehlermonade.typ_fehlermonade<any> = mod_vtm.mod_daten.lesen("zeichenketten-" + sprache);
if (lib_fehlermonade.voll(zeichenketten_roh_))
{
let zeichenketten_roh : {[schluessel : string] : string} = (<{[schluessel : string] : string}>(lib_fehlermonade.lesen(zeichenketten_roh_)));
_daten[sprache] = {};
for (let schluessel in zeichenketten_roh)
{
_daten[sprache][schluessel] = (<string>(zeichenketten_roh[schluessel]));
}
_sprachstapel.push(sprache);
}
else
{
let meldung : string = ("Zeichenketten für Sprache '" + sprache + "' konnten nicht geladen werden");
console.warn(meldung);
}
}
)
;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function lesen(sprache : string, schluessel : string) : lib_fehlermonade.typ_fehlermonade<string>
{
if (sprache in _daten)
{
let satz : {[schluessel : string] : string} = _daten[sprache];
if (schluessel in satz)
{
return (lib_fehlermonade.erstellen_schlicht<string>(satz[schluessel]));
}
else
{
let meldung : string = ("keine Zeichenketten für Schlüssel '" + schluessel + "' in Sprache '" + sprache + "'");
console.warn(meldung);
return (lib_fehlermonade.erstellen_nichts<string>());
}
}
else
{
let meldung : string = ("keine Zeichenketten für Sprache '" + sprache + "'");
console.warn(meldung);
return (lib_fehlermonade.erstellen_nichts<string>());
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function holen(schluessel : string, vars : {[name : string] : string} = {}) : string
{
let ergebnis : lib_fehlermonade.typ_fehlermonade<string> = (lib_fehlermonade.erstellen_nichts<string>());
let gefunden : boolean = (
_sprachstapel
.some
(
sprache =>
{
let ergebnis_ : lib_fehlermonade.typ_fehlermonade<string> = lesen(sprache, schluessel);
if (lib_fehlermonade.voll(ergebnis_))
{
ergebnis = ergebnis_;
return true;
}
else
{
return false;
}
}
)
);
if (gefunden)
{
let str : string = lib_fehlermonade.lesen(ergebnis);
for (let name in vars)
{
let muster : RegExp = (new RegExp("!var:" + name));
let wert : string = vars[name];
str = str.replace(muster, wert);
}
return str;
}
else
{
return ("{" + schluessel + "}");
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function anwenden(kontext : DocumentFragment) : void
{
let muster : RegExp = (new RegExp("^!translate:([a-z0-9\.]*)$"));
document.querySelectorAll("*")[""+"forEach"]
(
element =>
{
if (element.childElementCount === 0)
{
let inhalt : string = element.textContent;
let fund : any = muster.exec(inhalt);
if (fund != null)
{
let schluessel : string = fund[1];
let inhalt_ : string = holen(schluessel);
element.textContent = inhalt_;
}
else
{
// nichts tun
}
}
else
{
// nichts tun
}
}
)
;
}
}