Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ni18n with LanguageDetector #178

Open
koryonik opened this issue Oct 15, 2023 · 0 comments
Open

ni18n with LanguageDetector #178

koryonik opened this issue Oct 15, 2023 · 0 comments

Comments

@koryonik
Copy link

Hi, thanks for this lib!

I’m trying to use ni18n according to the example customer-language-selection and using the LanguageDetector plugin (https://github.com/i18next/i18next-browser-languageDetector/).

Everything seems to work expect the langue detection, fallback lang is always used and stored instead the expected fr (browser lang is fr_FR) :

//ni18n.config.ts

import type { Ni18nOptions } from 'ni18n'
import LanguageDetector from 'i18next-browser-languagedetector'

export const ni18nConfig: Ni18nOptions = {
  fallbackLng: 'en',
  supportedLngs:['en', 'fr'],
  use: [LanguageDetector], //load plugin here
  ns: ['translation'],
  react: {
    useSuspense: false,
  },
}
//_app.ts

function App({ Component, pageProps }: Props) {
  //...
  const locale = typeof window !== 'undefined' 
	&& window.localStorage.getItem('i18nextLng') //same key used by the plugin
  useSyncLanguage(locale)
  //...
}

If I register the plugin and init i18n before, it’s seems to work:

//ni18n.config.ts

import i18n from 'i18next'
import type { Ni18nOptions } from 'ni18n'
import LanguageDetector from 'i18next-browser-languagedetector'

const languageDetector = new LanguageDetector(null,{})
i18n.use(languageDetector).init() //register and init

export const ni18nConfig: Ni18nOptions = {
  fallbackLng: 'en',
  //...
  //don't load plugin here
}

What I’am wrong or what’s the best way to handle this?

@koryonik koryonik changed the title ni18n with ni18n with LanguageDetector Oct 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant