Last active
October 17, 2019 12:26
-
-
Save szkrd/fcf3314ee85694acb288897b39947c15 to your computer and use it in GitHub Desktop.
react-i18next detect missing keys during test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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