From 79cbbf257e7a50dcabaec32ba5b8dae6fc66fef5 Mon Sep 17 00:00:00 2001 From: Alexander Kellett Date: Fri, 11 Dec 2009 16:22:50 +0100 Subject: [PATCH] This commit adds the ability to do parameterizable translations. Pure.js's jquery support can be used to perform the actual parameterization. json translation file: { params_example: "first: %[param1], second: %[param2]" } html:
1st: First, 2nd: Second
pure.js directive: "div.params span.first": "param_pure_1", "div.params span.second": "param_pure_2", --- src/jquery.localize.js | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/jquery.localize.js b/src/jquery.localize.js index 77b1c04..a0a01c0 100644 --- a/src/jquery.localize.js +++ b/src/jquery.localize.js @@ -46,16 +46,33 @@ function defaultCallback(data) { $.localize.data[pkg] = data; - var keys, value; + var value; $wrappedSet.each(function(){ - elem = $(this); - key = elem.attr("rel").match(/localize\[(.*?)\]/)[1]; - value = valueForKey(key, data); + var elem = $(this); + var key = elem.attr("rel").match(/localize\[(.*?)\]/)[1]; + var params = {}; + elem.children("[rel*=params]").each(function(i, e) { + var myKey = $j(e).attr("rel").match(/params\[(.*?)\]/)[1]; + params[myKey] = $j(e); + }); + value = valueForKey(key, data) || "(missing)"; if (elem.attr('tagName') == "INPUT") { elem.val(value); } else { - elem.text(value); + elem.text(""); + var tokenizer = new $j.tokenizer([/%\[(\w+)\]/], function (src, real, re) { + var name = src.replace(re, function(all, name) { return name; }); + return real ? [true, name] : [false, src]; + }); + var tokens = tokenizer.parse(value); + $j.each(tokens, function(i, e) { + if (e[0]) { + elem.append(params[e[1]].clone()); + } else { + elem.append(e[1]); + } + }); } }); }