mirror of
https://github.com/ascribe/onion.git
synced 2024-11-16 01:55:07 +01:00
34 lines
1.2 KiB
JavaScript
34 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
import languages from '../constants/languages';
|
|
|
|
import { formatText } from './general_utils';
|
|
|
|
/**
|
|
* 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;
|
|
// this is just for testing, as changing the navigator.language wasn't possible
|
|
//ang = 'de';
|
|
try {
|
|
if(lang in languages) {
|
|
return formatText(languages[lang][s], args);
|
|
} else {
|
|
// just use the english language
|
|
return formatText(languages['en-US'][s], args);
|
|
}
|
|
} 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);
|
|
}
|
|
|
|
}
|
|
}
|