Skip to content

Instantly share code, notes, and snippets.

@sazzer
Created March 5, 2020 14:09
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 sazzer/d54603aa94fb70b349d35a6471e2a441 to your computer and use it in GitHub Desktop.
Save sazzer/d54603aa94fb70b349d35a6471e2a441 to your computer and use it in GitHub Desktop.
Catch missing message keys in tests using i18next
import LanguageDetector from "i18next-browser-languagedetector";
import defaultTranslations from "./messages.json";
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
i18n
.use(LanguageDetector)
.use(initReactI18next)
.init({
resources: {
dev: {
translation: defaultTranslations
}
},
nsSeparator: false,
debug: false,
interpolation: {
escapeValue: false
},
parseMissingKeyHandler: key => {
if (process.env.NODE_ENV === "test") {
throw new Error(`Missing message key: ${key}`);
} else {
return `!!${key}!!`;
}
}
});
export default i18n;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment