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 path = require('path');
const supported_languages = ['en', 'pt', 'fr', 'es']; const supported_languages = ['en', 'pt', 'fr', 'es'];
const default_language = 'en';
i18n.configure({ i18n.configure({
locales: supported_languages, locales: supported_languages,
@ -16,28 +17,20 @@ function isSupported(locale) {
return (supported_languages.indexOf(locale.substr(0, 2)) >= 0); 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) { function setOrDetectLocale(locale) {
if (locale && !isSupported(locale)) { const how = locale ? 'specified' : 'detected';
console.log("===== locale " + locale + " not supported ====="); 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);
return i18n.setLocale(locale.substr(0, 2));
}
setDefaultLocale();
} }
setDefaultLocale(); i18n.setLocale(default_language);
module.exports = { module.exports = {
i18n: i18n, i18n: i18n,
setOrDetectLocale: setOrDetectLocale setOrDetectLocale: setOrDetectLocale
}; };