Usage in non-next.js environment #2309
Replies: 4 comments
|
For example, my base config factory that is shared between 2 different NextJs apps looks like this (so I don't fetch admin translations for public website): |
|
Hi what you are looking for is It's the core library next-intl uses, you can leverage it to add translations to any react/node app. https://next-intl.dev/docs/environments/core-library What you probably need is the This is a snippet from one of my node apps. import {
createTranslator,
type Locale,
type Messages,
type NamespaceKeys,
type NestedKeyOf,
} from "use-intl/core";
import type { _Translator as Translator } from "use-intl/core";
import en from "@/messages/en.json";
const messages = {
"en-US": en,
};
export function getTranslator<T extends Namespace>(
locale: Locale,
namespace?: T,
): Translator<Messages, T> {
const t = createTranslator({
locale: locale,
messages: messages[locale as keyof typeof messages],
namespace: namespace,
});
return t;
}
Then you can just use getTranslator like this: const t = getTranslator(locale, "Translations.something")Hope this provides a solution to your problem! |
|
Thanks @tomtien ! while this is a great solution, i have some shared code that relies on calling
It actually worked great on node app. but package.json change broke NextJs app build... Thank you! |
|
Yep, the low-level I'll move this to a discussion since it's a usage question. |
Uh oh!
There was an error while loading. Please reload this page.
Is your feature request related to a problem? Please describe.
My application consists of NextJs web apps and a few Node.js workers and I need to translate some values in a worker (for example, sending email with few values translated). These values already exist in i18n files, and my incoming values are i18n keys, that i need to get translation for
Describe the solution you'd like
Add a new override for
getTranslationsto accept configDescribe alternatives you've considered
Manually loading translation files - not great DX
All reactions