Replies: 2 comments 10 replies
-
For now I am doing import "server-only";
import { createIntl, createIntlCache } from "@formatjs/intl";
const intlMessages = {
en: () => import("@/content/locales/en.json").then((m) => m.default),
};
export type Intls = keyof typeof intlMessages;
function getMessages(locale: string) {
if (locale in intlMessages) {
return intlMessages[locale as Intls]();
}
return intlMessages.en();
}
export async function getIntl() {
const locale = "en"; // FIXME: take from headers or cookies or URL
const cache = createIntlCache();
const messages = await getMessages(locale);
return createIntl({ locale, messages }, cache);
} and then in components I call it export default async function MyPage() {
const intl = await getIntl();
return (
<Link href="/pages/formatting">
{intl.formatMessage(
{ defaultMessage: "Fancy <b>formatting</b>" },
{
b: (text) => <strong>{text}</strong>,
},
)}
</Link>
);
} this works perfectly 👍 you can then pass the |
Beta Was this translation helpful? Give feedback.
3 replies
-
It would be great if we could have defineMessages to run on serverside too..
and call it in the component like this:
This way it would be great to isolate the messages per component and easier to ensure we're not repeating ids... Honestly.. I think I'll have to move to next-intl :/ |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Next.js 13's
/app
directory (docs) uses React Server Components. Server components don't have access to Context, which meansuseIntl
,FormattedMessage
and anything else relying on Context won't work. Do you have any plans for APIs that work in server components in the future?Beta Was this translation helpful? Give feedback.
All reactions