Can I fallback translations if message is missing for a language? #1061
Replies: 4 comments 1 reply
-
|
Sure, please see here: https://next-intl-docs.vercel.app/docs/usage/configuration#messages-fallback |
Beta Was this translation helpful? Give feedback.
-
|
I implemented that feature by using the custom hook, maybe it will be useful for someone too import { useTranslations as useIntlTranslations } from "next-intl";
import defaultMessages from "@/i18n/locales/en.json";
function getNestedValue(obj, path) {
return path.split(".").reduce((acc, key) => (acc && acc[key] !== undefined ? acc[key] : undefined), obj);
}
export function useTranslations() {
const t = useIntlTranslations();
return (namespace) => {
const translated = t(namespace);
return translated !== namespace ? translated : getNestedValue(defaultMessages, namespace) || namespace;
};
}Hoever, it doesn't solve the |
Beta Was this translation helpful? Give feedback.
-
|
Same issue here — using |
Beta Was this translation helpful? Give feedback.
-
|
Instead of manually deep-merging in getRequestConfig, use import { withLocaleChain } from 'next-intl-localechain';
export default withLocaleChain({
chains: {
'pt-BR': ['pt-BR', 'pt', 'en'],
'zh-Hant': ['zh-Hant', 'zh-Hans', 'zh', 'en'],
},
loadMessages: async (locale) => (await import(`../messages/${locale}.json`)).default,
});Loads all locales in the chain and deep-merges them. No more MISSING_MESSAGE errors. Install: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a
en.jsonwith:{ "name": "Lars", "age": 35 }and a
de.jsonwith this:{ "name": "Leon" }Age is missing in
de.jsonso I get:How can I make that it fallbacks to en (my default language). Is this possible?
Beta Was this translation helpful? Give feedback.
All reactions