Error if folder doesn't exist when using `fallbackLng`
See original GitHub issue🐛 Bug Report
- We run multiple domains, some of which are using the same
defaultLocale
- Next doesn’t support specifying the same
defaultLocale
for multiple domains - To get past this, we specify unique variants of languages e.g. English (
en-DE
,en-EN
) and rely onfallbackLng
to serve the correct language using the configuration below:
next-i18next.config.js
module.exports = {
i18n: {
defaultLocale: "en",
locales: ["en", "en-DE", "en-EN"],
},
fallbackLng: {
default: ["en"],
"en-DE": ["en"],
},
debug: process.env.NODE_ENV === 'development',
reloadOnPrerender: process.env.NODE_ENV === 'development',
};
next.config.js
const { i18n } = require("./next-i18next.config");
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
i18n: {
...i18n,
localeDetection: false,
domains: [
{
domain: "next-i18next.com",
defaultLocale: "en-EN",
locales: ["en-EN"],
},
{
domain: "de.next-i18next.com",
defaultLocale: "en-DE",
locales: ["en-DE"],
},
],
},
};
module.exports = nextConfig;
The above works, but only if the folder for that fallback language exists, if the folder doesn’t exist we run into this error:
It seems that we’re not checking whether the locale folder exists before we attempt to read from it:
https://github.com/i18next/next-i18next/blob/master/src/serverSideTranslations.ts#L80-L88
To Reproduce
- Checkout this repo featuring the issue: https://github.com/aaimio/next-i18next-bug
- Set up two domains in your
/etc/hosts
127.0.0.1 de.next-i18next.com
127.0.0.1 next-i18next.com
npm install
npm run dev
- In your browser, visit
de.next-i18next.com
you will get the same error
Expected behavior
We should first check whether the folder exists before reading from it.
- If default locale is specified, and its folder doesn’t exist, fallback to value in
fallbackLng
, use that language’s JSON file - If default locale is specified, and its folder exists, use that language’s JSON file
Your Environment
- Runtime version: v18.4.0
- next-i18next version: 12.0.0
- OS: Mac
Issue Analytics
- State:
- Created a year ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Fallback - i18next documentation
For production use, just set fallbackLng to an existing language. Namespace fallback.
Read more >i18next warn or lint on missing key for a language (not the ...
My assumption about your case, console log statement didn't work because the key exists in your fallback language.
Read more >fnando/i18n-js - Gitter
I'm using the i18n-js with multitenancy and at my rails application the translation file is defined by tenant name, so we have 4...
Read more >i18n Provider - Refine Dev
If you want to use other languages, follow the instructions above. ... "404": "Sorry, the page you visited does not exist.",
Read more >Set up continuous translation with Weblate and i18next
json if your lang are sorted by folders, or lang/messages-*.po … File format: Weblate can detect it automatically, but I didn't tested the ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
should be better with v12.0.1
uups… should be fixed now