fix locale detection so it works w/ env var and/or --locale

This commit is contained in:
Michael Bradley, Jr 2018-08-23 14:51:33 -05:00
parent 5fe86e35d1
commit 72c1a9cd4c
1 changed files with 9 additions and 16 deletions

View File

@ -3,6 +3,7 @@ const osLocale = require('os-locale');
const path = require('path');
const supported_languages = ['en', 'pt', 'fr', 'es'];
const default_language = 'en';
i18n.configure({
locales: supported_languages,
@ -16,28 +17,20 @@ function isSupported(locale) {
return (supported_languages.indexOf(locale.substr(0, 2)) >= 0);
}
function setLocale(locale) {
i18n.setLocale(locale.substr(0, 2));
}
function setDefaultLocale() {
osLocale().then(setLocale).catch();
}
function setOrDetectLocale(locale) {
if (locale && !isSupported(locale)) {
console.log("===== locale " + locale + " not supported =====");
const how = locale ? 'specified' : 'detected';
let _locale = locale || osLocale.sync();
_locale = _locale.substr(0, 2);
if (_locale && !isSupported(_locale)) {
console.log(`===== locale ${_locale} ${how} but not supported, default: en =====`);
return;
}
if (locale) {
return i18n.setLocale(locale.substr(0, 2));
}
setDefaultLocale();
return i18n.setLocale(_locale);
}
setDefaultLocale();
i18n.setLocale(default_language);
module.exports = {
i18n: i18n,
setOrDetectLocale: setOrDetectLocale
};