I want to add translator for my project but i18next show key and have missing key error in console.
my translations path: src/locales/{Language}/translation.json
i18n.ts code in src directory:
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import Backend from "i18next-http-backend";
import LanguageDetector from "i18next-browser-languagedetector";
import i18next from "i18next";
i18n.use(Backend)
.use(LanguageDetector)
.use(initReactI18next)
.init({
fallbackLng: "en",
debug: true,
interpolation: {
escapeValue: false,
},
backend: {
loadPath: "./locales/{{lng}}/translation.json",
},
});
export default i18n;
- react-i18next.d.ts code :
import "react-i18next";
import en from "./src/locales/en/translation.json";
import fa from "./src/locales/fa/translation.json";
declare module "react-i18next" {
interface Resources {
en: typeof en;
fa: typeof fa;
}
}
and translation.json is like this:
{
"Hello": "Hello World"
}
- use in component:
import { useTranslation } from "react-i18next";
function hello (){
const { t, i18n } = useTranslation();
return (
<div>
<h1> {t("Hello")}</h1>
</div>
);
}