Default namespace not working as what I expected #2067
-
I'm sorry that this is not really something contributing to the project. It's only my bad job at setting up but I can't find any helps elsewhere (posted something similar on StackOverflow but no answer). If this is not suitable, please tell me somewhere I can ask this, I'll be delightfully move this question there. I'm trying to use i18next with create-react-app and set up similar to the official docs of i18next. But my intellisense and typescript doesn't look right. My translation json {
"example": "text"
}
import "i18next";
import { resources } from "../i18n";
declare module "i18next" {
interface CustomTypeOptions {
resources: (typeof resources)["en"];
}
}
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import en from "./locale/en.json";
export const resources = {
en: {
translation: en,
},
};
i18n.use(initReactI18next).init({
resources,
lng: "en",
interpolation: {
escapeValue: false,
},
});
export default i18n; I tried to use the t function in my import "./App.css";
import { useTranslation } from "react-i18next";
const App = () => {
const { t } = useTranslation();
return (
<div className="App">
{/* This does not work */}
<p>{t("example")}</p>
{/* This does not work either */}
<p>{t("translation.example")}</p>
{/* This works, but unexpected */}
<p>{t("translation:example")}</p>
</div>
);
};
export default App; As far as I know, i18next has the default namespace of const { t } = useTranslation("translation"); But it doesn't change anything of intellisense and typescript error. For context, I'm using latest version for the time being: I want to know why and how this interaction work, since I'm working with some legacy code and cannot overhaul the whole thing (beforehand it doesn't use typescript and has an older version of i18next). Edit: My minimal reproduction of what I'm doing: https://github.com/ngtrthinh169TPC/rep-i18next-ts . Without context of the legacy code, the interaction is still the same. Edit 2: As a side note: my t function returns type |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 14 replies
-
https://github.com/ngtrthinh169TPC/rep-i18next-ts/pull/1/files |
Beta Was this translation helpful? Give feedback.
That's the problem: