language change in nextjs without changing url
Asked Answered
S

2

6

is there any way to switch language in nextjs without passing language parameter in url like baseurl/ar or baseurl/en ? if I want to change language from dropdown, url should not change.

Saprogenic answered 29/9, 2022 at 18:4 Comment(1)
If you're using Next.js i18n routing, the locale is required in the URL (for non-default locales). You could implement your own i18n routing solution if you don't want that behaviour.Provide
V
0

You need to import Link from next/link, and get the router instance from useRouter, or withRouter hoc. Usage:

<Link href={router.pathname} locale='your language'/>

Should work fine!

Valida answered 27/3, 2023 at 21:5 Comment(0)
R
-1

query parameters are defined by the routes of your api, in this way, when you change the url, a new call is made in the api passing the parameters that were informed in the route.

you can pass objects to the api route, something like:

const response = awai api.get('yourUrl', {language: en})

this way the information will not appear in the url, but this needs to be changed in the backend so that it knows where to get the parameters from.

  • you can choose to do the translation in json files, and just switch between them too.

Hope this helps.

#NSL

Righteous answered 29/9, 2022 at 19:45 Comment(1)
This answer doesn't address the issue on the question.Lover

© 2022 - 2025 — McMap. All rights reserved.