/* * Verrückte Turing-Maschinen — A turing complete game * Copyright (C) 2016 Christian Fraß * * 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 . */ module lib_uebersetzung { /** * @author kcf */ var _sprachstapel : Array; /** * @author kcf */ var _daten : {[sprache : string] : {[schluessel : string] : string}} = {}; /** * @author kcf */ export function einrichten(sprachen : Array) : void { _sprachstapel = []; sprachen.forEach ( sprache => { let zeichenketten_roh_ : lib_fehlermonade.typ_fehlermonade = 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] = ((zeichenketten_roh[schluessel])); } _sprachstapel.push(sprache); } else { let meldung : string = ("Zeichenketten für Sprache '" + sprache + "' konnten nicht geladen werden"); console.warn(meldung); } } ) ; } /** * @author kcf */ function lesen(sprache : string, schluessel : string) : lib_fehlermonade.typ_fehlermonade { if (sprache in _daten) { let satz : {[schluessel : string] : string} = _daten[sprache]; if (schluessel in satz) { return (lib_fehlermonade.mod_schlicht.erstellen(satz[schluessel])); } else { let meldung : string = ("keine Zeichenketten für Schlüssel '" + schluessel + "' in Sprache '" + sprache + "'"); console.warn(meldung); return (lib_fehlermonade.mod_nichts.erstellen()); } } else { let meldung : string = ("keine Zeichenketten für Sprache '" + sprache + "'"); console.warn(meldung); return (lib_fehlermonade.mod_nichts.erstellen()); } } /** * @author kcf */ export function holen(schluessel : string, vars : {[name : string] : string} = {}) : string { let ergebnis : lib_fehlermonade.typ_fehlermonade = (lib_fehlermonade.mod_nichts.erstellen()); let gefunden : boolean = ( _sprachstapel .some ( sprache => { let ergebnis_ : lib_fehlermonade.typ_fehlermonade = 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 */ 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 } } ) ; } }