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-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
|
|
|
|
*/
|
2015-06-02 13:48:01 +02:00
|
|
|
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) {
|
2015-07-15 15:54:37 +02:00
|
|
|
//if(!(s in languages[lang])) {
|
2015-07-16 11:50:52 +02:00
|
|
|
//console.warn('Language-string is not in constants file. Add: "' + s + '" to the "' + lang + '" language file. Defaulting to keyname');
|
2015-08-24 12:11:07 +02:00
|
|
|
return formatText(s, args);
|
2015-07-15 15:54:37 +02:00
|
|
|
//} else {
|
|
|
|
// console.error(err);
|
|
|
|
//}
|
2015-06-01 18:20:15 +02:00
|
|
|
}
|
2015-07-03 19:08:56 +02:00
|
|
|
}
|