1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-11 05:40:11 +02:00
copilot/frontend/src/intl.js

40 lines
948 B
JavaScript
Raw Normal View History

2016-10-20 22:42:39 +03:00
const qs = require('querystring');
const ReactIntl = require('react-intl');
const {
addLocaleData
} = ReactIntl;
module.exports = (({
Locales = {},
ReactIntlLocaleData = {}
}) => {
const en = Locales['en-us'] || {};
Object.keys(ReactIntlLocaleData).forEach((lang) => {
addLocaleData(ReactIntlLocaleData[lang] || []);
});
// http://stackoverflow.com/a/38150585
const detectedLocale = (
qs.parse((document.location.search || '').replace(/^\?/, '')).locale ||
navigator.languages && navigator.languages[0] || // Chrome / Firefox
navigator.language || // All browsers
navigator.userLanguage || // IE <= 10
'en-US'
).toLowerCase();
const lang = detectedLocale.split(/\-/)[0];
2016-10-20 22:46:50 +03:00
const locale = ReactIntlLocaleData[lang]
? (Locales[detectedLocale] ? detectedLocale : 'en-us')
: 'en-us';
2016-10-20 22:42:39 +03:00
return {
locale,
messages: {
...en,
...(Locales[locale] || {})
}
};
})(window);