From 72c1a9cd4cc1b2eb5bf1dc595ebc809675ecdfc4 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 23 Aug 2018 14:51:33 -0500 Subject: [PATCH] fix locale detection so it works w/ env var and/or --locale --- lib/core/i18n/i18n.js | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/lib/core/i18n/i18n.js b/lib/core/i18n/i18n.js index 8c772e772..0c758bbee 100644 --- a/lib/core/i18n/i18n.js +++ b/lib/core/i18n/i18n.js @@ -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 }; -