Using typesafe-i18n with Next.js 13 App Directory #580
-
Data fetching changes significantly with Next.js 13 App Directory and I'm not sure how to implement typesafe-i18n with the removal of getStaticProps. Any help would be appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 23 replies
-
I can check if I find some time next week. Haven't used Next.js 13 yet. |
Beta Was this translation helpful? Give feedback.
-
Also keen on this, thanks! |
Beta Was this translation helpful? Give feedback.
-
A colleague of mine has figured this out. When i get the chance, I'll post
an overview.
…On Thu, 11 May 2023, 02:32 Hofer Ivan, ***@***.***> wrote:
Unfortunately I don't had time to do it until now and it also does not
look that good in the next weeks.
In the end it is just making the correct calls in the correct places. You
need to first understand how the whole workflow of Next.js works and then
you need to call the correct functions where it makes sense. It is a bit of
a trial and error approach 🙃
—
Reply to this email directly, view it on GitHub
<#580 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJQOHJKK6SVORWNOTHSK2Z3XFPNMLANCNFSM6AAAAAAUHD3FLI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
I am the colleague @AngaBlue mentioned. This overview might not be the best way to do this, and there may be some inconsistencies, but it does work. This guide is for route-based locale, rather than subdomain-based locale. You could probably get it working using subdomain-based, but it would need some adaption. The first problem is that Next.js App router has either removed support for native locale navigation, or hasn't implemented it yet (its not clear from the docs whether they intend to do so in future), so we have to implement it ourselves. The Next.js documentation on this is actually not bad, (https://nextjs.org/docs/app/building-your-application/routing/internationalization), so we can roughly follow that. Basically, it boils down to putting all your pages inside a dynamic route, like so (I've called ours lang, but it can be called whatever you want): This allows you to catch and handle the locale from the path as part of the params on each page. Next, we need to ensure that users always have a locale prefix in their path, otherwise it might result in some parsing errors. We can do this with middleware redirects. Put a
Basically, that code checks whether the path already has a relevant locale in it, and if not, attempts to recognize the user's language selection from cookies, falls back to headers, and then falls back to the baseLocale. The most confusing part is the regex matcher. We want to avoid locale-ing any internal next pages (_next/...), and any static exports (usually the public/ folder). In our case, we found that all our static exports had a file extension, while all our locale'd pages did not, so we could filter by the inclusion of a '.' in the filename. If that is not the case for you, you will need to change the regex. The final part of the regex filters out known locale paths. We do this because otherwise, the middleware will run any time a user visits any page. That can get very expensive, so we filter out paths that already have been redirected. Unfortunately, the matcher string must be a pure literal, meaning you cannot generate it based on you i18n files. Therefore, every time you add a locale, you will have to manually add the locale to the string. The one in the example is set up for a site with only English and Spanish translations Next, we need to fix 404 pages. By default, if a route cannot be found, Next will render the root The code in here will simply be:
This will force invalid routes to be greeted by the Finally, we need to implement a wrapper for
Now we have reimplemented the old Next.js internationalised routing can actually get started on loading the locale into the client. We can grab our locale using the new server components, however, to pass the information to other components we need a Context Provider, which cannot be rendered in a server component. The simple solution is to fetch the translation in a server component, pass it as a prop to a child client component, and then render the context provider there.
Now we can use the All of this has been dynamically loaded, but if you need static generation of all your locales, you can do that by adding a
The final thing to cover is generating translated titles and other metadata. The app router now uses either a metadata export, for static metadata, or generateMetadata function, for dynamic metadata. We need the generateMetadata since it is dynamically generated. Since this is serverside, we can load the locales as usual and use them directly.
In hindsight, I think that might break parameterised strings, but we don't use any for our page titles, so it hasn't been a problem. If you need that, you'll probably need to figure it out yourself. |
Beta Was this translation helpful? Give feedback.
-
How to change the default language? I have changed baseLocale to "mm". |
Beta Was this translation helpful? Give feedback.
I am the colleague @AngaBlue mentioned. This overview might not be the best way to do this, and there may be some inconsistencies, but it does work.
This guide is for route-based locale, rather than subdomain-based locale. You could probably get it working using subdomain-based, but it would need some adaption.
The first problem is that Next.js App router has either removed support for native locale navigation, or hasn't implemented it yet (its not clear from the docs whether they intend to do so in future), so we have to implement it ourselves. The Next.js documentation on this is actually not bad, (https://nextjs.org/docs/app/building-your-application/routing/internationalization), so w…