regexper-static/src/i18n.js

56 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-01-06 17:08:37 +00:00
import i18n from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import Backend from 'i18next-xhr-backend';
export const locales = [
{ code: 'en', name: 'English' },
{ code: 'en-AC', name: 'English (ALL CAPS)' }
];
i18n
.use(LanguageDetector)
.use(Backend)
.init({
fallbackLng: 'en',
debug: false,
keySeparator: null,
nsSeparator: null,
saveMissing: true,
saveMissingTo: 'current',
missingKeyHandler: (lng, ns, key, fallback) => {
if (!i18n.getResourceBundle(lng)) {
return; // Don't bother logging if the resource bundle isn't loaded
}
2019-01-06 17:08:37 +00:00
const escapedKey = key.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
// eslint-disable-next-line no-console
if (console && console.group && console.log) {
// eslint-disable-next-line no-console
console.group(`Missing key in ${ lng }`);
// eslint-disable-next-line no-console
console.log(`"${ escapedKey }": |\n ${ fallback }`);
// eslint-disable-next-line no-console
console.groupEnd();
}
},
interpolation: {
escapeValue: false
},
backend: {
loadPath: '{{lng}}',
parse: data => data,
ajax: async (lng, options, callback) => {
2019-01-06 17:08:37 +00:00
try {
const { default: locale } = await import(`locales/${ lng }.yaml`);
callback(locale, { status: '200' });
2019-01-06 17:08:37 +00:00
}
catch (e) {
callback(null, { status: '500' });
2019-01-06 17:08:37 +00:00
}
}
}
});
export default i18n;