Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Localization Nextjs

default discord avatar
idctaglast year
10

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

  • default discord avatar
    zephury.last year

    Are you using the local api to fetch your data?



    If so, you can provide a

    locale

    argument 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:


    https://nextjs.org/docs/app/building-your-application/routing/internationalization

    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.

  • default discord avatar
    odd_plebeianlast year
    @132273173847736320

    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

  • default discord avatar
    zephury.last year

    the messages thing is what you do

    not

    necessarily 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})
    }
  • default discord avatar
    odd_plebeianlast year

    Okay, so i am setting up from this page.



    https://next-intl.dev/docs/getting-started/app-router/without-i18n-routing

    I dont want the route to change based on the language



    okay

  • default discord avatar
    zephury.last year

    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

  • default discord avatar
    odd_plebeianlast year

    okay

  • default discord avatar
    zephury.last year

    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

  • default discord avatar
    themachine0488last year
    @934777092862803979

    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/1314286546928668682
  • default discord avatar
    zephury.last year

    you 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.


    https://github.com/conqdb/conqdb/tree/main/apps/web
  • default discord avatar
    idctaglast year
    @132273173847736320

    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.



    @132273173847736320

    It works thank you so much it has been a headache for few weeks



    you are my saving angel

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.