i18next / i18next-browser-languageDetector

language detector used in browser environment for i18next

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to disable getBestMatchFromCodes?

yoroshikun opened this issue Β· comments

πŸ› Bug Report

When consuming this library to detect language (we currently use path) it will auto extrapolate the language code very eagerly. We need a way to disable that. Unless we are missing something in the config code.

Related line.

if (this.services.languageUtils.getBestMatchFromCodes) return detected; // new i18next v19.5.0

To Reproduce

With the config set to below

  supportedLngs: ['ja', 'en', ...],
  ...
          detection: {
            order: ['path'],
          },

navigating to {base}/e will infer en <- incorrect assumption
navigating to {base}/c will infer cimode <- not supported at all
and so on.

Expected behavior

No assumption to be made on the intended language, expecially in path mode.

navigating to {base}/e will not infer en falling back to the default language set
same for c etc.

Your Environment

  • runtime version: i.e. node 20.10
  • i18next version: i.e. 21.10.0
  • os: Mac,
  • any other relevant information

try i18next v23.10.1 and set the fallbackLng option to your desired fallback language (ja or en)

if this does not help, you can add your own language detector (https://github.com/i18next/i18next-browser-languageDetector?tab=readme-ov-file#adding-own-detection-functionality) that validates for correct language codes...
Or provide a PR with an optional option for to do this language code validation here: https://github.com/i18next/i18next-browser-languageDetector/blob/master/src/index.js#L76

Thank you so much for this change, I have confirmed that this correctly defaults to the fallback lang instead of inferring non supported languages