UNPKG

granula

Version:

i18n tool for angular.js applications

128 lines (119 loc) 4.43 kB
(function() { var ATTRS_TO_TRANSLATE, ATTR_TO_IGNORE, TAGS_TO_IGNORE, granulaCtor, keys, _; granulaCtor = require('../granula/granula'); _ = require('underscore'); keys = require('../granula/keys'); ATTRS_TO_TRANSLATE = ["title", "alt", "placeholder"]; TAGS_TO_IGNORE = ["script", "link", "style"]; ATTR_TO_IGNORE = "gr-skip"; module.exports = function() { var granula, hasSomethingToTranslate; granula = granulaCtor(); hasSomethingToTranslate = function(str) { var something; if (!str) { return false; } something = false; granula.compile("en", str).apply({ string: function(ctx, str) { if (str.replace(/\s+/gi, '').length > 0) { return something = true; } }, pluralExpression: function() { return something = true; } }); return something; }; return { processHtml: function(lang, htmlDocument, options, log) { var elementIterator, getAttrs, shallProcess; options = _.defaults(options, { onlyMarked: true, textAsKey: "nokey", warnings: ["subtags"], wordsLimitForKey: 10, replaceSpaces: false, attrsToTranslate: ATTRS_TO_TRANSLATE }); shallProcess = { marked: function(element) { return htmlDocument.hasAttribute(element, "gr-key") || htmlDocument.hasAttribute(element, "gr-attrs"); }, all: function(element) { return true; } }; getAttrs = { marked: function(element) { var _ref, _ref1; return (_ref = (_ref1 = htmlDocument.getAttribute(element, "gr-attrs")) != null ? _ref1.split(",") : void 0) != null ? _ref : []; }, all: function(element) { return options.attrsToTranslate.filter(function(attr) { return htmlDocument.hasAttribute(element, attr); }); } }; elementIterator = { shallProcess: options.onlyMarked ? shallProcess.marked : shallProcess.all, getAttrs: options.onlyMarked ? getAttrs.marked : getAttrs.all, getKey: function(text, attribute, path) { var e; try { return keys.toKey(attribute, text, options); } catch (_error) { e = _error; return log.addError(e.message + (" (" + (path()))); } }, shallProcessElement: function(element) { return !_.contains(TAGS_TO_IGNORE, element.name) && !htmlDocument.hasAttribute(element, ATTR_TO_IGNORE) && this.shallProcess(element); }, process: function(element) { var attrs, html, key, text, _this = this; if (!this.shallProcessElement(element)) { return; } if (htmlDocument.hasAttribute(element, 'gr-key')) { text = htmlDocument.getText(element); html = htmlDocument.getInnerHtml(element); if (hasSomethingToTranslate(text)) { key = this.getKey(text, htmlDocument.getAttribute(element, "gr-key"), function() { return htmlDocument.getPath(element); }); this.processKey(key, html); } } attrs = this.getAttrs(element); return attrs.forEach(function(attrName) { text = htmlDocument.getAttribute(element, attrName); if (hasSomethingToTranslate(text)) { key = htmlDocument.getAttribute(element, "gr-key-" + attrName); key = _this.getKey(text, key, function() { return htmlDocument.getPath(element) + (" / @" + attrName); }); return _this.processKey(key, text); } }); }, processKey: function(key, text) { if (lang[key]) { if (lang[key] !== text) { return log.addWarning("Key '" + key + "' met again with another value: was '" + lang[key] + "', now '" + text + "'"); } } else { return lang[key] = text; } } }; return htmlDocument.forEach(function(e) { return elementIterator.process(e); }); } }; }; }).call(this);