New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

i18next-sync-fs-backend

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

i18next-sync-fs-backend - npm Package Compare versions

Comparing version 0.1.0 to 1.0.0

.travis.yml

126

lib/index.js
'use strict';
Object.defineProperty(exports, '__esModule', {
Object.defineProperty(exports, "__esModule", {
value: true

@@ -9,10 +9,6 @@ });

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var _utils = require('./utils');

@@ -30,6 +26,16 @@

var _json5 = require('json5');
var _json = require('json5');
var _json52 = _interopRequireDefault(_json5);
var _json2 = _interopRequireDefault(_json);
var _jsYaml = require('js-yaml');
var _jsYaml2 = _interopRequireDefault(_jsYaml);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function getDefaults() {

@@ -44,21 +50,51 @@ return {

function readFile(filename, callback) {
var data = undefined;
try {
data = _fs2['default'].readFileSync(filename, 'utf8');
} catch (e) {
return callback(e);
var extension = _path2.default.extname(filename);
var result = void 0;
if (/^\.(js|ts)$/.test(extension)) {
try {
var file = require(filename);
result = file.default ? file.default : file;
if ((typeof result === 'undefined' ? 'undefined' : _typeof(result)) !== 'object') {
return callback(new Error('A resource file must export an object.'));
}
callback(null, result);
} catch (err) {
callback(err);
}
} else {
var data = void 0;
try {
data = _fs2.default.readFileSync(filename, 'utf8');
} catch (err) {
return callback(err);
}
try {
data = data.replace(/^\uFEFF/, '');
switch (extension) {
case '.json5':
result = _json2.default.parse(data);
break;
case '.yml':
case '.yaml':
result = _jsYaml2.default.safeLoad(data);
break;
default:
result = JSON.parse(data);
}
} catch (err) {
err.message = 'error parsing ' + filename + ': ' + err.message;
return callback(err);
}
callback(null, result);
}
var result = undefined;
try {
result = _path2['default'].extname(filename) === '.json5' ? _json52['default'].parse(data.replace(/^\uFEFF/, '')) : JSON.parse(data.replace(/^\uFEFF/, '')); // strip byte-order mark
} catch (err) {
err.message = 'error parsing ' + filename + ': ' + err.message;
return callback(err);
}
callback(null, result);
}
var Backend = (function () {
var Backend = function () {
function Backend(services) {
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

@@ -75,4 +111,4 @@ _classCallCheck(this, Backend);

value: function init(services) {
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var coreOptions = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var coreOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};

@@ -100,10 +136,14 @@ this.services = services;

value: function create(languages, namespace, key, fallbackValue, callback) {
var _this = this,
_arguments = arguments;
var _this = this;
if (!callback) callback = function () {};
if (!callback) callback = function callback() {};
if (typeof languages === 'string') languages = [languages];
var todo = languages.length;
function done() {
if (! --todo) callback && callback();
}
languages.forEach(function (lng) {
_this.queue.apply(_this, _arguments);
_this.queue.call(_this, lng, namespace, key, fallbackValue, done);
});

@@ -116,3 +156,15 @@ }

key: 'write',
value: function write(lng, namespace) {
value: function write() {
for (var lng in this.queuedWrites) {
var namespaces = this.queuedWrites[lng];
if (lng !== 'locks') {
for (var ns in namespaces) {
this.writeFile(lng, ns);
}
}
}
}
}, {
key: 'writeFile',
value: function writeFile(lng, namespace) {
var _this2 = this;

@@ -139,3 +191,3 @@

_fs2['default'].writeFileSync(filename, JSON.stringify(resources, null, _this2.options.jsonIndent));
_fs2.default.writeFileSync(filename, JSON.stringify(resources, null, _this2.options.jsonIndent));

@@ -149,3 +201,4 @@ // unlock

_this2.debouncedWrite(lng, namespace);
// rerun
_this2.debouncedWrite();
});

@@ -159,3 +212,3 @@ }

this.debouncedWrite(lng, namespace);
this.debouncedWrite();
}

@@ -165,7 +218,6 @@ }]);

return Backend;
})();
}();
Backend.type = 'backend';
exports['default'] = Backend;
module.exports = exports['default'];
exports.default = Backend;
'use strict';
Object.defineProperty(exports, '__esModule', {
Object.defineProperty(exports, "__esModule", {
value: true

@@ -10,3 +10,2 @@ });

exports.getPath = getPath;
function debounce(func, wait, immediate) {

@@ -26,6 +25,4 @@ var timeout;

};
}
};
;
function getLastOfPath(object, path, Empty) {

@@ -53,7 +50,6 @@ function cleanKey(key) {

function setPath(object, path, newValue) {
var _getLastOfPath = getLastOfPath(object, path, Object);
var _getLastOfPath = getLastOfPath(object, path, Object),
obj = _getLastOfPath.obj,
k = _getLastOfPath.k;
var obj = _getLastOfPath.obj;
var k = _getLastOfPath.k;
obj[k] = newValue;

@@ -63,7 +59,6 @@ }

function pushPath(object, path, newValue, concat) {
var _getLastOfPath2 = getLastOfPath(object, path, Object);
var _getLastOfPath2 = getLastOfPath(object, path, Object),
obj = _getLastOfPath2.obj,
k = _getLastOfPath2.k;
var obj = _getLastOfPath2.obj;
var k = _getLastOfPath2.k;
obj[k] = obj[k] || [];

@@ -75,9 +70,8 @@ if (concat) obj[k] = obj[k].concat(newValue);

function getPath(object, path) {
var _getLastOfPath3 = getLastOfPath(object, path);
var _getLastOfPath3 = getLastOfPath(object, path),
obj = _getLastOfPath3.obj,
k = _getLastOfPath3.k;
var obj = _getLastOfPath3.obj;
var k = _getLastOfPath3.k;
if (!obj) return undefined;
return obj[k];
}
{
"name": "i18next-sync-fs-backend",
"version": "0.1.0",
"description": "i18next backend which loads resources from filesystem synchronously",
"main": "./lib/index.js",
"version": "1.0.0",
"description": "node.js backend layer for i18next using fs module to load resources synchronously from filesystem",
"main": "./index.js",
"keywords": [

@@ -10,28 +10,35 @@ "i18next",

],
"homepage": "https://github.com/arve0/i18next-sync-fs-backend",
"bugs": "https://github.com/arve0/i18next-sync-fs-backend/issues",
"homepage": "https://github.com/sallar/i18next-sync-fs-backend",
"bugs": "https://github.com/sallar/i18next-sync-fs-backend/issues",
"dependencies": {
"json5": "0.4.0"
"codecov": "^2.2.0",
"js-yaml": "3.5.4",
"json5": "0.5.0"
},
"devDependencies": {
"babel": "5.8.21",
"babel-core": "^6.3.17",
"babel-eslint": "4.1.0",
"babel-cli": "6.7.7",
"babel-core": "6.7.7",
"babel-eslint": "5.0.0",
"babel-preset-es2015": "6.3.13",
"babel-preset-stage-0": "6.5.0",
"chai": "^3.4.1",
"eslint": "1.2.1",
"gulp": "3.9.0",
"gulp-babel": "5.3.0",
"gulp-eslint": "1.0.0",
"i18next": "^2.0.4",
"mocha": "2.2.5",
"mockery": "^1.4.0"
"eslint": "2.4.0",
"i18next": "2.3.5",
"istanbul": "0.4.1",
"mocha": "2.3.4",
"mockery": "1.4.1",
"sinon": "1.17.2",
"watchify": "3.7.0",
"yargs": "4.3.2"
},
"scripts": {
"build": "gulp",
"prepublish": "npm run test",
"postpublish": "git push && git push --tags",
"test": "gulp babel eslint && mocha"
"test": "npm run build && mocha",
"transpile": "babel src -d lib",
"build": "npm run transpile",
"version": "npm run build",
"postversion": "git push && git push --tags && rm -rf build/temp",
"coverage": "NODE_ENV=test istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && codecov"
},
"author": "Arve Seljebu",
"author": "Sallar Kabioli <sallar.kaboli@gmail.com> (https://github.com/sallar)",
"license": "MIT"
}
# i18next-sync-fs-backend
i18next backend which loads resources from filesystem synchronously, in contrast to [i18next-node-fs-backend](https://github.com/i18next/i18next-node-fs-backend) which is async (this is a fork). This avoids having your code wrapped in a `i18next.init()`-callback. Supported filetypes:
[![Travis](https://img.shields.io/travis/sallar/i18next-sync-fs-backend/master.svg?style=flat-square)](https://travis-ci.org/sallar/i18next-sync-fs-backend)
[![CodeCov](https://img.shields.io/codecov/c/github/sallar/i18next-sync-fs-backend.svg?style=flat-square)](https://codecov.io/gh/sallar/i18next-sync-fs-backend)
[![npm version](https://img.shields.io/npm/v/i18next-sync-fs-backend.svg?style=flat-square)](https://www.npmjs.com/package/i18next-sync-fs-backend)
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
## Install
⚠️ **This is a fork** of the [official fs backend](https://github.com/i18next/i18next-node-fs-backend) and works syncronously.
```sh
npm install i18next-sync-fs-backend
✨ Thanks to [@arve0](https://github.com/arve0) for transferring the Github repo to me. His old code is available in `legacy` branch.
# Getting started
Source can be loaded via [npm](https://www.npmjs.com/package/i18next-sync-fs-backend).
```
$ npm install i18next-sync-fs-backend
```
## Usage
Wiring up:
```js
var i18n = require('i18next');
var Backend = require('i18next-sync-fs-backend');
import i18next from 'i18next';
import Backend from 'i18next-sync-fs-backend';
i18n
i18next
.use(Backend)
.init({}, function(err){
// handle errors
.init({
// This is necessary for this sync version
// of the backend to work:
initImmediate: false,
// ...i18next options
});
// use at once
console.log(i18n.t('key'));
// 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.
## Options
## Backend Options
```js
var i18n = require('i18next');
var Backend = require('i18next-sync-fs-backend');
{
// path where resources get loaded from
loadPath: '/locales/{{lng}}/{{ns}}.json',
i18n
// 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:
```js
import i18next from 'i18next';
import Backend from 'i18next-sync-fs-backend';
i18next
.use(Backend)
.init({
// this is the defaults
initImmediate: false,
backend: {
// 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
// Backend options here...
}
});
```
on construction:
```js
import Backend from 'i18next-sync-fs-backend';
const backend = new Backend(null, options);
```
by calling `init`:
```js
import Backend from 'i18next-sync-fs-backend';
const backend = new Backend();
backend.init(options);
```

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc