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 | 1 1 1 1 1 1 7 7 7 7 7 1 7 7 4 3 2 1 7 7 7 7 1 7 1 1 6 1 1 5 14 9 9 9 9 1 9 9 5 7 7 1 10 10 5 10 8 10 7 10 1 9 1 22 1 1 21 1 1 20 20 20 20 1 8 2 2 2 2 6 6 6 2 2 1 2 1 1 2 2 2 2 2 2 1 8 9 9 1 4 4 3 4 4 1 8 1 1 7 1 1 6 6 6 14 6 1 3 2 2 3 4 4 | 'use strict'; var through = require('through2'); var gutil = require('gulp-util'); var s18n = require('s18n'); var PLUGIN_NAME = 'gulp-l10n'; var localeCaches = {}; module.exports = function(options) { options = options || {}; var cacheId = options.cacheId || 'default'; var hrefRewrite = options.hrefRewrite || null; var aHrefRegex = /<\s*a\s+(?:[^>]*?\s+)?href\s*=\s*"([^"]*)"|<\s*a\s+(?:[^>]*?\s+)?href\s*=\s*\'([^\']*)\'|<\s*a\s+(?:[^>]*?\s+)?href\s*=\s*([^\s>]*)/gi; var replacer = function (hrefRewriteFunction, locale){ return function(match, double, single, noquotes){ var matched; if(typeof double !== 'undefined'){ matched = double; } else if(typeof single !== 'undefined'){ matched = single; } else { matched = noquotes; } return match.replace(matched, function(href){ return hrefRewriteFunction(href, locale); }); }; }; var outPath = options.outPath || function (base, path, localeId) { return path.replace(base, base + localeId + '/'); }; function localizeFiles(file, enc, cb) { // ignore empty files if (file.isNull()) { cb(null); return; } if (file.isStream()) { cb(new gutil.PluginError(PLUGIN_NAME, 'streaming not supported')); return; } // file is buffer for (var id in localeCaches[cacheId].locales) { if (id !== localeCaches[cacheId].native) { // create clone of file for each locale var localizedFile = file.clone(); // place files in the locale's subdirectory localizedFile.path = outPath(localizedFile.base, localizedFile.path, id); var contents = s18n(String(localizedFile.contents), { nativeLocale: localeCaches[cacheId].locales[localeCaches[cacheId].native], locale: localeCaches[cacheId].locales[id] }); if(hrefRewrite){ contents = contents.replace(aHrefRegex, replacer(hrefRewrite, id)); } localizedFile.contents = new Buffer(contents); plugin.push(localizedFile); } } cb(); } var plugin = through.obj(localizeFiles); return plugin; }; module.exports.setLocales = function(options) { options = options || {}; if (typeof options.native === 'undefined') { options.native = 'en'; } if (typeof options.cacheId === 'undefined') { options.cacheId = 'default'; } if (typeof options.enforce === 'undefined') { options.enforce = 'silent'; } if (options.enforce !== 'silent' && options.enforce !== 'warn' && options.enforce !== 'strict') { throw new Error('Unrecognized `enforce` mode passed to setLocales'); // cb(new gutil.PluginError(PLUGIN_NAME, 'Unrecognized `enforce` mode passed to setLocales')); } localeCaches[options.cacheId] = { 'native': options.native, locales: {} }; function cacheLocale(file, enc, cb) { // ignore empty files if (file.isNull()) { cb(null); return; } if (file.isStream()) { cb(new gutil.PluginError(PLUGIN_NAME, 'streaming not supported')); return; } // file is buffer var localeId = file.path.split('/').pop().split('.').shift(); var locale = JSON.parse(String(file.contents)); localeCaches[options.cacheId].locales[localeId] = locale; cb(); } function enforceLocalization(cb) { if (options.enforce !== 'silent') { var sendError = false; var nativeLocaleId = localeCaches[options.cacheId].native; var nativeLocale = localeCaches[options.cacheId].locales[nativeLocaleId]; for (var thisLocaleId in localeCaches[options.cacheId].locales) { var thisLocale = localeCaches[options.cacheId].locales[thisLocaleId]; var comparison = s18n.compareLocales( nativeLocale, thisLocale ); if (comparison[1].length > 0) { var logType; if (options.enforce === 'warn') { logType = 'WARN'; } if (options.enforce === 'strict') { logType = 'ERROR'; sendError = true; } for(var i = 0; i < comparison[1].length; i++){ var out = logType + ': locale `' + thisLocaleId + '` '; out += 'is missing: `' + comparison[1][i].hash + '`, '; out += 'native string: `' + comparison[1][i].string + '`'; gutil.log(out); } } } if(sendError){ cb(new gutil.PluginError(PLUGIN_NAME, 'Locales did not meet enforcement requirements')); } } cb(); } var plugin = through.obj(cacheLocale, enforceLocalization); return plugin; }; module.exports.extract = function(options) { options = options || {}; if (typeof options.native === 'undefined') { options.native = 'en'; } var projectLocale = {}; var canOutput = false; function extractFromFile(file, enc, cb) { // ignore empty files if (file.isNull()) { cb(null); return; } if (file.isStream()) { cb(new gutil.PluginError(PLUGIN_NAME, 'streaming not supported')); return; } // file is buffer canOutput = true; var fileLocale = s18n.extract(file.contents, options); for (var hash in fileLocale) { projectLocale[hash] = fileLocale[hash]; } cb(); } function returnLocale(cb) { if (canOutput) { var projectLocaleString = s18n.formatLocale(projectLocale, { output: 'string' }); plugin.push(new gutil.File({ cwd: '', base: '', path: options.native + '.json', contents: new Buffer(projectLocaleString) })); } cb(); } var plugin = through.obj(extractFromFile, returnLocale); return plugin; }; |