question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error if folder doesn't exist when using `fallbackLng`

See original GitHub issue

🐛 Bug Report

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:

oops

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:closed
  • Created a year ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
adraicommented, Sep 4, 2022

should be better with v12.0.1

2reactions
adraicommented, Sep 4, 2022

uups… should be fixed now

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found