vtm/source/helpers/translate.ts

167 lines
4.2 KiB
TypeScript
Raw Normal View History

2018-03-20 13:30:00 +01:00
/*
* Verrückte Turing-Maschinen A turing complete game
2018-03-29 22:00:42 +02:00
* Copyright (C) 2016-2018 kcf <vidofnir@folksprak.org>
2018-03-20 13:30:00 +01:00
*
* 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/>.
*/
2018-03-27 19:21:40 +02:00
2018-03-28 13:59:29 +02:00
module lib_translate
2018-03-20 13:30:00 +01:00
{
2018-03-27 19:21:40 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
var _languagestack : Array<string>;
2018-03-27 19:21:40 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
var _data : {[language : string] : {[key : string] : string}} = {};
2018-03-27 19:21:40 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function setup(languages : Array<string>) : void
2018-03-20 13:30:00 +01:00
{
2018-03-28 13:59:29 +02:00
_languagestack = [];
languages.forEach
2018-03-27 19:21:40 +02:00
(
2018-03-28 13:59:29 +02:00
language =>
2018-03-26 22:14:10 +02:00
{
2018-03-28 13:59:29 +02:00
let strs_raw_ : lib_errormonade.type_errormonade<any> = mod_vtm.mod_data.read("strings-" + language);
if (lib_errormonade.filled(strs_raw_))
2018-03-20 13:30:00 +01:00
{
2018-03-28 13:59:29 +02:00
let strs_raw : {[key : string] : string} = (<{[key : string] : string}>(lib_errormonade.read(strs_raw_)));
_data[language] = {};
for (let key in strs_raw)
2018-03-26 22:14:10 +02:00
{
2018-03-28 13:59:29 +02:00
_data[language][key] = (<string>(strs_raw[key]));
2018-03-20 13:30:00 +01:00
}
2018-03-28 13:59:29 +02:00
_languagestack.push(language);
2018-03-20 13:30:00 +01:00
}
else
{
2018-03-28 13:59:29 +02:00
let message : string = ("Zeichenketten für Sprache '" + language + "' konnten nicht geladen werden");
console.warn(message);
2018-03-20 13:30:00 +01:00
}
}
2018-03-27 19:21:40 +02:00
)
;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
function read(language : string, key : string) : lib_errormonade.type_errormonade<string>
2018-03-27 19:21:40 +02:00
{
2018-03-28 13:59:29 +02:00
if (language in _data)
2018-03-27 19:21:40 +02:00
{
2018-03-28 13:59:29 +02:00
let dataset : {[key : string] : string} = _data[language];
if (key in dataset)
2018-03-27 19:21:40 +02:00
{
2018-03-28 13:59:29 +02:00
return (lib_errormonade.create_just<string>(dataset[key]));
2018-03-27 19:21:40 +02:00
}
else
{
2018-03-28 13:59:29 +02:00
let message : string = ("keine Zeichenketten für Schlüssel '" + key + "' in Sprache '" + language + "'");
console.warn(message);
return (lib_errormonade.create_nothing<string>());
2018-03-27 19:21:40 +02:00
}
}
else
{
2018-03-28 13:59:29 +02:00
let message : string = ("keine Zeichenketten für Sprache '" + language + "'");
console.warn(message);
return (lib_errormonade.create_nothing<string>());
2018-03-27 19:21:40 +02:00
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function get(key : string, vars : {[name : string] : string} = {}) : string
2018-03-27 19:21:40 +02:00
{
2018-03-28 13:59:29 +02:00
let result : lib_errormonade.type_errormonade<string> = (lib_errormonade.create_nothing<string>());
let found : boolean = (
_languagestack
2018-03-27 19:21:40 +02:00
.some
(
2018-03-28 13:59:29 +02:00
language =>
2018-03-27 19:21:40 +02:00
{
2018-03-28 13:59:29 +02:00
let result_ : lib_errormonade.type_errormonade<string> = read(language, key);
if (lib_errormonade.filled(result_))
2018-03-27 19:21:40 +02:00
{
2018-03-28 13:59:29 +02:00
result = result_;
2018-03-27 19:21:40 +02:00
return true;
}
else
{
return false;
}
}
)
);
2018-03-28 13:59:29 +02:00
if (found)
2018-03-27 19:21:40 +02:00
{
2018-03-29 01:19:49 +02:00
return lib_string.stance(lib_errormonade.read(result), vars);
2018-03-27 19:21:40 +02:00
}
else
{
2018-03-28 13:59:29 +02:00
return ("{" + key + "}");
2018-03-27 19:21:40 +02:00
}
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function deploy(context : DocumentFragment) : void
2018-03-27 19:21:40 +02:00
{
2018-03-28 13:59:29 +02:00
let pattern : RegExp = (new RegExp("^!translate:([a-z0-9\.]*)$"));
2018-03-27 19:21:40 +02:00
document.querySelectorAll("*")[""+"forEach"]
(
element =>
2018-03-20 13:30:00 +01:00
{
2018-03-27 19:21:40 +02:00
if (element.childElementCount === 0)
2018-03-20 13:30:00 +01:00
{
2018-03-28 13:59:29 +02:00
let content : string = element.textContent;
let fund : any = pattern.exec(content);
2018-03-27 19:21:40 +02:00
if (fund != null)
{
2018-03-28 13:59:29 +02:00
let key : string = fund[1];
let content_ : string = get(key);
element.textContent = content_;
2018-03-27 19:21:40 +02:00
}
else
2018-03-20 13:30:00 +01:00
{
2018-03-28 13:59:29 +02:00
// nothing tun
2018-03-20 13:30:00 +01:00
}
}
else
{
2018-03-28 13:59:29 +02:00
// nothing tun
2018-03-20 13:30:00 +01:00
}
}
2018-03-27 19:21:40 +02:00
)
;
2018-03-20 13:30:00 +01:00
}
}