i18next-keys-ondemand
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

npm version

I18next with ability to download missing keys

Existing i18next offers backend plugins like i18next-xhr-backend, but those plugins are requesting keys by namespaces and not at the granularity of an individual key. The goal of this module is to be able to fetch missing keys individually, taking in consideration performances by debouncing the requests in a individual request.

Installation

# using npm
$ npm install i18next-keys-ondemand

# using yarn
$ yarn add i18next-keys-ondemand

Who to use it?

  • Use the module when initializing i18next:
import * as i18n from 'i18next';
import { I18nextKeysOnDemand, TranslationMap } from 'i18next-keys-ondemand';



function translationService(keys: string[]) {
    // simulate AJAX call
    return new Promise<TranslationMap>((resolve) => {
        const result: TranslationMap = {};
        keys.map(k => { result[k] = 'translation of ' + k; });

        setTimeout(() => {
            resolve(result);
        },         50);
    });
}

i18n
  .use(new I18nextKeysOnDemand({ translationsGetter: translationService })) // init i18next here
  .init({
    fallbackLng: 'en',
    ns: ['thenamespace'],
    defaultNS: 'thenamespace'
  });

Readme

Keywords

none

Package Sidebar

Install

npm i i18next-keys-ondemand

Weekly Downloads

30

Version

0.2.0

License

MIT

Last publish

Collaborators

  • enenkey