How to translate <title> with i18next? #3666
Closed
edwin-speer
started this conversation in
General
Replies: 2 comments 4 replies
-
For anyone with the same problem: I fixed it with a hook:
And then include it in your topmost component.
|
Beta Was this translation helpful? Give feedback.
3 replies
-
@edwin-speer You sir are a gentleman and a scholar 🎩 This helped me resolve the issue with translating Turns out the configuration of {
defaultNS: false,
ns: [],
} to remove namespaces was causing /**
* A hook that manages language change effects in the application.
* It tracks the current i18n locale and updates the document's text direction
* when the language changes. The hook also forces a component rerender by
* updating state and triggers a router invalidation to ensure the application
* updates accordingly.
*
* The hook uses a state setter to trigger rerenders and sets up an i18n
* languageChanged event listener to handle language changes. It cleans up
* the event listener when the component unmounts.
*
*/
export function useLanguageChange() {
const [, setLocale] = useState(i18n.language);
const router = useRouter();
useEffect(() => {
const handler = (locale: string) => {
setLocale(locale);
document.documentElement.dir = i18n.dir(locale);
void router?.invalidate();
};
i18n.on("languageChanged", handler);
return () => i18n.off("languageChanged", handler);
}, []);
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using i18next to translate texts. But I'm unable to translate the page title (title meta tag/document.title).
When I do this (when following the documentation):
The text is not translated, probably because the translation file is not yet loaded when
head()
is called.The
<RouterProvider/>
is wrapped inside<I18nextProvider/>
.It seems the problem is that
head()
is only called once and not again when the translation file is loaded.I now "solved" it in a very hacky (and possibly brittle) way, like this
Later I read out the
translationKey
and setdocument.title
, but it's very non standard and not a desired solution.I couldn't find any documentation on how to solve this, although this problem seems to be not uncommon.
Is this a bug, feature request or am I missing something?
I'm using
@tanstack/router-plugin 1.111.3
Beta Was this translation helpful? Give feedback.
All reactions