Skip to content

Commit

Permalink
add possibility to pass resources directly via config and set localeP…
Browse files Browse the repository at this point in the history
…ath to null
  • Loading branch information
adrai committed Jan 4, 2024
1 parent 5a0fe99 commit 301bd08
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` | |
Expand All @@ -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.
</br>
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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 11 additions & 5 deletions src/config/createConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
)
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type UserConfig = {
namespace: string,
missing: boolean
) => string)
| null
localeStructure?: string
onPreInitI18next?: (i18n: I18n) => void
reloadOnPrerender?: boolean
Expand Down

0 comments on commit 301bd08

Please sign in to comment.