refact(es6): use es6 syntax.
* fixed #10, But iojs-2 not support Object.assign, so use object-assign shim instead for now. * use es6 syntax. * use eslint to instead jshint.
This commit is contained in:
147
index.js
147
index.js
@@ -14,43 +14,56 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var debug = require('debug')('koa-locales');
|
||||
var ini = require('ini');
|
||||
var util = require('util');
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var ms = require('humanize-ms');
|
||||
var merge = require('merge-descriptors');
|
||||
const debug = require('debug')('koa-locales');
|
||||
const ini = require('ini');
|
||||
const util = require('util');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const ms = require('humanize-ms');
|
||||
const assign = require('object-assign');
|
||||
|
||||
const DEFAULT_OPTIONS = {
|
||||
defaultLocale: 'en-US',
|
||||
queryField: 'locale',
|
||||
cookieField: 'locale',
|
||||
cookieMaxAge: '1y',
|
||||
dir: undefined,
|
||||
dirs: [path.join(process.cwd(), 'locales')],
|
||||
functionName: '__',
|
||||
};
|
||||
|
||||
module.exports = function (app, options) {
|
||||
options = options || {};
|
||||
var defaultLocale = formatLocale(options.defaultLocale || 'en-US');
|
||||
var queryField = options.queryField || 'locale';
|
||||
var cookieField = options.cookieField || 'locale';
|
||||
var cookieMaxAge = ms(options.cookieMaxAge || '1y');
|
||||
var localeDir = options.dir;
|
||||
var localeDirs = options.dirs || [path.join(process.cwd(), 'locales')];
|
||||
var functionName = options.functionName || '__';
|
||||
var resources = {};
|
||||
options = assign({}, DEFAULT_OPTIONS, options);
|
||||
const defaultLocale = formatLocale(options.defaultLocale);
|
||||
const queryField = options.queryField;
|
||||
const cookieField = options.cookieField;
|
||||
const cookieMaxAge = ms(options.cookieMaxAge);
|
||||
const localeDir = options.dir;
|
||||
const localeDirs = options.dirs;
|
||||
const functionName = options.functionName;
|
||||
const resources = {};
|
||||
|
||||
/**
|
||||
* @Deprecated Use options.dirs instead.
|
||||
*/
|
||||
if (localeDir && localeDirs.indexOf(localeDir) === -1) {
|
||||
localeDirs.push(localeDir);
|
||||
}
|
||||
|
||||
for (var i = 0; i < localeDirs.length; i ++) {
|
||||
var dir = localeDirs[i];
|
||||
for (let i = 0; i < localeDirs.length; i++) {
|
||||
const dir = localeDirs[i];
|
||||
|
||||
if (!fs.existsSync(dir)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var names = fs.readdirSync(dir);
|
||||
for (var j = 0; j < names.length; j++) {
|
||||
var name = names[j];
|
||||
var filepath = path.join(dir, name);
|
||||
const names = fs.readdirSync(dir);
|
||||
for (let j = 0; j < names.length; j++) {
|
||||
const name = names[j];
|
||||
const filepath = path.join(dir, name);
|
||||
// support en_US.js => en-US.js
|
||||
var locale = formatLocale(name.split('.')[0]);
|
||||
var resource = {};
|
||||
const locale = formatLocale(name.split('.')[0]);
|
||||
let resource = {};
|
||||
|
||||
if (name.endsWith('.js') || name.endsWith('.json')) {
|
||||
resource = flattening(require(filepath));
|
||||
@@ -59,46 +72,22 @@ module.exports = function (app, options) {
|
||||
}
|
||||
|
||||
resources[locale] = resources[locale] || {};
|
||||
merge(resources[locale], resource);
|
||||
assign(resources[locale], resource);
|
||||
}
|
||||
}
|
||||
|
||||
debug('init locales with %j, got %j resources', options, Object.keys(resources));
|
||||
|
||||
var ARRAY_INDEX_RE = /\{(\d+)\}/g;
|
||||
function formatWithArray(text, values) {
|
||||
return text.replace(ARRAY_INDEX_RE, function (orignal, matched) {
|
||||
var index = parseInt(matched);
|
||||
if (index < values.length) {
|
||||
return values[index];
|
||||
}
|
||||
// not match index, return orignal text
|
||||
return orignal;
|
||||
});
|
||||
}
|
||||
|
||||
var Object_INDEX_RE = /\{(.+?)\}/g;
|
||||
function formatWithObject(text, values) {
|
||||
return text.replace(Object_INDEX_RE, function (orignal, matched) {
|
||||
var value = values[matched];
|
||||
if (value) {
|
||||
return value;
|
||||
}
|
||||
// not match index, return orignal text
|
||||
return orignal;
|
||||
});
|
||||
}
|
||||
|
||||
app.context[functionName] = function (key, value) {
|
||||
if (arguments.length === 0) {
|
||||
// __()
|
||||
return '';
|
||||
}
|
||||
|
||||
var locale = this.__getLocale();
|
||||
var resource = resources[locale] || {};
|
||||
const locale = this.__getLocale();
|
||||
const resource = resources[locale] || {};
|
||||
|
||||
var text = resource[key] || key;
|
||||
const text = resource[key] || key;
|
||||
debug('%s: %j => %j', locale, key, text);
|
||||
if (!text) {
|
||||
return '';
|
||||
@@ -130,9 +119,9 @@ module.exports = function (app, options) {
|
||||
}
|
||||
|
||||
// __(key, value1, ...)
|
||||
var args = new Array(arguments.length);
|
||||
const args = new Array(arguments.length);
|
||||
args[0] = text;
|
||||
for(var i = 1; i < args.length; i++) {
|
||||
for(let i = 1; i < args.length; i++) {
|
||||
args[i] = arguments[i];
|
||||
}
|
||||
return util.format.apply(util, args);
|
||||
@@ -146,20 +135,20 @@ module.exports = function (app, options) {
|
||||
return this.__locale;
|
||||
}
|
||||
|
||||
var cookieLocale = this.cookies.get(cookieField);
|
||||
var locale = this.query[queryField] || cookieLocale;
|
||||
const cookieLocale = this.cookies.get(cookieField);
|
||||
let locale = this.query[queryField] || cookieLocale;
|
||||
if (!locale) {
|
||||
// Accept-Language: zh-CN,zh;q=0.5
|
||||
// Accept-Language: zh-CN
|
||||
var languages = this.acceptsLanguages();
|
||||
let languages = this.acceptsLanguages();
|
||||
if (languages) {
|
||||
if (Array.isArray(languages)) {
|
||||
if (languages[0] === '*') {
|
||||
languages = languages.slice(1);
|
||||
}
|
||||
if (languages.length > 0) {
|
||||
for (var i = 0; i < languages.length; i++) {
|
||||
var lang = formatLocale(languages[i]);
|
||||
for (let i = 0; i < languages.length; i++) {
|
||||
const lang = formatLocale(languages[i]);
|
||||
if (resources[lang]) {
|
||||
locale = lang;
|
||||
break;
|
||||
@@ -199,25 +188,49 @@ module.exports = function (app, options) {
|
||||
this.__locale = locale;
|
||||
return locale;
|
||||
};
|
||||
|
||||
function formatLocale(locale) {
|
||||
// support zh_CN, en_US => zh-CN, en-US
|
||||
return locale.replace('_', '-').toLowerCase();
|
||||
}
|
||||
};
|
||||
|
||||
function isObject(obj) {
|
||||
return Object.prototype.toString.call(obj) === '[object Object]';
|
||||
}
|
||||
|
||||
const ARRAY_INDEX_RE = /\{(\d+)\}/g;
|
||||
function formatWithArray(text, values) {
|
||||
return text.replace(ARRAY_INDEX_RE, function (orignal, matched) {
|
||||
const index = parseInt(matched);
|
||||
if (index < values.length) {
|
||||
return values[index];
|
||||
}
|
||||
// not match index, return orignal text
|
||||
return orignal;
|
||||
});
|
||||
}
|
||||
|
||||
const Object_INDEX_RE = /\{(.+?)\}/g;
|
||||
function formatWithObject(text, values) {
|
||||
return text.replace(Object_INDEX_RE, function (orignal, matched) {
|
||||
const value = values[matched];
|
||||
if (value) {
|
||||
return value;
|
||||
}
|
||||
// not match index, return orignal text
|
||||
return orignal;
|
||||
});
|
||||
}
|
||||
|
||||
function formatLocale(locale) {
|
||||
// support zh_CN, en_US => zh-CN, en-US
|
||||
return locale.replace('_', '-').toLowerCase();
|
||||
}
|
||||
|
||||
function flattening(data) {
|
||||
|
||||
var result = {};
|
||||
const result = {};
|
||||
|
||||
function deepFlat (data, keys) {
|
||||
Object.keys(data).forEach(function(key) {
|
||||
var value = data[key];
|
||||
var k = keys ? keys + '.' + key : key;
|
||||
const value = data[key];
|
||||
const k = keys ? keys + '.' + key : key;
|
||||
if (isObject(value)) {
|
||||
deepFlat(value, k);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user