-
-
Notifications
You must be signed in to change notification settings - Fork 336
Description
Messages can either be consumed in Server- or Client Components.
The former is recommended in the docs, as it has both performance benefits as well as a simpler model in general since there is only a single environment where code executes. However, for highly interactive apps, using messages on the client side is totally fine too. The situation gets a bit hairy when you need to split messages based on usage in Client Components (e.g. by page, or a mix of RSC / RCC).
Ideally, we'd provide more help with handing off translations to Client Components.
Ideas:
- User-defined namespaces
- Manually composed namespaces
- Compiler-based approach
(1) User-defined namespaces
This is already possible (and shown in the docs), but unfortunately not so maintainable. For single components it can be fine however, so when you have many small interactive leaf components, this is quite ok.
(2) Manually composed namespaces
This worked well for the Pages Router and was documented, but unfortunately doesn't work with RSC.
Apollo has a similar situation with static fragments: apollographql/apollo-client-integrations#27
A potential solution could be to use a build-time macro that is evaluated separately.
(3) Compiler-based approach
See #1 (comment). Similar to fragment hoisting in Relay, if the collection of namespaces happens at build time, we could provide this list to Server Components. This seems to be the best option.
Design-wise this requires analyzing the namespace that is passed to useTranslations. Furthermore, a nice-to-have would be to analyze which keys are passed to t (if all calls are statically known).
Current progress:
- A proof-of-concept is developed in jantimon/next-poc-i18n-split (based on https://github.com/yashvesikar/next-intl-translation-bundler-example)
- APIs are being researched in https://github.com/amannn/nextjs-compiled-messages (this is a combined experiment that also includes AOT compilation for messages)
See also: