Next.js Discord

Discord Forum

How to use i18n next with app directory?

Unanswered
Harlequin posted this in #help-forum
Open in Discord
HarlequinOP
Can any1 tell me how this works?

28 Replies

HarlequinOP
i want to have ssg content in different languages? what packages do i use for that? it is soo confusing
@averydelusionalperson https://nextjs.org/docs/app/building-your-application/routing/internationalization
Polar bear
so, i18n is only supported for server components?
otherwise we have to find some other non-nexty solution?
seems like a huge gap...
American Crow
next-intl supports server components, client components and static rendering (experimental_)
Used it multiple times can highly recommand
Polar bear
maybe i just have no idea what i'm doing then because I can't get any of this to work across all components.
doesn't seem like it should be this difficult, but i guess that's the nextjs way
everything says it's loading translations but then it never appears translated on the page
run dev now doesn't work to fast reload after changes
i have not fucked up my projects this badly in 20 years
American Crow
are you using next-intl or not?
Polar bear
yes
trying to use it :lolsob:
American Crow
i mean it's 6 steps to get started. Can't you even get it to run on a new project?
https://next-intl-docs.vercel.app/docs/getting-started/app-router
Polar bear
it's like useMessages is not seeing the locale
i've seen that page but it doesn't address client components
useTranslations throws errors unless i have a provider in the layout
but then no tranlsations load
like it doesn't see locale
messages/es.json :
{"translations":{"Legal":"Legal","Log Out":"Cerrar sesión","Menu":"Menú"}}


  const messages = (await import(`./messages/${locale}.json`)).default
  console.log(`GRC locale, read messages from ./messages/${locale}.json`, JSON.stringify(messages))


but the log says:
read messages from ./messages/es.json {"translations":{"Legal":"Legal","Log Out":"Log out","Menu":"Menu"}}
feel like i'm missing something really fundamental
how does it load the en file instead of the es file?
i hardcode es in the getRequestConfig and it loads the right file.
this works, but I don't know why the other does not.

  const locale_messages = {
    es: (await import(`./messages/es.json`)).default,
    en: (await import(`./messages/en.json`)).default,
  }
  return {
    messages: locale_messages[locale],
this works, but the relative paths are not ok
 (await import(`../../../messages/${locale}.json`, {assert:{type:"json"}})).default
Polar bear
@American Crow thanks for the assist!
Cape lion
With the introduction of nextjs app router, I have found out the implementation of i18 next too verbose/complicated(might be just me). I found integrating next-intl much simpler. I have spun up a demo as well for interested ones on github already so if it will help you here is the repo link https://github.com/dorji-dev/next-app-i18n and here is the demo site https://next-i18n-three.vercel.app