diff --git a/lib/compile.js b/lib/compile.js index 88be38c..166a958 100644 --- a/lib/compile.js +++ b/lib/compile.js @@ -786,6 +786,7 @@ var compile = (function(){ '__fest_jschars=/' + jschars.source + '/g,' + '__fest_jschars_test=/' + jschars.source + '/,' + '__fest_jshash=' + JSON.stringify(jshash) + ',' + + 'i18n=typeof __fest_i18n === "undefined" ? function (str) {return str;} : __fest_i18n,' + '___fest_log_error;' + (options.mode === 'function' ? 'function __fest_pushstr(_,s){__fest_buf+=s}' : '') + 'if(typeof __fest_error === "undefined"){___fest_log_error = (typeof console !== "undefined" && console.error) ? function(){return Function.prototype.apply.call(console.error, console, arguments)} : function(){};}else{___fest_log_error=__fest_error};' + diff --git a/lib/translate.js b/lib/translate.js index 80021a3..20fa5e6 100644 --- a/lib/translate.js +++ b/lib/translate.js @@ -124,6 +124,42 @@ function translate(data) { } } + function wrap(text) { + var node = stack[stack.length - 1], + parent = stack[stack.length - 2], + regex = /^data\-/, + params = {}; + + if (!node || node.ns[node.prefix] !== fest_i18n_ns) { + return escapeHTML(text); + } + + text = translate_message(new Message(text)); + text = 'i18n("' + text + '"'; + + for (var key in node.attributes) { + if (regex.test(key)) { + params[key.replace(regex, '')] = node.attributes[key].value; + } + } + + if (Object.keys(params).length) { + text += ',' + JSON.stringify(params); + } + + if (node.attributes.context) { + text += ',' + '"' + node.attributes.context.value + '"'; + } + + text += ')'; + + if (parent && ['get', 'script', 'value'].indexOf(parent.local) === -1) { + text = '' + text + ''; + } + + return text; + } + parser.onprocessinginstruction = function(instruction){ result += ''; }; @@ -178,7 +214,7 @@ function translate(data) { parser.ontext = function(text){ var xml; closetag(); - ontext(text, escapeHTML); + ontext(text, wrap); }; parser.oncdata = function(text){ diff --git a/spec/build.spec.js b/spec/build.spec.js deleted file mode 100644 index 17d1410..0000000 --- a/spec/build.spec.js +++ /dev/null @@ -1,71 +0,0 @@ -var fs = require('fs'); - - -describe('fest-build', function () { - - it('should compile directories with templates', function () { - var actualFiles = fs.readdirSync(__dirname + '/tmp/build/initial'), - expectedFiles = fs.readdirSync(__dirname + '/expected/build/initial'); - expect( - actualFiles.length - ).toBe( - expectedFiles.length - ); - expectedFiles.forEach(function (fn) { - var actual = fs.readFileSync(__dirname + '/tmp/build/initial/' + fn, 'utf8'), - expected = fs.readFileSync(__dirname + '/expected/build/initial/' + fn, 'utf8'); - expect(actual).toBe(expected); - }); - }); - - it('should translate and compile directories with templates', function () { - var actualFiles = fs.readdirSync(__dirname + '/tmp/build/translated'), - expectedFiles = fs.readdirSync(__dirname + '/expected/build/translated'); - expect( - actualFiles.length - ).toBe( - expectedFiles.length - ); - expectedFiles.forEach(function (fn) { - var actual = fs.readFileSync(__dirname + '/tmp/build/translated/' + fn, 'utf8'), - expected = fs.readFileSync(__dirname + '/expected/build/translated/' + fn, 'utf8'); - expect(actual).toBe(expected); - }); - }); - -}); - - -describe('fest-compile', function () { - - it('should compile directories with templates', function () { - var actualFiles = fs.readdirSync(__dirname + '/tmp/compile/initial'), - expectedFiles = fs.readdirSync(__dirname + '/expected/compile/initial'); - expect( - actualFiles.length - ).toBe( - expectedFiles.length - ); - expectedFiles.forEach(function (fn) { - var actual = fs.readFileSync(__dirname + '/tmp/compile/initial/' + fn, 'utf8'), - expected = fs.readFileSync(__dirname + '/expected/compile/initial/' + fn, 'utf8'); - expect(actual).toBe(expected); - }); - }); - - it('should translate and compile directories with templates', function () { - var actualFiles = fs.readdirSync(__dirname + '/tmp/compile/translated'), - expectedFiles = fs.readdirSync(__dirname + '/expected/compile/translated'); - expect( - actualFiles.length - ).toBe( - expectedFiles.length - ); - expectedFiles.forEach(function (fn) { - var actual = fs.readFileSync(__dirname + '/tmp/compile/translated/' + fn, 'utf8'), - expected = fs.readFileSync(__dirname + '/expected/compile/translated/' + fn, 'utf8'); - expect(actual).toBe(expected); - }); - }); - -}); diff --git a/spec/message.spec.js b/spec/message.spec.js index 84b224a..272a518 100644 --- a/spec/message.spec.js +++ b/spec/message.spec.js @@ -61,6 +61,23 @@ describe('fest:message', function () { ); }); + it('should support external i18n function', function () { + global.__fest_i18n = function (str) { return str + ' test'; }; + + expect( + render('templates/message-with-i18n-ns.xml', {}, { + messages: { + 'Строка': 'Line' + } + }).contents + ).toBe( + 'Line test' + ); + + delete global.__fest_i18n; + }); + + it('should allow redefine messages via events', function () { var sourceMap = { 'Логотип {json.name}': '0', diff --git a/spec/templates/message-with-i18n-ns.xml b/spec/templates/message-with-i18n-ns.xml index 70cd75a..87f533a 100644 --- a/spec/templates/message-with-i18n-ns.xml +++ b/spec/templates/message-with-i18n-ns.xml @@ -4,7 +4,8 @@ { - text: 'Строка' + text: Строка } - \ No newline at end of file + +