2018-05-09 22:43:09 +00:00
|
|
|
const i18n = require('i18n');
|
|
|
|
const osLocale = require('os-locale');
|
|
|
|
const path = require('path');
|
|
|
|
|
2018-08-14 20:37:16 +00:00
|
|
|
const supported_languages = ['en', 'pt', 'fr', 'es'];
|
2018-08-23 19:51:33 +00:00
|
|
|
const default_language = 'en';
|
2018-05-09 22:43:09 +00:00
|
|
|
|
|
|
|
i18n.configure({
|
|
|
|
locales: supported_languages,
|
|
|
|
register: global,
|
2018-08-23 18:16:31 +00:00
|
|
|
updateFiles: false,
|
|
|
|
syncFiles: false,
|
2018-07-20 18:55:20 +00:00
|
|
|
directory: path.join(__dirname, '../../../', 'locales')
|
2018-05-09 22:43:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function isSupported(locale) {
|
|
|
|
return (supported_languages.indexOf(locale.substr(0, 2)) >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setOrDetectLocale(locale) {
|
2018-08-23 19:51:33 +00:00
|
|
|
const how = locale ? 'specified' : 'detected';
|
|
|
|
let _locale = locale || osLocale.sync();
|
|
|
|
_locale = _locale.substr(0, 2);
|
|
|
|
if (_locale && !isSupported(_locale)) {
|
2018-08-23 20:00:25 +00:00
|
|
|
console.warn(`===== locale ${_locale} ${how} but not supported, default: en =====`.yellow);
|
2018-08-23 19:51:33 +00:00
|
|
|
return;
|
2018-05-09 22:43:09 +00:00
|
|
|
}
|
2018-08-23 19:51:33 +00:00
|
|
|
return i18n.setLocale(_locale);
|
2018-05-09 22:43:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 19:51:33 +00:00
|
|
|
i18n.setLocale(default_language);
|
2018-05-09 22:43:09 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
i18n: i18n,
|
|
|
|
setOrDetectLocale: setOrDetectLocale
|
|
|
|
};
|