diff --git a/lib/gulp-h5i18n.js b/lib/gulp-h5i18n.js index ef537a9..440d11e 100644 --- a/lib/gulp-h5i18n.js +++ b/lib/gulp-h5i18n.js @@ -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'); @@ -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); diff --git a/package.json b/package.json index afae9b6..e41e495 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -33,7 +33,7 @@ }, "dependencies": { "gulp-util": "^3.0.8", - "h5i18n": "^0.6.0", + "h5i18n": "^0.6.8", "through2": "^2.0.3" }, "files": [ diff --git a/src/gulp-h5i18n.js b/src/gulp-h5i18n.js index c638774..310aa52 100644 --- a/src/gulp-h5i18n.js +++ b/src/gulp-h5i18n.js @@ -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); diff --git a/test/test.js b/test/test.js index b0d97bf..b9ae97f 100644 --- a/test/test.js +++ b/test/test.js @@ -76,6 +76,40 @@ describe('fixtures', function () { }); +describe('extract', function () { + it('case 1', function (done) { + var input = 'console.log(languages.get("中文"))'; + 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 = '
中文"
'; + 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: '' + } + }, input, output, done); + }); + + it('case 3', function (done) { + var input = '
'; + var output = ''; + expect_equals({ + locale: 'jp', + extract: true, + }, input, output, done); + }); + +}); + describe('null', function () { it('does nothing', function (done) { var file = new gutil.File({