Possible to use multiple message files per one locale? #357
-
Looking at the Messages section of the docs, it's not quite clear if it's possible to use multiple message files per one locale. What I'm wondering is if it's possible to do something like this:
Would this require additional middleware customization? And I'm not sure how that would work with TypeScript and passing namespaces to Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 26 replies
-
I'm currently working on a major docs update and have added a section that should hopefully answer this: Messages configuration ("How can I split my messages into multiple files?"). Feel free to have a look! If you have any feedback, please let me know in #351 |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Below I explain the way I found in the json structure in a Type safe way
// i18n.ts
import { getRequestConfig } from 'next-intl/server'
export default getRequestConfig(async ({ locale }) => {
const loginMessageImport = await import(`./messages/login/${locale}.json`);
const loginMessages = loginMessageImport.default;
const profileMessageImport = await import(`./messages/profile/${locale}.json`);
const profileMessages = profileMessageImport.default;
//Importing other language files ..
// Combine all messages into a single object
const messages = {
...loginMessages,
....profileMessages,
// Add messages from other language files here
};
// Return merged messages
return messages;
}); // global.d.ts
type LoginMessages = typeof import("./messages/login/en.json");
type ProfileMessages = typeof import("./messages/profile/en.json");
// Importing other language files ..
// Create a new type by combining all message types
type Messages = LoginMessages & ProfileMessages;
declare interface IntlMessages extends Messages {} |
Beta Was this translation helpful? Give feedback.
-
Hi @amannn , thanks for the great work. |
Beta Was this translation helpful? Give feedback.
-
I have created this config in the pages router for a more dynamic approach Function that loads the config // i18n.ts
const translationsConfig = [
{
page: "index",
files: ["index", "pricing"],
},
];
export const loadLangFiles = async ({ page = "index", locale = "en" }) => {
const translationFilesNames = translationsConfig.find(
(item) => item.page === "index",
)?.files;
if (!translationFilesNames)
throw new Error(`Translation configuration for ${page} does not exist!`);
const translationObjects = await Promise.all(
translationFilesNames.map((file) =>
loadLangFile({
file,
locale,
}),
),
);
return Object.assign({}, ...translationObjects) as IntlMessages;
};
export const loadLangFile = async ({ file = "index", locale = "en" }) => {
return await import(`../messages/${locale}/${file}.json`);
}; Use in your page like this: // index.ts
export async function getStaticProps(context: GetStaticPropsContext) {
return {
props: {
messages: await loadLangFiles({}),
},
};
} TypeSafety: // global.d.ts
type IndexMessages = typeof import("../messages/en/index.json");
type PricingMessages = typeof import("../messages/en/pricing.json");
type Messages = IndexMessages & PricingMessages;
declare interface IntlMessages extends Messages {} |
Beta Was this translation helpful? Give feedback.
-
You can also use |
Beta Was this translation helpful? Give feedback.
I'm currently working on a major docs update and have added a section that should hopefully answer this: Messages configuration ("How can I split my messages into multiple files?").
Feel free to have a look! If you have any feedback, please let me know in #351