i18next / i18next-browser-languageDetector

language detector used in browser environment for i18next
MIT License
873 stars 90 forks source link

Path detector isn't working properly #228

Closed ridaeh closed 4 years ago

ridaeh commented 4 years ago

🐛 Bug Report

In my order list I put only 'path' detector, unfortunately when I access mylocalhost with '/ar/' it does not work, in other hand when I access mylocalhost with '/?lng=ar' it translates the page even when I am not using querystring detector

To Reproduce

Here is my configuration :

import i18n from "i18next"
import { initReactI18next } from "react-i18next"
import { resources } from "../config/translate"
import LanguageDetector from "i18next-browser-languagedetector"

const detector = new LanguageDetector()
detector.init({
  order: ["path"],
  lookupFromPathIndex: 0,
})

i18n
  .use(detector)
  .use(initReactI18next) 
  .init({
    resources,
    fallbackLng: "en",
    supportedLngs: ["en", "ar"],
    keySeparator: false, 
    interpolation: {
      escapeValue: false, 
    },
  })

Your Environment

adrai commented 4 years ago

You are initializing it in the wrong way.

Try:

import i18n from "i18next"
import { initReactI18next } from "react-i18next"
import { resources } from "../config/translate"
import LanguageDetector from "i18next-browser-languagedetector"

const detector = new LanguageDetector()
detector.init({
  order: ["path"],
  lookupFromPathIndex: 0,
})

i18n
  .use(LanguageDetector)
  .use(initReactI18next) 
  .init({
    resources,
    fallbackLng: "en",
    supportedLngs: ["en", "ar"],
    keySeparator: false, 
    interpolation: {
      escapeValue: false, 
    },
    detection: {
      order: ["path"],
      lookupFromPathIndex: 0,
    }
  })
ridaeh commented 4 years ago

thanks @adrai for your help, I do not know why it does't work in the first time. Just to make the answer clear, we do not need detector object any more.

adrai commented 4 years ago

Yes, you don't need to instanciate LanguageDetector yourself