Next.js Discord

Discord Forum

Next-intl passing groups of messages

Answered
Jumbo flying squid posted this in #help-forum
Open in Discord
Jumbo flying squidOP
Hey all, how do we pass groups of messages using next intl? My use case is I have say message groups 'common' and 'userpage'. I dont want to pass single messages as the docs suggest. I also dont want to pass the whole translation file to the client component. Ideally, I can just pass down the required groups of messages to call.

en.json file

"Common": {
    "Follow": "Follow",
    "VisitWebsite": "Visit Website",
    "Apply": "Apply",
    "Listings": "Listings",
    "Submit": "Submit",
    "Error": "Error",
    "Creator": "Creator",
    "Brand": "Brand"
  },
  "UserTypeSelectPage": {
    "Title": "Welcome to Valley",
    "Subtitle": "Are you a Creator or a Brand?",
    "Modal": {
      "BodyLabel": "Who should select this?",
      "CreatorBody": "Users who create content on websites such as: Twitch, Youtube, and Instagram",
      "BrandBody": "Users who want to advertise using creators"
    }

Server component
const commonMessages = await getTranslations("Common");
  const userTypeSelectPageMessages =
    await getTranslations("UserTypeSelectPage");


then pass the above as props down to the component
Answered by Jumbo flying squid
did this by wrapping the component in the provider, and using 'pick' function to pull out messages I need and wrap them back into the expected object. option 3 in here https://next-intl-docs.vercel.app/docs/environments/server-client-components#option-3-providing-individual-messages. just make the messages an array
View full answer

1 Reply

Jumbo flying squidOP
did this by wrapping the component in the provider, and using 'pick' function to pull out messages I need and wrap them back into the expected object. option 3 in here https://next-intl-docs.vercel.app/docs/environments/server-client-components#option-3-providing-individual-messages. just make the messages an array
Answer