Giter VIP home page Giter VIP logo

i18next-resource-store-loader's Introduction

i18next-resource-store-loader

npm install i18next-resource-store-loader

This loader generates the resStore config needed for i18next to avoid loading language resources via extra HTTP requests. It generates this config given a directory.

Usage

In this example we will assume the following file structure:

└── app
    └── src
        ├── assets
        │   └── i18n
        │       ├── index.js
        │       ├── de
        │       │   └── translation.json
        │       └── en
        │           └── translation.json
        └── js
            └── main.js

Use the loader in the following way:

// File: main.js
var i18n = require("i18next");
var resBundle = require(
  "i18next-resource-store-loader!../assets/i18n/index.js"
);

i18n.init({
  resources: resBundle
});

// Use the resources as documented on i18next.com
i18n.t("key");

For older versions of i18n < 2.X, use the old property names while setting up i18n

// File: main.js
var i18n = require("i18next-client");
 var resBundle = require(
   "i18next-resource-store-loader!../assets/i18n/index.js"
 );
 		  
 i18n.init({
  resStore: resBundle
 });
 		  
// Use the resources as documented on i18next.com	
// e.g. 'translation' namespace
i18n.t("translation:key");		

And you're done! The index.js can be empty, it's just needed to point the loader to the root directory of the locales.

Advanced Usage

Filter

You can filter files in your file structure using include and exclude parameters:

// will only load files with json extension
var resBundle = require("i18next-resource-store-loader" +
                        "?include=\\.json$!../assets/i18n/index.js");
// will skip files with json extension
var resBundle = require("i18next-resource-store-loader" +
                        "?exclude=\\.json$!../assets/i18n/index.js");

Override

In cases of customized applications it may be handy to have an easy way to replace particular parts of the res store bundle with customized values. The loader supports that by two query parameters.

// will replace everyhting from base with what is existent in override
var resBundle = require("i18next-resource-store-loader" +
                        "?overrideDir=override&baseDir=base!../assets/i18n/index.js");

This configures the loader to work on a file structure like the following:

└── app
    └── src
        ├── assets
        │   └── i18n
        │       ├── index.js
        │       ├── base
        │       │   ├── de
        │       │   │   └── translation.json
        │       │   └── en
        │       │       └── translation.json
        │       └── override		
        │           └── en
        │               └── translation.json
        │
        └── js
            └── main.js

Everthing from base/en/translation.js will be overridden with stuff noted in override/en/translation.js - partial overrides are possible.

i18next-resource-store-loader's People

Contributors

de-robat avatar gary-mycase avatar morinted avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.