1
0
mirror of https://github.com/ascribe/onion.git synced 2024-11-15 17:45:10 +01:00
onion/js/utils/lang_utils.js

33 lines
1.2 KiB
JavaScript
Raw Normal View History

'use strict';
import languages from '../constants/languages';
2015-06-02 13:42:17 +02:00
import { formatText } from './general_utils';
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
*/
export function getLangText(s, ...args) {
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-08 13:47:24 +02:00
}