Code Monkey home page Code Monkey logo

i18next-sync-fs-backend's Introduction

⚠️ DEPRECATED.

Use the official backend instead

i18next-sync-fs-backend

Travis CodeCov npm version

This is a fork of the official i18next fs backend to be used node.js. It will load resources synchronously from filesystem. Right now it supports following filetypes:

  • .json
  • .json5
  • .yml

⚠️ This is a fork of the official fs backend and works syncronously.

✨ Thanks to @arve0 for transferring the Github repo to me. His old code is available in legacy branch.

Getting started

Source can be loaded via npm.

$ npm install i18next-sync-fs-backend

Wiring up:

import i18next from 'i18next';
import Backend from 'i18next-sync-fs-backend';

i18next
  .use(Backend)
  .init({
    // This is necessary for this sync version
    // of the backend to work:
    initImmediate: false,
    // ...i18next options
  });

// i18next is immediately ready:
console.log(i18next.t('someKey'));

As with all modules you can either pass the constructor function (class) to the i18next.use or a concrete instance.

Backend Options

{
  // path where resources get loaded from
  loadPath: '/locales/{{lng}}/{{ns}}.json',

  // path to post missing resources
  addPath: '/locales/{{lng}}/{{ns}}.missing.json',

  // jsonIndent to use when storing json files
  jsonIndent: 2
}

hint {{lng}}, {{ns}} use the same prefix, suffix you define in interpolation for translations!!!

Options can be passed in:

preferred - by setting options.backend in i18next.init:

import i18next from 'i18next';
import Backend from 'i18next-sync-fs-backend';

i18next
  .use(Backend)
  .init({
    initImmediate: false,
    backend: {
      // Backend options here...
    }
  });

on construction:

import Backend from 'i18next-sync-fs-backend';
const backend = new Backend(null, options);

by calling init:

import Backend from 'i18next-sync-fs-backend';
const backend = new Backend();
backend.init(options);

i18next-sync-fs-backend's People

Contributors

arve0 avatar greenkeeperio-bot avatar hoangsetup avatar jamuhl avatar manuelbieh avatar sallar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

i18next-sync-fs-backend's Issues

Error while writing missing keys

On this piece of code shouldn't be checked if the parent folder structure exists befores calling writeFileSync?

I am getting an ENOENT error at this line and I imagine that this is the cause. (in my case I can't create the directory structure manually)

TypeError: _fs2.default.readFileSync is not a function at readFile

I get the error TypeError: _fs2.default.readFileSync is not a function at readFile when I add the i18next-sync-fs-backend configuration to i18next. The versions are as folllows.

"i18next": "^7.1.2",
"i18next-browser-languagedetector": "^1.0.1",
"i18next-sync-fs-backend": "^0.1.0",

The i18next configuration is as follows.

i18next
   .use(Backend)
   .use(LngDetector)
   .init({
       initImmediate: false,
       detection: {
           order: ['cookie', 'navigator'],
           lookupCookie: 'lng',
       },
       fallbackLng: 'en',
       ns: 'properties',
       backend: {
           loadPath: 'locales/{{lng}}/{{ns}}.json',
           addPath: 'locales/{{lng}}/{{ns}}.missing.json',
           jsonIndent: 2
       }
   }, (err, t) => {
       // initialized and ready to go!
       this.setState({t: t});
   });

The stack trace is as follows.

(anonymous function) (I18nStore.js:40)
(anonymous function) (i18next.js:195)
done (i18next.js:285)
(anonymous function) (i18next.js:306)
(anonymous function) (BackendConnector.js:144)
loaded (BackendConnector.js:136)
(anonymous function) (BackendConnector.js:230)
(anonymous function) (BackendConnector.js:170)
(anonymous function) (index.js:89)
readFile (index.js:46)
read (index.js:88)
read (BackendConnector.js:163)
readOne (BackendConnector.js:226)
(anonymous function) (BackendConnector.js:237)
load (BackendConnector.js:236)
(anonymous function) (i18next.js:235)
load (CacheConnector.js:56)
loadResources (i18next.js:234)
changeLanguage (i18next.js:305)
load (i18next.js:190)
init (i18next.js:200)
initI18next (I18nStore.js:25)
init (I18nStore.js:18)
(anonymous function) (mixer.js:32)
updated.init (mixer.js:31)
Store (createStore.js:48)
module.exports (createStore.js:60)
479.i18next (I18nStore.js:14)
s (_prelude.js:1)
(anonymous function) (_prelude.js:1)
459.../actions/AssignmentActions.js (MainScreen.react.jsx:41)
s (_prelude.js:1)
(anonymous function) (_prelude.js:1)
428../components-mm/Spinner.react.jsx (app.jsx:5)
s (_prelude.js:1)
e (_prelude.js:1)
(anonymous function) (_prelude.js:1)

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.