vtm/quelldatein/helfer/uebersetzung.ts

173 lines
4.7 KiB
TypeScript
Raw Normal View History

2018-03-20 13:30:00 +01:00
/*
* 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 mod_vtm_helfer
{
/**
* @author kcf <vidofnir@folksprak.org>
*/
var _uebersetzung_sprachstapel : Array<string>;
/**
* @author kcf <vidofnir@folksprak.org>
*/
export var _uebersetzung_daten : {[sprache : string] : {[schluessel : string] : string}} = {};
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function uebersetzung_einrichten(sprachen : Array<string>) : void
{
_uebersetzung_sprachstapel = [];
sprachen.forEach
(
sprache =>
{
let zeichenketten_roh_ : schnittstelle_fehlermonade<any> = mod_vtm_daten.lesen("zeichenketten-" + sprache);
if (zeichenketten_roh_.ist_schlicht())
{
let zeichenketten_roh : {[schluessel : string] : string} = (<{[schluessel : string] : string}>(zeichenketten_roh_.lesen()));
_uebersetzung_daten[sprache] = {};
for (let schluessel in zeichenketten_roh)
{
_uebersetzung_daten[sprache][schluessel] = (<string>(zeichenketten_roh[schluessel]));
}
_uebersetzung_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 uebersetzung_lesen(sprache : string, schluessel : string) : schnittstelle_fehlermonade<string>
{
if (sprache in _uebersetzung_daten)
{
let satz : {[schluessel : string] : string} = _uebersetzung_daten[sprache];
if (schluessel in satz)
{
return (new klasse_schlicht<string>(satz[schluessel]));
}
else
{
let meldung : string = ("keine Zeichenketten für Schlüssel '" + schluessel + "' in Sprache '" + sprache + "'");
console.warn(meldung);
return (new klasse_nichts<string>());
}
}
else
{
let meldung : string = ("keine Zeichenketten für Sprache '" + sprache + "'");
console.warn(meldung);
return (new klasse_nichts<string>());
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function uebersetzung_holen(schluessel : string, vars : {[name : string] : string} = {}) : string
{
let ergebnis : schnittstelle_fehlermonade<string> = (new klasse_nichts<string>());
let gefunden : boolean = (
_uebersetzung_sprachstapel
.some
(
sprache =>
{
let ergebnis_ : schnittstelle_fehlermonade<string> = uebersetzung_lesen(sprache, schluessel);
if (ergebnis_.ist_schlicht())
{
ergebnis = ergebnis_;
return true;
}
else
{
return false;
}
}
)
);
if (gefunden)
{
let str : string = ergebnis.lesen();
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 uebersetzung_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 = uebersetzung_holen(schluessel);
element.textContent = inhalt_;
}
else
{
// nichts tun
}
}
else
{
// nichts tun
}
}
)
;
}
}