/* * 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 . */ module 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 => { let strs_raw_ : lib_errormonade.type_errormonade = mod_vtm.mod_data.read("strings-" + language); if (lib_errormonade.filled(strs_raw_)) { let strs_raw : {[key : string] : string} = (<{[key : string] : string}>(lib_errormonade.read(strs_raw_))); _data[language] = {}; for (let key in strs_raw) { _data[language][key] = ((strs_raw[key])); } _languagestack.push(language); } else { let 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 { let message : string = ("keine Zeichenketten für Schlüssel '" + key + "' in Sprache '" + language + "'"); console.warn(message); return (lib_errormonade.create_nothing()); } } else { let 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()); let found : boolean = ( _languagestack .some ( language => { let 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 { let pattern : RegExp = (new RegExp("^!translate:([a-z0-9\.]*)$")); document.querySelectorAll("*")[""+"forEach"] ( element => { if (element.childElementCount === 0) { let content : string = element.textContent; let fund : any = pattern.exec(content); if (fund != null) { let key : string = fund[1]; let content_ : string = get(key); element.textContent = content_; } else { // nothing tun } } else { // nothing tun } } ) ; } }