2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-06-01 18:20:15 +02:00
|
|
|
import languages from '../constants/languages';
|
|
|
|
|
2015-06-02 13:42:17 +02:00
|
|
|
import { formatText } from './general_utils';
|
2015-06-02 13:31:12 +02:00
|
|
|
|
2015-06-02 14:33:30 +02:00
|
|
|
/**
|
|
|
|
* Is used to translate strings to another language. Basically can be used with C's string format method.
|
|
|
|
* @param {string} s The string you want to translate
|
|
|
|
* @param {array} args An array of arguments (essentially JavaScript's this.arguments) that can be used to substitute digits and other strings
|
|
|
|
* @return {string} The formated string
|
|
|
|
*/
|
2015-06-02 13:48:01 +02:00
|
|
|
export function getLangText(s, ...args) {
|
2015-06-01 18:20:15 +02:00
|
|
|
let lang = navigator.language || navigator.userLanguage;
|
2015-06-02 14:29:06 +02:00
|
|
|
// this is just for testing, as changing the navigator.language wasn't possible
|
2015-06-03 16:34:32 +02:00
|
|
|
//lang = 'de';
|
2015-06-02 12:00:59 +02:00
|
|
|
try {
|
|
|
|
if(lang in languages) {
|
2015-06-02 13:42:17 +02:00
|
|
|
return formatText(languages[lang][s], args);
|
2015-06-02 12:00:59 +02:00
|
|
|
} else {
|
|
|
|
// just use the english language
|
2015-06-02 13:42:17 +02:00
|
|
|
return formatText(languages['en-US'][s], args);
|
2015-06-02 12:00:59 +02:00
|
|
|
}
|
|
|
|
} catch(err) {
|
2015-06-02 13:42:17 +02:00
|
|
|
if(!(s in languages[lang])) {
|
2015-07-07 10:28:39 +02:00
|
|
|
console.warn('Language-string is not in constants file. Add: "' + s + '" to the "' + lang + '" language file. Defaulting to keyname');
|
|
|
|
return s;
|
2015-06-02 13:42:17 +02:00
|
|
|
} else {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
|
2015-06-01 18:20:15 +02:00
|
|
|
}
|
2015-06-08 13:47:24 +02:00
|
|
|
}
|