embark-area-51/lib/core/i18n/i18n.js

43 lines
884 B
JavaScript
Raw Normal View History

const i18n = require('i18n');
const osLocale = require('os-locale');
const path = require('path');
2018-05-20 19:53:08 +00:00
const supported_languages = ['en', 'pt', 'fr'];
i18n.configure({
locales: supported_languages,
register: global,
2018-06-01 17:44:49 +00:00
updateFiles: false,
2018-07-20 18:55:20 +00:00
directory: path.join(__dirname, '../../../', 'locales')
});
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 =====");
}
if (locale) {
return i18n.setLocale(locale.substr(0, 2));
}
setDefaultLocale();
}
setDefaultLocale();
module.exports = {
i18n: i18n,
setOrDetectLocale: setOrDetectLocale
};