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

44 lines
908 B
JavaScript
Raw Normal View History

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'];
i18n.configure({
locales: supported_languages,
register: global,
2018-08-23 17:53:18 +00:00
updateFiles: true,
syncFiles: true,
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
};