QuiiBz / next-international

Type-safe internationalization (i18n) for Next.js
https://next-international.vercel.app
MIT License
1.28k stars 59 forks source link

How to enable SSR for pages dir? (pageProps.locale is undefined during SSR rendering) #396

Closed addlistener closed 5 months ago

addlistener commented 5 months ago

My goal

i18n SSR or SSG for pages dir

The problem

the html generated from SSR or SSG is empty because no locale are provided on the server.

Known solution

next-intl seems have it by router.locale. https://next-intl-docs.vercel.app/docs/getting-started/pages-router

My detailed question -> how to construct pageProps.locale ?

I followed the docs for pages dir https://next-international.vercel.app/docs/pages-setup

If I log _app.tsx pageProps.locale in server I get undefined

So I constructed it manually. Following the structure below https://github.com/QuiiBz/next-international/blob/a049386f43bf2c42a2b3ed9b8d3e82b42ff9eebe/packages/next-international/src/pages/create-i18n-provider.tsx#L69-L76

// pages/index.tsx

export const getStaticProps = (async (context) => {
  const localeKey = context.locale ?? "en";
  const localeContent = flattenLocale((await allLoadedForServer[localeKey]()).default);
  console.log('getStaticProps', { localeKey, localeContent });
  return {
    props: {
      locale:  {
        localeContent: localeContent,
        // fallbackLocale: undefined,
        locale: localeKey,
      },
    }
  };
}) satisfies GetStaticProps<{
  // locale: any
}>

All I get is the message keys instead of the message values

image

Anything recommended I can do?

Versions

=> Found "next-international@1.2.4" => Found "next@14.1.4"

addlistener commented 5 months ago

Another potential feature request: provide messages on a page level ( just like https://next-intl-docs.vercel.app/docs/getting-started/pages-router)

I guess even if I successfully make the above work. There will be another problem which is the full set of message keys are loaded in any single page. It will be unnecessary overhead.

addlistener commented 5 months ago

I nailed it. pageProps.locale should be of type BaseLocale

addlistener commented 5 months ago

Another potential feature request: provide messages on a page level ( just like https://next-intl-docs.vercel.app/docs/getting-started/pages-router)

I guess even if I successfully make the above work. There will be another problem which is the full set of message keys are loaded in any single page. It will be unnecessary overhead.

I nailed it as well. picking the keys in pageProps.locale works