Merge pull request #740 from embark-framework/bug_fix/detect-locale

fix locale detection
This commit is contained in:
Iuri Matias 2018-08-23 16:16:56 -04:00 committed by GitHub
commit 3fe1cd4a2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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.warn(`===== locale ${_locale} ${how} but not supported, default: en =====`.yellow);
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
}; };