Skip to content

Instantly share code, notes, and snippets.

@szkrd
Last active October 17, 2019 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szkrd/fcf3314ee85694acb288897b39947c15 to your computer and use it in GitHub Desktop.
Save szkrd/fcf3314ee85694acb288897b39947c15 to your computer and use it in GitHub Desktop.
react-i18next detect missing keys during test
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import resources from '../locales/translations.json';
// we disable the translation texts for dev mode so that the
// tests would only break if an ID is missing but not
// if the translation has been changed by the translators.
const devTranslations = Object.keys(resources['en-US'].translation).reduce((acc, key) => {
acc[key] = key;
return acc;
}, {});
// you can use this for specific components with
// `<I18nextProvider i18n={i18nForTests}><Component...`
// but probably including it in setupTests is better ofr us.
i18n.use(initReactI18next).init({
lng: 'dev',
preload: ['dev'],
resources: {
dev: {
translation: devTranslations
}
},
// use `debug: true` to get more info about missing keys!
parseMissingKeyHandler: (key) => {
console.warn(`react-i18next key "${key}" not found.`);
return key;
}
});
export { i18n as i18nForTests };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment