Hi i am trying to create a simple website that can toggle between 2 languages i am having a hard time trying to accomplish, i have set up all the localization in the payload just not sure how to render it and make a toggle between.
for context i can render the blocks and pages just not sure how to switch between the languages
Are you using the local api to fetch your data?
If so, you can provide a
localeargument in the options object that you provide to the local api.
That will retrieve the localized version of content you're looking for.
The part that isn't specific to payload is how you manage the state of which locale your user is in. Typically this is done with a routing strategy, which you can read about here:
you can also explore libraries like next-intl, if you'd like to go a bit deeper in to it. Personally, that is my default choice.
You don't need the dictionaries, or messages concept, unless you decide to translate things outside of your localized payload content. The main thing is just setting up internationalized routing and providing the language/locale param to your queries, so that it retrieves the appropriate data.
looking at setting up next-intl with the "without i18n routing" option and following the setup it says "Messages represent the translations that are available per language and can be provided either locally or loaded from a remote data source." How do a change the local for payload? as looking over the docs I cant see how to do this
the messages thing is what you do
notnecessarily need. You do not provide your blocks or payload content as "messages", you are simply using the locale routing to provide your application with a param of the user's language
if you're using the local api for example, you would pass that to the request, ie;
const Page = async ({params}) => {
const {segments} = await params;
const pathname = resolvePathname(segments)
const locale = getLocale()
const payload = await getPayload({config})
const data = await payload.find({collection: "page", locale: "en", where: {pathname: {equals: pathname}}, limit: 1})
}Okay, so i am setting up from this page.
I dont want the route to change based on the language
okay
I advise reconsidering that, as its ideal to be able to render the page on the server and the only other way to really do that is with client state, or headers outside of page routing and its much less efficient
okay
if you do it via headers, your entire site will be dynamic pretty much and if you do it client side, you wont get prerendered html markup, which is not great for seo
oh, just in case there was some confusion, I think I may have misunderstood
You can see this thread:
https://discordapp.com/channels/967097582721572934/1313553281582366770, it´s a similar discussion. I suggest to reading through the thread to understand the context. Here is the post with step by step:
https://discordapp.com/channels/967097582721572934/1313553281582366770/1314286546928668682you do want the "without i18n routing" but it doesnt mean your routes dont have specifical locales in them-- that refers to localized slugs entirely, where the entire url will be transliterated
I also have a repo you could look at, which implements next-intl, though next-intl has changed some of their api recently and those parts are no longer correct. (a few files renamed/placed differently) but the concepts are all the same, as well as the payload side.
thanks i have used next-intl to setup localization before didn't know i could match the two. i will try to make it work.
It works thank you so much it has been a headache for few weeks
you are my saving angel
Star
Discord
online
Get dedicated engineering support directly from the Payload team.