Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat): pass locale to params inside getStaticProps to load content based on locale #933

Merged
merged 1 commit into from
Oct 16, 2024

Conversation

JayaKrishnaNamburu
Copy link
Member

NextJS has a default support for loading content using locale. Which means we don't need to manually add the [locale] route to get it from the URL and pass it to the data fetchers. You can read it more about it here.
https://nextjs.org/docs/pages/building-your-application/routing/internationalization#sub-path-routing

module.exports = {
  i18n: {
    locales: ['en', 'ro'],
    defaultLocale: 'en',
  },
}

You can directly get the locale from the contextinsidegetStaticProps`.

export async function getStaticProps(context) {
  try {
    const response = await blogpostsPageInitialPropsTqRZResource({
      ...context?.params,
      ...(context?.locale && {
        locale: context.locale,
      }),
    })
    if (!response) {
      return {
        notFound: true,
      }
    }
    return {
      props: {
        blogpostsEntities: response,
        ...response?.meta,
      },
      revalidate: 60,
    }
  } catch (error) {
    console.log(error)
    return {
      notFound: true,
    }
  }
}

So, we are going to pass the locale field to all the data-fetchers by default. This PR only adds support to load content using the locale for cms-pages. If the projects are using cms-list or cms-item which uses DataProvider to fetch the data. Then we need to take the locale from the route using useRouter from nextjs. Which we can discuss further when needed.

This locale field is used by different cms platforms in their own way, as passing as query-params, post body, gql query etc.

@JayaKrishnaNamburu JayaKrishnaNamburu self-assigned this Sep 10, 2024
@JayaKrishnaNamburu JayaKrishnaNamburu added the cms All pull requests and bugs related to cms integrations label Sep 10, 2024
@JayaKrishnaNamburu JayaKrishnaNamburu merged commit 4388577 into development Oct 16, 2024
2 checks passed
@JayaKrishnaNamburu JayaKrishnaNamburu deleted the pass-locale-to-props branch October 16, 2024 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cms All pull requests and bugs related to cms integrations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant