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

39 lines
1.3 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-07-15 14:52:14 +02:00
export function getLang() {
// this is just for testing, as changing the navigator.language wasn't possible
// return 'fr';
return navigator.languages ? navigator.languages[0] :
(navigator.language || navigator.userLanguage);
}
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) {
2015-07-15 14:52:14 +02:00
let lang = getLang();
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) {
//if(!(s in languages[lang])) {
//console.warn('Language-string is not in constants file. Add: "' + s + '" to the "' + lang + '" language file. Defaulting to keyname');
return s;
//} else {
// console.error(err);
//}
}
2015-07-03 19:08:56 +02:00
}