i18next-sync-fs-backend
Advanced tools
Comparing version 0.1.0 to 1.0.0
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: | ||
[](https://travis-ci.org/sallar/i18next-sync-fs-backend) | ||
[](https://codecov.io/gh/sallar/i18next-sync-fs-backend) | ||
[](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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
14147
9
223
0
95
3
14
3
1
+ Addedcodecov@^2.2.0
+ Addedjs-yaml@3.5.4
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedargparse@1.0.10(transitive)
+ Addedargv@0.0.2(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@0.2.01.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.6.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedboom@2.10.1(transitive)
+ Addedcaseless@0.11.0(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedcodecov@2.3.1(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcommander@2.20.3(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addedcryptiles@2.0.5(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedesprima@2.7.3(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.1.4(transitive)
+ Addedgenerate-function@2.3.1(transitive)
+ Addedgenerate-object-property@1.2.0(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedhar-validator@2.0.6(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedhawk@3.1.3(transitive)
+ Addedhoek@2.16.3(transitive)
+ Addedhttp-signature@1.1.1(transitive)
+ Addedis-my-ip-valid@1.0.1(transitive)
+ Addedis-my-json-valid@2.20.6(transitive)
+ Addedis-property@1.0.2(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjs-yaml@3.5.4(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjson5@0.5.0(transitive)
+ Addedjsonpointer@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addednode-uuid@1.4.8(transitive)
+ Addedoauth-sign@0.8.2(transitive)
+ Addedpinkie@2.0.4(transitive)
+ Addedpinkie-promise@2.0.1(transitive)
+ Addedpunycode@1.4.1(transitive)
+ Addedqs@6.3.3(transitive)
+ Addedrequest@2.77.0(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsntp@1.0.9(transitive)
+ Addedsprintf-js@1.0.3(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedstringstream@0.0.6(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)
+ Addedtough-cookie@2.3.4(transitive)
+ Addedtunnel-agent@0.4.3(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addedurlgrey@0.4.4(transitive)
+ Addedverror@1.10.0(transitive)
+ Addedxtend@4.0.2(transitive)
- Removedjson5@0.4.0(transitive)
Updatedjson5@0.5.0