From 2a0f61bc4dc34ca9539fd3a7891ef82326019704 Mon Sep 17 00:00:00 2001 From: intpp Date: Thu, 2 Feb 2023 14:23:33 +0100 Subject: [PATCH] feat(server-side): custom default config path --- README.md | 28 ++++++++++++++++++++++++++++ src/serverSideTranslations.ts | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 796cff0d..a1ba21cd 100644 --- a/README.md +++ b/README.md @@ -329,6 +329,34 @@ For example, if you want to use `{` and `}` the config would look like this: } ``` +#### Custom `next-i18next.config.js` path + +If you want to change the default config path, you can set the environment variable `I18NEXT_DEFAULT_CONFIG_PATH`. + +For example, inside the `.env` file you can set a static path: +``` +I18NEXT_DEFAULT_CONFIG_PATH=/path/to/project/apps/my-app/next-i18next.config.js +``` + +Or you can use a trick for dynamic path and set the following inside `next.config.js`: + +```js +process.env.I18NEXT_DEFAULT_CONFIG_PATH = `${__dirname}/next-i18next.config.js`; + +// ... Some other imports + +const { i18n } = require('./next-i18next.config'); + +// ... Some other code + +module.exports = { + i18n, + ... +}; +``` + +This means that the i18n configuration file will be in the same directory as `next.config.js` and it doesn't matter where your current working directory is. This helps for example for `nx` when you have monorepo and start your application from project root but the application is in `apps/{appName}`. + ## Notes ### Vercel and Netlify diff --git a/src/serverSideTranslations.ts b/src/serverSideTranslations.ts index a59e8706..b6a08a9a 100644 --- a/src/serverSideTranslations.ts +++ b/src/serverSideTranslations.ts @@ -9,7 +9,7 @@ import { globalI18n } from './appWithTranslation' import { UserConfig, SSRConfig } from './types' import { getFallbackForLng, unique } from './utils' -const DEFAULT_CONFIG_PATH = './next-i18next.config.js' +const { I18NEXT_DEFAULT_CONFIG_PATH: DEFAULT_CONFIG_PATH = './next-i18next.config.js' } = process.env export const serverSideTranslations = async ( initialLocale: string,