From 72c1a9cd4cc1b2eb5bf1dc595ebc809675ecdfc4 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 23 Aug 2018 14:51:33 -0500 Subject: [PATCH 1/2] 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 }; - From 1a3e1a18df5fb96c98a193b02c0e104f90725d1f Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Thu, 23 Aug 2018 15:00:25 -0500 Subject: [PATCH 2/2] use console.warn and yellow text for locale fallback msg --- lib/core/i18n/i18n.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core/i18n/i18n.js b/lib/core/i18n/i18n.js index 0c758bbee..d28d70204 100644 --- a/lib/core/i18n/i18n.js +++ b/lib/core/i18n/i18n.js @@ -22,7 +22,7 @@ function setOrDetectLocale(locale) { let _locale = locale || osLocale.sync(); _locale = _locale.substr(0, 2); if (_locale && !isSupported(_locale)) { - console.log(`===== locale ${_locale} ${how} but not supported, default: en =====`); + console.warn(`===== locale ${_locale} ${how} but not supported, default: en =====`.yellow); return; } return i18n.setLocale(_locale);