1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | 1 1 1 1 1 1 1 1 1 8 8 8 8 8 8 8 8 1 7 1 1 6 6 6 6 8 8 8 8 8 8 7 8 8 8 10 1 8 8 2 2 6 6 1 8 17 9 8 1 16 34 18 27 3 31 16 1 8 8 8 1 3 3 8 1 10 8 8 1 1 1 1 1 1 1 1 1 3 2 2 1 1 2 2 2 2 4 4 4 4 8 24 24 4 4 2 1 1 1 3 3 3 3 3 3 15 1 15 1 3 3 3 3 3 1 3 3 3 3 15 3 9 3 3 3 1 | 'use strict'; var through = require('through2'); var gutil = require('gulp-util'); var PLUGIN_NAME = 'gulp-l10n'; var htmlparser = require('htmlparser2'); var crypto = require('crypto'); var glob = require('glob'); var fs = require('fs'); var gulpL10n = {}; gulpL10n.extractLocale = function(opt) { var options = opt = opt || {}; //localize the contents of all of the following elements options.elements = opt.elements || ['title', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']; //localize the contents of all of the following attributes options.attributes = opt.attributes || ['alt', 'title']; //localize the contents of all elements with the following attributes options.directives = opt.directives || ['localize']; options.algorithm = opt.hashAlgorithm || 'md5'; options.hashLength = opt.hashLength || 8; options.nativeLocale = opt.nativeLocale || 'en'; var locale = {}; function addFile(file, enc, cb){ // ignore empty files if (file.isNull()) { cb(); return; } Iif (file.isStream()) { cb(new gutil.PluginError(PLUGIN_NAME, 'Streaming not (yet) supported')); return; } Eif (file.isBuffer()) { parser.write(file.contents); } cb(); } var parser = new htmlparser.Parser(new htmlparser.DomHandler(function(error, dom){ Iif(error) { throw new gutil.PluginError(PLUGIN_NAME, error); } else { var strings = []; //extract strings from options.elements and elements with options.directives var elements = filterElementsByTagNames(dom, options.elements); elements = elements.concat(filterElementsByAttributes(dom, options.directives)); for(var i = 0; i < elements.length; i++) { strings.push(htmlparser.DomUtils.getInnerHTML(elements[i])); } //extract strings from options.attributes strings = strings.concat(extractStringsFromAttributes(dom, options.attributes)); // strings are ordered alphabetically for human readability & better source control strings.sort(); for(var j = 0; j < strings.length; j++){ locale[hash(strings[j], options.algorithm, options.hashLength)] = strings[j]; } } }, { normalizeWhitespace: true })); function createLocaleFile(cb){ parser.done(); if (Object.keys(locale).length === 0) { cb(); return; } pipeSegment.push(new gutil.File({ cwd: '', base: '', path: options.nativeLocale + '.json', contents: new Buffer(JSON.stringify(locale, null, ' ')) })); cb(); } function filterElementsByTagNames(dom, tagsArray) { var checkTags = function(elem){ if(htmlparser.DomUtils.isTag(elem)){ return tagsArray.indexOf(elem.name) !== -1; } }; return htmlparser.DomUtils.filter(checkTags, dom); } function filterElementsByAttributes(dom, attributesArray) { var checkAttrs = function(elem){ if(elem.attribs) { for(var i = 0; i < attributesArray.length; i++){ if (elem.attribs.hasOwnProperty(attributesArray[i])) { return true; } } } return false; }; return htmlparser.DomUtils.filter(checkAttrs, dom); } function extractStringsFromAttributes(dom, attributesArray) { var strings = []; var elements = filterElementsByAttributes(dom, attributesArray); for (var i = 0; i < elements.length; i++){ for (var j = 0; j < attributesArray.length; j++){ Eif (elements[i].attribs.hasOwnProperty(attributesArray[j])) { strings.push(elements[i].attribs[attributesArray[j]]); } } } return strings; } function hash(str, algorithm, length) { return crypto.createHash(algorithm).update(str).digest('hex').slice(0, length); } var pipeSegment = through.obj(addFile, createLocaleFile); return pipeSegment; }; gulpL10n.localize = function(opt, cb) { opt = opt || {}; //path of nativeLocale file Iif(!opt.hasOwnProperty('nativeLocale')){ throw new gutil.PluginError(PLUGIN_NAME, 'Please provide the path to the `nativeLocale`.'); } var nativeLocalePath = opt.nativeLocale; var nativeLocale = JSON.parse(String(fs.readFileSync(nativeLocalePath))); //glob of locales to use in localizing files Iif(!opt.hasOwnProperty('locales')){ throw new gutil.PluginError(PLUGIN_NAME, 'Please provide a `locales` glob string.'); } var localePaths = glob.sync(opt.locales); var locales = {}; for (var i = 0; i < localePaths.length; i++){ // don't add the native locale to the dictionary of locales if(localePaths[i] !== nativeLocalePath){ var localeIdentifier = localePaths[i].split('/').pop().split('.').shift(); locales[localeIdentifier] = JSON.parse(String(fs.readFileSync(localePaths[i]))); } } // strings must be exactly between delimiters in this set to be localized // (this avoids unintentional localization of unrelated strings) var potentialDelimiters = [ ['>', '<'], ['"', '"'], ['\'', '\''] ]; function localizeFile(file, enc, cb){ // ignore empty files Iif (file.isNull()) { cb(); return; } Iif (file.isStream()) { cb(new gutil.PluginError(PLUGIN_NAME, 'Streaming not (yet) supported')); return; } Eif (file.isBuffer()) { for (var localeIdentifier in locales) { // create clone of file for each locale var localizedFile = file.clone(); // place files in the locale's subdirectory localizedFile.path = localizedFile.path.replace(localizedFile.base, localizedFile.base + localeIdentifier + '/'); var contents = String(localizedFile.contents); for (var hash in nativeLocale){ for (var i = 0; i < potentialDelimiters.length; i++){ var chunks = contents.split( potentialDelimiters[i][0] + nativeLocale[hash] + potentialDelimiters[i][1]); contents = chunks.join( potentialDelimiters[i][0] + locales[localeIdentifier][hash] + potentialDelimiters[i][1]); } } localizedFile.contents = new Buffer(contents); pipeSegment.push(localizedFile); } } cb(); } var pipeSegment = through.obj(localizeFile); return pipeSegment; }; gulpL10n.simulateTranslation = function(opt) { var options = opt = opt || {}; //simulate localization to the following locales options.locales = opt.locales || ['de', 'es', 'fr']; options.dictionary = opt.dictionary || { 'a': 'á', 'e': 'é', 'i': 'í', 'o': 'ó', 'u': 'ú' }; var files = []; var regexs = {}; //key is dictionary value, value is the search regex for(var entry in options.dictionary){ //don't replace characters inside html tags (between `<` and `>`) regexs[options.dictionary[entry]] = new RegExp('(?![^<]*>)' + escapeRegExp(entry), 'g'); } function escapeRegExp(string) { return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1'); } function addFile(file, enc, cb){ // ignore empty files Iif (file.isNull()) { cb(); return; } Iif (file.isStream()) { cb(new gutil.PluginError(PLUGIN_NAME, 'Streaming not (yet) supported')); return; } Eif (file.isBuffer()) { files.push(file); } cb(); } function createLocalizations(cb){ Iif (files.length > 1) { cb(new gutil.PluginError( PLUGIN_NAME, 'Please simulateTranslation() of one nativeLocale file at a time. Consider using gulp-foreach for more control.' )); return; } var translation = JSON.parse(String(files[0].contents)); for(var hash in translation){ for(var replacement in regexs){ translation[hash] = translation[hash].replace(regexs[replacement], replacement); } } for(var i = 0; i < options.locales.length; i++){ pipeSegment.push(new gutil.File({ cwd: '', base: '', path: options.locales[i] + '.json', contents: new Buffer(JSON.stringify(translation, null, ' ')) })); } cb(); } var pipeSegment = through.obj(addFile, createLocalizations); return pipeSegment; }; module.exports = gulpL10n; |