Override language routing #681
-
I'm using next-translate in my Next.js app. When you change languages, it edits the url to have a /[locale] at the end. Is there a way to disable this? We want all languages to have the exact same url (ie. both en and es homepage are '/'). This is a private website, so SEO is not an issue. Any help? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 9 replies
-
All the i18n routing is a feature from Next.js not from this library, documented here: https://nextjs.org/docs/advanced-features/i18n-routing. So in principle, it is not supported. Maybe you can achieve this by using the https://github.com/vinissimus/next-translate#i18nprovider Something like: import React, { useContext, createContext } from 'react'
import I18nProvider from 'next-translate/I18nProvider'
import useTranslation from 'next-translate/useTranslation'
const I18nCtx = createContext()
export function I18nWrapper({ children }) {
const i18n = useTranslation()
const [lang, changeLang] = useState(i18n.lang)
return (
<I18nCtx.Provider value={changeLang}>
<I18nProvider key={lang} lang={lang}>
{children}
</I18nProvider>
</I18nCtx.Provider>
)
}
export function useChangeLang() {
return useContext(I18nCtx)
} Where you put the |
Beta Was this translation helpful? Give feedback.
-
Thank you for the information. I tried your example.
If I change namespaces at the same time, locale will be changed as follows.
Can't we change locale only with lang? Or do I need more settings? |
Beta Was this translation helpful? Give feedback.
-
@aralroca I am having issues getting this to work as expected as well. Providing a detailed example below, excluding the Wrapper implementation
Wrapper usage
When the button is pressed, it properly updates the state of the Any additional pointers on this is greatly appreciated. |
Beta Was this translation helpful? Give feedback.
-
I can't get it to work. I'd like to be able to handle it outside of next's routes. There are many applications that handle translations with other methods outside of the URL (obviously losing SEO):
|
Beta Was this translation helpful? Give feedback.
-
Is there a way to make this work with app router ? I noticed we have As the main post I would also like to do translations that don't change the url. |
Beta Was this translation helpful? Give feedback.
-
For App Routing, folder should be name as (app)/[lang]/pages.tsx and not [locales] |
Beta Was this translation helpful? Give feedback.
-
I have an app that may/may not be localized depending on settings from the CMS (all routing is handled from the CMS). I am unsure how to handle this. The recommended approach where you nest all routes inside a [lang] slug won't work, as I will have both layout(s), pages, not-found, errors, etc. in routes without a lang param. I am trying to avoid a query parameter, so a simple cookie set from middleware would be a reasonably simple solution I guess. However, I am unsure how Is it correctly understood that |
Beta Was this translation helpful? Give feedback.
All the i18n routing is a feature from Next.js not from this library, documented here: https://nextjs.org/docs/advanced-features/i18n-routing. So in principle, it is not supported.
Maybe you can achieve this by using the
I18nProvider
on top of your application, where the lang is stored by you and controlled by you.https://github.com/vinissimus/next-translate#i18nprovider
Something like: