Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: How to load messages from several files. #1091

Closed
wants to merge 1 commit into from

Conversation

aprezcuba24
Copy link

This PR is to add more documentations to explain, how the users can split the messages into several files.

The goal is create the file i18n.ts with this code.

import { notFound } from 'next/navigation';
import { getRequestConfig } from 'next-intl/server';
import fs from 'fs'

// Can be imported from a shared config
const locales = ['en', 'de'];

const loadLocale = async (locale: string) => {
  if (!fs.existsSync(`${process.cwd()}/messages/${locale}`)) {
    return {}
  };
  const files = fs.readdirSync(`${process.cwd()}/messages/${locale}`);
  return files.reduce(async (acc, fileName) => {
    const prev = await acc;
    return {
      ...prev,
      [fileName.replace(/\.[^/.]+$/, "")]: (await import(`@/messages/${locale}/${fileName}`)).default
    }
  }, {});
}

export default getRequestConfig(async ({ locale }) => {
  // Validate that the incoming `locale` parameter is valid
  if (!locales.includes(locale as any)) notFound();
  const messages = await loadLocale(locale);
  return {
    messages,
    timeZone: 'America/Mexico_City',
  };
});

Copy link

vercel bot commented May 25, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
next-intl-example-app-router ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 27, 2024 0:38am

Copy link

vercel bot commented May 25, 2024

@aprezcuba24 is attempting to deploy a commit to the next-intl Team on Vercel.

A member of the Team first needs to authorize it.

@amannn
Copy link
Owner

amannn commented May 27, 2024

Hey @aprezcuba24, thanks for the proposal! We do in fact already already have docs for this at https://next-intl-docs.vercel.app/docs/usage/configuration#messages-split-files.

@aprezcuba24
Copy link
Author

aprezcuba24 commented May 27, 2024

Hey @aprezcuba24, thanks for the proposal! We do in fact already already have docs for this at https://next-intl-docs.vercel.app/docs/usage/configuration#messages-split-files.

Thank you for your response. You can ignore this PR then.

In any case. I like my solution because it is more dynamic. does it make sense?

Thanks again.

@amannn
Copy link
Owner

amannn commented May 27, 2024

It really depends on what the user is looking for, the recommendation in the docs is to use a single file for all messages. I'll close this PR then, thank you anyway for sharing your solution! You can also post it as a discussion for increased visibility in case users search for such a solution!

@amannn amannn closed this May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants