diff --git a/README.md b/README.md index 06f2ed4..4442487 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,7 @@ WIP! Options src and jsonSrc may be specified according to the grunt Configuring tasks guide. - [src](#src) +- [htmlSrc](#htmlSrc) - [nullEmpty](#nullempty-v026) - [namespace](#namespace-v026) - [interpolation](#interpolation) @@ -116,10 +117,21 @@ Options src and jsonSrc may be specified according to the grunt Configuring task Type: `Array` Default: `undefined` -Example: `[ 'src/**/*.js' ]` +Example: `[ 'src/**/*.js', 'src/**/*.html' ]` Define a file list to parse for extract translation. +#### htmlSrc + +Type: `Array` +Default: `undefined` + +Example: `[ 'src/**/*.html' ]` + +Define a file list to parse with [cheerio](https://github.com/cheeriojs/cheerio). + +There **translate** and **translate-attr** directives with nested html are supported. + #### nullEmpty (v0.2.6) Type: `Boolean` diff --git a/package.json b/package.json index ccbeff0..c7ae3c3 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ ], "readmeFilename": "README.md", "dependencies": { + "cheerio": "^1.0.0-rc.2", "flat": "^1.2.0", "glob-expand": "0.1.0", "json-stable-stringify": "^1.0.0", diff --git a/src/index.js b/src/index.js index a153936..c1f376a 100644 --- a/src/index.js +++ b/src/index.js @@ -7,6 +7,7 @@ *l */ +import cheerio from 'cheerio' import fs from 'fs' import path from 'path' import _ from 'lodash' @@ -42,6 +43,7 @@ var extractor // Declare all var from configuration var files = _utils.expand(this.data.src), dest = _utils.getRealPath(this.data.dest || '.'), + htmlSrc = _utils.expand(this.data.htmlSrc || []), jsonSrc = _utils.expand(this.data.jsonSrc || []), jsonSrcName = _.union(this.data.jsonSrcName || [], ['label']), interpolation = this.data.interpolation || {startDelimiter: '{{', endDelimiter: '}}'}, @@ -276,6 +278,46 @@ var extractor }) + // Parse all files in htmlSrc with cheerio + htmlSrc.forEach((file) => { + _log.debug("Process file with cheerio: " + file) + let content = fs.readFileSync(file, { encoding: 'utf8' }), + $ = cheerio.load(content, { decodeEntities: false }), + $targets, + storeTranslationValueByKey = (k, v) => { + // Avoid empty keys + if (k === '') return + // Avoid interpolated keys + if (_.startsWith(k, interpolation.startDelimiter) && + _.endsWith(k, interpolation.endDelimiter) && + !(k.split(interpolation.endDelimiter).length - 2)) { + return + } + results[k] = (keyAsText === true) ? k : v + } + + // Find translate directive + $targets = $('[translate]') + $targets.each((i, el) => { + el = $(el) + let translationValue = el.html().trim(), + translationKey = el.attr('translate').trim() || translationValue + storeTranslationValueByKey(translationKey, translationValue) + }) + + // Find translate-attr directive + $targets = $('[translate-attr]') + $targets.each((i, el) => { + el = $(el) + let ta = eval(`(${el.attr('translate-attr')})`) + _.forEach(ta, (v, k) => { + let translationValue = (el.attr(k) || '').trim(), + translationKey = v + storeTranslationValueByKey(translationKey, translationValue) + }) + }) + }) + // Parse all extra files to extra jsonSrc.forEach(function (file) { _log.debug("Process extra file: " + file)