2016-06-13 15:05:53 +02:00
|
|
|
import { getBrowserLang } from 'js-utility-belt/es6/lang';
|
2015-06-05 11:06:36 +02:00
|
|
|
|
2015-06-01 18:20:15 +02:00
|
|
|
import languages from '../constants/languages';
|
|
|
|
|
2016-06-13 16:16:27 +02:00
|
|
|
import { formatText } from './text';
|
2015-06-02 13:31:12 +02:00
|
|
|
|
2015-07-15 14:52:14 +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.
|
2016-06-13 15:06:06 +02:00
|
|
|
* @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
|
2015-06-02 14:33:30 +02:00
|
|
|
* @return {string} The formated string
|
|
|
|
*/
|
2015-06-02 13:48:01 +02:00
|
|
|
export function getLangText(s, ...args) {
|
2016-06-13 15:05:53 +02:00
|
|
|
const lang = getBrowserLang();
|
2016-06-13 15:06:06 +02:00
|
|
|
let str = s;
|
|
|
|
|
2015-06-02 12:00:59 +02:00
|
|
|
try {
|
2016-06-13 15:06:06 +02:00
|
|
|
// Default to english if the language isn't found
|
|
|
|
const language = languages[lang in languages ? lang : 'en-US'];
|
|
|
|
|
|
|
|
// Default to the original string if no match is found
|
|
|
|
str = language[s] || s;
|
|
|
|
} catch (err) {
|
|
|
|
// TODO: turn the warning back on when our localization is on point
|
|
|
|
if (false && process.env.NODE_ENV !== 'production') {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.warn(`Language-string is not in constants file. Add: "${s}" to the "${lang}"` +
|
|
|
|
'language file. Defaulting to keyname');
|
2015-06-02 12:00:59 +02:00
|
|
|
}
|
2015-06-01 18:20:15 +02:00
|
|
|
}
|
2016-06-13 15:06:06 +02:00
|
|
|
|
|
|
|
return formatText(str, ...args);
|
2015-07-03 19:08:56 +02:00
|
|
|
}
|