From 301bd086d8746988b5992d1816bcc227ed72c6b5 Mon Sep 17 00:00:00 2001 From: Adriano Raiano Date: Fri, 5 Jan 2024 00:14:34 +0100 Subject: [PATCH] add possibility to pass resources directly via config and set localePath to null --- CHANGELOG.md | 4 ++++ README.md | 4 +++- package.json | 2 +- src/config/createConfig.ts | 16 +++++++++++----- src/types.ts | 1 + 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07f219bd..bf2505ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 15.2.0 + +- add possibility to pass resources directly via config and set localePath to null + ## 15.1.2 - fix: Install error with react-i18next v14 [#2248](https://github.com/i18next/next-i18next/issues/2248) diff --git a/README.md b/README.md index e760936c..aa36b5bb 100644 --- a/README.md +++ b/README.md @@ -332,7 +332,7 @@ This option will reload your translations whenever `serverSideTranslations` is c | Key | Default value | Note | | ------------------- | -------------------- | -------------------------------------------------------------- | | `defaultNS` | `'common'` | | -| `localePath` | `'./public/locales'` | Can be a function, see note below. | +| `localePath` | `'./public/locales'` | Can be a function, see note below. (can also be null, if passing resources option directly via config, like [here](https://www.i18next.com/how-to/add-or-load-translations#add-on-init)) | | `localeExtension` | `'json'` | Ignored if `localePath` is a function. | | `localeStructure` | `'{{lng}}/{{ns}}'` | Ignored if `localePath` is a function. | | `reloadOnPrerender` | `false` | | @@ -345,6 +345,8 @@ This option will reload your translations whenever `serverSideTranslations` is c If the localePath is a function, make sure you also define the ns option, because on server side we're not able to preload the namespaces then. All other [i18next options](https://www.i18next.com/overview/configuration-options) and [react-i18next options](https://react.i18next.com/latest/i18next-instance) can be passed in as well. +
+You can also pass in the [`resources`](https://www.i18next.com/overview/configuration-options#languages-namespaces-resources) directly in combination with setting `localePath` to `null`. #### Custom interpolation prefix/suffix diff --git a/package.json b/package.json index 17febaf8..9726c212 100644 --- a/package.json +++ b/package.json @@ -114,7 +114,7 @@ "eslint-plugin-typescript-sort-keys": "^3.1.0", "gh-release": "7.0.2", "husky": "^8.0.3", - "i18next": "^23.7.13", + "i18next": "^23.7.16", "jest": "^29.7.0", "jest-environment-jsdom": "^29.7.0", "next": "^14.0.1", diff --git a/src/config/createConfig.ts b/src/config/createConfig.ts index 111613df..7267706e 100644 --- a/src/config/createConfig.ts +++ b/src/config/createConfig.ts @@ -186,7 +186,7 @@ export const createConfig = ( loadPath: (locale: string, namespace: string) => localePath(locale, namespace, false), } - } else { + } else if (localePath) { throw new Error( `Unsupported localePath type: ${typeof localePath}` ) @@ -222,11 +222,17 @@ export const createConfig = ( return ret } - const namespacesByLocale = locales.map(locale => - getLocaleNamespaces( - path.resolve(process.cwd(), `${localePath}/${locale}`) + let namespacesByLocale + const r = combinedConfig.resources + if (!localePath && r) { + namespacesByLocale = locales.map(locale => Object.keys(r[locale])) + } else { + namespacesByLocale = locales.map(locale => + getLocaleNamespaces( + path.resolve(process.cwd(), `${localePath}/${locale}`) + ) ) - ) + } const allNamespaces = [] for (const localNamespaces of namespacesByLocale) { diff --git a/src/types.ts b/src/types.ts index a1173e4b..8744c77b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -43,6 +43,7 @@ export type UserConfig = { namespace: string, missing: boolean ) => string) + | null localeStructure?: string onPreInitI18next?: (i18n: I18n) => void reloadOnPrerender?: boolean