npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

i18next-redux-languagedetector

v1.1.0

Published

This ReduxDetector detect language in Redux using the plugin i18next-browser-languageDetector for i18next.

Downloads

1,294

Readme

Introduction

This ReduxDetector detect language in Redux using the plugin i18next-browser-languageDetector for i18next (react-i18next).

Getting started

See Example

npm install --save i18next-browser-languagedetector
npm install --save i18next-redux-languagedetector
// configureI18n
import i18n from 'i18next';
import { reactI18nextModule } from 'react-i18next';

import LanguageDetector from 'i18next-browser-languagedetector';
import ReduxDetector from 'i18next-redux-languagedetector';

import Backend from 'i18next-chained-backend';
import XHR from 'i18next-xhr-backend';


const LngDetector = new LanguageDetector();
LngDetector.addDetector(ReduxDetector);

export default function configureI18n({ redux, i18nextConfig }) {
  i18n
    .use(Backend)
    .use(LngDetector)
    .use(reactI18nextModule)
    .init({
      backend: {
        // array of existing i18next backends from https://www.i18next.com/plugins-and-utils.html#backends
        backends: [
          XHR      // primary // fallback
        ],
        // array of options in order of backends above
        backendOptions: [
          {
            loadPath: '/locales/{{lng}}/{{ns}}.json' // xhr load path for my own fallback
          }
        ]
      },

      detection: {
        // order and from where user language should be detected
        // order: ['querystring', 'cookie', 'localStorage', 'navigator', 'htmlTag', 'path', 'subdomain'],
        order: [
          'redux',
        ],

        // keys or params to lookup language from
        // lookupQuerystring: 'lng',
        // lookupCookie: 'i18next',
        // lookupLocalStorage: 'i18nextLng',
        // lookupFromPathIndex: 0,
        // lookupFromSubdomainIndex: 0,

        lookupRedux: redux.lookupRedux,
        cacheUserLanguageRedux: redux.cacheUserLanguageRedux,

        // cache user language on
        // caches: ['localStorage', 'cookie'],
        caches: ['redux'],
        excludeCacheFor: ['cimode'], // languages to not persist (cookie, localStorage)

        // optional expire and domain for set cookie
        // cookieMinutes: 10,
        // cookieDomain: 'myDomain',

        // optional htmlTag with lang attribute, the default is:
        // htmlTag: document.documentElement
      },

      whitelist: i18nextConfig.whitelist,
      fallbackLng: i18nextConfig.fallbackLng,
      // have a common namespace used around the full app
      ns: i18nextConfig.ns,
      defaultNS: i18nextConfig.defaultNS,
      debug: i18nextConfig.debug,

      interpolation: {
        escapeValue: false, // not needed for react!!
      },

      react: {
        defaultTransParent: 'div', // needed for preact
        wait: true
      }
    });

  return i18n;
};
// reducers
import { i18nextReducer } from 'i18next-redux-languagedetector';

export default {
  i18next: i18nextReducer,
}
// index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { I18nextProvider } from 'react-i18next';
import './index.css';
import configureStore from './store';
import configureI18n from './i18n';
import { languageChange } from 'i18next-redux-languagedetector';
import App from './App';
import registerServiceWorker from './registerServiceWorker';


const i18nextConfig = {
  language: null,
  whitelist: ['en', 'de', 'ru'],
  fallbackLng: 'en',
  ns: ['translations'],
  defaultNS: 'translations',
  debug: true,
}

const { store } = configureStore({
  i18next: i18nextConfig,
});

const i18n = configureI18n({
  i18nextConfig: i18nextConfig,
  redux: {
    lookupRedux: () => store.getState().i18next,
    cacheUserLanguageRedux: (language) => store.dispatch(languageChange(language))
  }
});


ReactDOM.render(
  <Provider store={store}>
    <I18nextProvider i18n={i18n}>
        <App />
    </I18nextProvider>
  </Provider>
  , document.getElementById('root')
);

registerServiceWorker();