/* * Verrückte Turing-Maschinen — A turing complete game * Copyright (C) 2016-2018 kcf * * 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 . */ namespace lib_translate { /** * @author kcf */ var _languagestack : Array; /** * @author kcf */ var _data : {[language : string] : {[key : string] : string}} = {}; /** * @author kcf */ export function setup(languages : Array) : void { _languagestack = []; languages.forEach ( language => { const strs_raw_ : lib_errormonade.type_errormonade = 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] = ((strs_raw[key])); } _languagestack.push(language); } else { const message : string = ("Zeichenketten für Sprache '" + language + "' konnten nicht geladen werden"); console.warn(message); } } ) ; } /** * @author kcf */ function read(language : string, key : string) : lib_errormonade.type_errormonade { if (language in _data) { let dataset : {[key : string] : string} = _data[language]; if (key in dataset) { return (lib_errormonade.create_just(dataset[key])); } else { const message : string = ("keine Zeichenketten für Schlüssel '" + key + "' in Sprache '" + language + "'"); console.warn(message); return (lib_errormonade.create_nothing()); } } else { const message : string = ("keine Zeichenketten für Sprache '" + language + "'"); console.warn(message); return (lib_errormonade.create_nothing()); } } /** * @author kcf */ export function get(key : string, vars : {[name : string] : string} = {}) : string { let result : lib_errormonade.type_errormonade = (lib_errormonade.create_nothing()); const found : boolean = ( _languagestack .some ( language => { const result_ : lib_errormonade.type_errormonade = 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 */ 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 = document.querySelectorAll("*"); if (old) { for (let index : int = 0; index < nodelist.length; index += 1) adjust(nodelist[index]) } else { nodelist.forEach(element => adjust(element)); } } }