Skip to content

Commit

Permalink
✨ Add extract dictionary.
Browse files Browse the repository at this point in the history
  • Loading branch information
zswang committed Jun 16, 2017
1 parent ac4f89d commit 558c024
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
40 changes: 37 additions & 3 deletions lib/gulp-h5i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* A mobile page of internationalization development framework
* @author
* zswang (http://weibo.com/zswang)
* @version 0.0.5
* @date 2017-05-22
* @version 0.0.6
* @date 2017-06-16
*/
var h5i18n = require('h5i18n');
var through = require('through2');
Expand Down Expand Up @@ -49,7 +49,41 @@ module.exports = function (options) {
map = JSON.parse(fs.readFileSync(options.mapfile));
}
langs.dictionary(map);
var contents = langs.replace(file.contents, options.locale);
var contents = '';
if (options.extract) {
var lines = [];
langs.replace(file.contents, options.locale, function (type, text) {
var expr = langs.parse(text, 'cn');
/* istanbul ignore else */
if (expr) {
var line = ' - type: ' + type + '\n';
line += ' lang:\n'
Object.keys(expr.optionsLang).forEach(function (lang) {
var text = expr.optionsLang[lang].trim();
if (/["\n]/.test(text)) {
text = JSON.stringify(text);
}
if (/[*\n]/.test(lang)) {
lang = JSON.stringify(lang);
}
line += ' ' + lang + ': ' + text + '\n';
});
lines.push(line);
}
});
if (lines.length) {
var filename;
/* istanbul ignore else */
if (process.env.PWD) {
filename = path.relative(process.env.PWD, file.path);
} else {
filename = file.relative;
}
contents = '-file: ' + filename + '\n i18n:\n' + lines.join('');
}
} else {
contents = langs.replace(file.contents, options.locale);
}
file.contents = new Buffer(contents);
}
return callback(null, file);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-h5i18n",
"version": "0.0.5",
"version": "0.0.6",
"description": "A mobile page of internationalization development framework",
"main": "lib/gulp-h5i18n.js",
"scripts": {
Expand Down Expand Up @@ -33,7 +33,7 @@
},
"dependencies": {
"gulp-util": "^3.0.8",
"h5i18n": "^0.6.0",
"h5i18n": "^0.6.8",
"through2": "^2.0.3"
},
"files": [
Expand Down
36 changes: 35 additions & 1 deletion src/gulp-h5i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,41 @@ module.exports = function (options) {
}
langs.dictionary(map);

var contents = langs.replace(file.contents, options.locale);
var contents = '';
if (options.extract) {
var lines = [];
langs.replace(file.contents, options.locale, function (type, text) {
var expr = langs.parse(text, 'cn');
/* istanbul ignore else */
if (expr) {
var line = ' - type: ' + type + '\n';
line += ' lang:\n'
Object.keys(expr.optionsLang).forEach(function (lang) {
var text = expr.optionsLang[lang].trim();
if (/["\n]/.test(text)) {
text = JSON.stringify(text);
}
if (/[*\n]/.test(lang)) {
lang = JSON.stringify(lang);
}
line += ' ' + lang + ': ' + text + '\n';
});
lines.push(line);
}
});
if (lines.length) {
var filename;
/* istanbul ignore else */
if (process.env.PWD) {
filename = path.relative(process.env.PWD, file.path);
} else {
filename = file.relative;
}
contents = '-file: ' + filename + '\n i18n:\n' + lines.join('');
}
} else {
contents = langs.replace(file.contents, options.locale);
}
file.contents = new Buffer(contents);
}
return callback(null, file);
Expand Down
34 changes: 34 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,40 @@ describe('fixtures', function () {

});

describe('extract', function () {
it('case 1', function (done) {
var input = 'console.log(languages.get("中文<!--{*}100--><!--{en}English-->"))';
var output = '-file: testfile.js\n i18n:\n - type: code\n lang:\n "*": 100\n en: English\n cn: 中文\n';
expect_equals({
locale: 'en',
extract: true,
}, input, output, done);
});

it('case 2', function (done) {
var input = '<div>中文"<!--{en}English--></div>';
var output = '-file: ../testfile.js\n i18n:\n - type: element\n lang:\n en: English\n cn: \"中文\\\"\"\n';
process.env.PWD = null;
expect_equals({
locale: 'jp',
extract: true,
map: {
click: '<!--{jp}クリック-->'
}
}, input, output, done);
});

it('case 3', function (done) {
var input = '<div></div>';
var output = '';
expect_equals({
locale: 'jp',
extract: true,
}, input, output, done);
});

});

describe('null', function () {
it('does nothing', function (done) {
var file = new gutil.File({
Expand Down

0 comments on commit 558c024

Please sign in to comment.