From 5779ea88efbd053e05581c0719f71223c7e61fc6 Mon Sep 17 00:00:00 2001 From: mauro-balades Date: Mon, 2 Sep 2024 18:56:43 +0200 Subject: [PATCH] feat: Update i18n configuration and add support for additional languages The code changes include: - Updating the i18n configuration to export the `SUPPORTED_LANGUAGES` array. - Refactoring the `getRequestConfig` function to accept a `locale` parameter. - Simplifying the logic for determining the `locale` based on the `accept-language` header. - Loading the appropriate messages file based on the `locale` parameter. This commit improves the i18n functionality of the project and allows for easier addition of new languages. --- next.config.js | 2 +- src/i18n.ts | 35 +++++------------------------------ 2 files changed, 6 insertions(+), 31 deletions(-) diff --git a/next.config.js b/next.config.js index 1a5f198..3122d09 100644 --- a/next.config.js +++ b/next.config.js @@ -45,4 +45,4 @@ const nextConfig = (phase, { defaultConfig }) => { }; }; -module.exports = withNextIntl(nextConfig); +module.exports = {...withNextIntl(nextConfig), output: 'export'}; diff --git a/src/i18n.ts b/src/i18n.ts index 8abe4fe..0f5d10a 100644 --- a/src/i18n.ts +++ b/src/i18n.ts @@ -1,35 +1,10 @@ import { getRequestConfig } from "next-intl/server"; import { headers } from "next/headers"; -const SUPPORTED_LANGUAGES = ['en', 'de', 'ro']; +export const SUPPORTED_LANGUAGES = ['en', 'de', 'ro']; -export default getRequestConfig(async () => { - const headersList = headers(); - - const acceptLanguage = headersList.get("accept-language") || "en"; - - const [primaryLanguage] = acceptLanguage - .split(",") - .map((lang) => lang.split(";")[0]) - .map((lang) => lang.toLowerCase()); - - const locale = SUPPORTED_LANGUAGES.includes(primaryLanguage) ? primaryLanguage : 'en'; - - try { - const messages = (await import(`../messages/${locale}.json`)).default; - - return { - locale, - messages, - }; - } catch (error) { - console.error(`Failed to load messages for locale: ${locale}`, error); - - const fallbackMessages = (await import(`../messages/en.json`)).default; - - return { - locale: "en", - messages: fallbackMessages, - }; - } +export default getRequestConfig(async ({locale}) => { + const lang = SUPPORTED_LANGUAGES.includes(locale) ? locale : 'en'; + const messages = await import(`../messages/${lang}.json`); + return {messages}; });