Open meightythree opened 1 year ago
Are you using useRouter
? Please provide a reproducible example
The example is on the stackblitz link. There are two buttons english/spanish, if you click either of them the error will occur.
Ah ok. Yes, setLanguage
is not adapted yet to app dir, you can use useRouter
from 'next/navigation': https://beta.nextjs.org/docs/api-reference/use-router
Thank you for the quick answer @aralroca
I have extended stackblitz with the provided example in next-translate docs, however it does not change the language with Link tags either. What am I missing @aralroca?
I'm gonna explain. Well, setLanguage
has Router
provided from next/router
which is deprecated in Next.js 13 (I mean, specifically, only in appDir) and should use next/navigation
instead. This is why your current stackblitz is not working.
This is how I solved this issue, I created a custom hook
function useSetLanguage() {
const router = useRouter();
const pathname = usePathname();
const searchParams = useSearchParams();
return (locale: string) => {
// now you got a read/write object
const current = new URLSearchParams(Array.from(searchParams.entries())); // -> has to use this form
current.set('lang', locale);
// cast to string
const search = current.toString();
// or const query = `${'?'.repeat(search.length && 1)}${search}`;
const query = search ? `?${search}` : '';
router.push(`${pathname}${query}`);
};
}
Which then I use like this:
const MyComponent = () => {
const { lang } = useTranslation('common');
const setLanguage = useSetLanguage();
return (
<Select
onValueChange={(value) => setLanguage(value)}
defaultValue={lang}
>
...
</Select>
);
};
And this is where I found this solution: https://github.com/vercel/next.js/discussions/47583#discussioncomment-5449707
@aralroca I can try to make a PR to include this if it makes sense to you π
@joaopedrodcf feel free to PR π thanks
@joaopedrodcf is there a way not to include lang
as a queryParam and use NEXT_LOCALE
cookie?
Didn't have time to create the PR about this sorry @aralroca maybe someone can take it over :/
@meightythree I would say that is possible but I didn't tried it, just remove the part where we see the query params and check the cookie, then you shouldn't need to add the router.push as well, as the url will be the same.
@meightythree I would say that is possible but I didn't tried it, just remove the part where we see the query params and check the cookie, then you shouldn't need to add the router.push as well, as the url will be the same.
Thanks I will take a look and comment again if I succeed!
Hi @meightythree,
I was wondering if you managed to discover a viable solution to utilize the setLanguage feature.
Hi @meightythree
I second the question from @ThijmenGThN Did you manage to find a solution? π
May close the issue for no activity π
I haven't found a solution... started using another package next-international. Sorry it's not the answer you wanted to see.
I could repro the issue on stackblitz
What version of this package are you using? "next": "13.3.0", "next-translate": "^2.0.4", "next-translate-plugin": "^2.0.4"
What operating system, Node.js, and npm version? stackblitz, node v16.14.2, npm 7.17.0
What happened? I got an error when I change the language.
What did you expect to happen? Language should change without error.
Are you willing to submit a pull request to fix this bug? Hell yea, this would be my first open source contribution, I might need a little help thou.