fix
This commit is contained in:
28
book/node_modules/i18n-t/.npmignore
generated
vendored
Normal file
28
book/node_modules/i18n-t/.npmignore
generated
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
|
||||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# Compiled binary addons (http://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
|
||||
# Dependency directory
|
||||
# Deployed apps should consider commenting this line out:
|
||||
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
|
||||
node_modules
|
||||
|
||||
# vim swapfile
|
||||
*.swp
|
4
book/node_modules/i18n-t/.travis.yml
generated
vendored
Normal file
4
book/node_modules/i18n-t/.travis.yml
generated
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "stable"
|
||||
- "0.12"
|
32
book/node_modules/i18n-t/README.md
generated
vendored
Normal file
32
book/node_modules/i18n-t/README.md
generated
vendored
Normal file
@ -0,0 +1,32 @@
|
||||
# i18n-t
|
||||
|
||||
Easy to use i18n utility. It does not contain express specific middlewares, etc.
|
||||
|
||||
[](https://travis-ci.org/SamyPesse/i18n-t)
|
||||
[](http://badge.fury.io/js/i18n-t)
|
||||
|
||||
|
||||
```js
|
||||
var I18n = require('i18n-t');
|
||||
|
||||
|
||||
var i18n = new I18n({
|
||||
defaultLocale: 'en'
|
||||
});
|
||||
|
||||
// Load locales from a directory
|
||||
i18n.load('./locales');
|
||||
|
||||
// or using a pre-loaded objects
|
||||
i18n.set({
|
||||
en: {
|
||||
HELLO: 'Hello {{name}}',
|
||||
CATS: '{{0}} cats'
|
||||
}
|
||||
});
|
||||
|
||||
// Translate sentences
|
||||
i18n.t('en', 'HELLO', { name: 'Samy' });
|
||||
i18n.t('en', 'CATS', 10);
|
||||
```
|
||||
|
125
book/node_modules/i18n-t/index.js
generated
vendored
Normal file
125
book/node_modules/i18n-t/index.js
generated
vendored
Normal file
@ -0,0 +1,125 @@
|
||||
var _ = require('lodash');
|
||||
var path = require('path');
|
||||
var fs = require('fs');
|
||||
|
||||
function I18n(opts) {
|
||||
if (!(this instanceof I18n)) return new I18n(opts);
|
||||
|
||||
this.opts = _.defaults(opts || {}, {
|
||||
defaultLocale: 'en'
|
||||
});
|
||||
|
||||
this._locales = {};
|
||||
|
||||
_.bindAll(this, _.functionsIn(this));
|
||||
}
|
||||
|
||||
// Extend locales
|
||||
I18n.prototype.set = function(_locales) {
|
||||
this._locales = _.extend(this._locales, _locales);
|
||||
};
|
||||
|
||||
// Return a locales or all
|
||||
I18n.prototype.get = function(lang) {
|
||||
if (lang) {
|
||||
lang = this.resolve(lang);
|
||||
return this._locales[lang];
|
||||
} else {
|
||||
return this._locales;
|
||||
}
|
||||
};
|
||||
|
||||
// Return list of locales
|
||||
I18n.prototype.locales = function() {
|
||||
return _.keys(this._locales);
|
||||
};
|
||||
|
||||
// Load locales from a folder
|
||||
I18n.prototype.load = function(root) {
|
||||
var locales = _.chain(fs.readdirSync(root))
|
||||
.map(function(filename) {
|
||||
var ext = path.extname(filename);
|
||||
if (ext != '.json' && ext != '.js') return;
|
||||
|
||||
var lang = path.basename(filename, ext);
|
||||
var filepath = path.resolve(root, filename);
|
||||
|
||||
return [
|
||||
lang,
|
||||
require(filepath)
|
||||
];
|
||||
})
|
||||
.compact()
|
||||
.fromPairs()
|
||||
.value();
|
||||
|
||||
this.set(locales);
|
||||
};
|
||||
|
||||
// Resolve a language to an existing locale
|
||||
I18n.prototype.resolve = function(lang, defaultLocale) {
|
||||
defaultLocale = _.isUndefined(defaultLocale)? this.opts.defaultLocale : defaultLocale;
|
||||
|
||||
return _.chain(this.locales())
|
||||
.map(function(locale) {
|
||||
return {
|
||||
locale: locale,
|
||||
score: compareLocales(lang, locale)
|
||||
};
|
||||
})
|
||||
.filter(function(lang) {
|
||||
return lang.score > 0;
|
||||
})
|
||||
.sortBy('score')
|
||||
.map('locale')
|
||||
.last()
|
||||
.value() || defaultLocale;
|
||||
};
|
||||
|
||||
// Translate a phrase
|
||||
I18n.prototype.t = function(lang, phrase) {
|
||||
var args = _.toArray(arguments).slice(2);
|
||||
var kwargs = {};
|
||||
var locale = this.get(lang);
|
||||
|
||||
if (_.isObject(_.last(args))) {
|
||||
kwargs = _.last(args);
|
||||
args = args.slice(0, -1);
|
||||
}
|
||||
|
||||
var tpl = locale[phrase];
|
||||
|
||||
if (_.isUndefined(tpl)) {
|
||||
tpl = this.get(this.opts.defaultLocale)[phrase];
|
||||
}
|
||||
|
||||
return interpolate(tpl || phrase, args, kwargs);
|
||||
};
|
||||
|
||||
// Compare two language to find the most adequate
|
||||
function compareLocales(lang, locale) {
|
||||
var langMain = _.first(lang.split('-'));
|
||||
var langSecond = _.last(lang.split('-'));
|
||||
|
||||
var localeMain = _.first(locale.split('-'));
|
||||
var localeSecond = _.last(locale.split('-'));
|
||||
|
||||
if (locale == lang) return 100;
|
||||
if (localeMain == langMain && localeSecond == localeMain) return 51;
|
||||
if (localeMain == langMain) return 50;
|
||||
if (localeSecond == langSecond) return 20;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Interpolate a template
|
||||
function interpolate(tpl, args, kwargs) {
|
||||
var value = tpl;
|
||||
kwargs = _.extend(kwargs, args);
|
||||
|
||||
return _.reduce(kwargs, function(value, val, key) {
|
||||
var re = /{{([\s\S]*?[^\$])}}/g;
|
||||
return value.replace(re, val);
|
||||
}, tpl);
|
||||
}
|
||||
|
||||
module.exports = I18n;
|
24
book/node_modules/i18n-t/package.json
generated
vendored
Normal file
24
book/node_modules/i18n-t/package.json
generated
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "i18n-t",
|
||||
"version": "1.0.1",
|
||||
"description": "Very easy to use and dumb i18n utility",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"lodash": "^4.13.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"mocha": "^2.3.3"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "export TESTING=true; mocha --reporter list --bail"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/SamyPesse/i18n-t.git"
|
||||
},
|
||||
"author": "Samy Pesse <samypesse@gmail.com>",
|
||||
"license": "Apache-2.0",
|
||||
"bugs": {
|
||||
"url": "https://github.com/SamyPesse/i18n-t/issues"
|
||||
}
|
||||
}
|
69
book/node_modules/i18n-t/test/i18n.js
generated
vendored
Normal file
69
book/node_modules/i18n-t/test/i18n.js
generated
vendored
Normal file
@ -0,0 +1,69 @@
|
||||
var path = require('path');
|
||||
var assert = require('assert');
|
||||
|
||||
var I18n = require('../');
|
||||
|
||||
describe('I18n', function() {
|
||||
var i18n = I18n();
|
||||
|
||||
describe('.load', function() {
|
||||
it('should read from directory', function() {
|
||||
i18n.load(path.resolve(__dirname, 'locales'));
|
||||
|
||||
assert.deepEqual(i18n.locales(), ['en', 'fr']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.set', function() {
|
||||
it('should extend locales', function() {
|
||||
i18n.set({
|
||||
'en-gb': {
|
||||
'HELLO': 'Hello Sir {{name}}'
|
||||
}
|
||||
});
|
||||
|
||||
assert.deepEqual(i18n.locales(), ['en', 'fr', 'en-gb']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.resolve', function() {
|
||||
it('should resolve non-existing locales', function() {
|
||||
assert.equal(i18n.resolve('en-us'), 'en');
|
||||
assert.equal(i18n.resolve('fr-ca'), 'fr');
|
||||
});
|
||||
|
||||
it('should resolve existing locales', function() {
|
||||
assert.equal(i18n.resolve('en-gb'), 'en-gb');
|
||||
assert.equal(i18n.resolve('en'), 'en');
|
||||
});
|
||||
|
||||
it('should default to en', function() {
|
||||
assert.equal(i18n.resolve('zh'), 'en');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.t', function() {
|
||||
it('should translate without args', function() {
|
||||
assert.equal(i18n.t('en', 'WORLD'), 'World');
|
||||
});
|
||||
|
||||
it('should translate with kwargs', function() {
|
||||
assert.equal(i18n.t('en', 'HELLO', { name: 'Samy' }), 'Hello Samy');
|
||||
});
|
||||
|
||||
it('should translate with args', function() {
|
||||
assert.equal(i18n.t('en', 'CATS', 10), '10 cats');
|
||||
});
|
||||
|
||||
it('should default non-existing locales to en', function() {
|
||||
assert.equal(i18n.t('zh', 'WORLD'), 'World');
|
||||
});
|
||||
|
||||
it('should default non-existing translation to en', function() {
|
||||
assert.equal(i18n.t('fr', 'CATS', 10), '10 cats');
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
5
book/node_modules/i18n-t/test/locales/en.json
generated
vendored
Normal file
5
book/node_modules/i18n-t/test/locales/en.json
generated
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"HELLO": "Hello {{name}}",
|
||||
"WORLD": "World",
|
||||
"CATS": "{{0}} cats"
|
||||
}
|
6
book/node_modules/i18n-t/test/locales/fr.js
generated
vendored
Normal file
6
book/node_modules/i18n-t/test/locales/fr.js
generated
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
|
||||
module.exports = {
|
||||
HELLO: 'Bonjour {{name}}',
|
||||
WORLD: 'Monde'
|
||||
};
|
||||
|
Reference in New Issue
Block a user