Nested options #10

Merged
merged 4 commits into from May 11, 2012
View
@@ -191,7 +191,7 @@
// $.t shortcut
$.t = $.t || translate;
- function parse(ele, key) {
+ function parse(ele, key, options) {
if (key.length === 0) return;
var attr = 'text';
@@ -206,47 +206,54 @@
key = key.substr(0, key.length-2);
}
+ var optionsToUse;
if (attr === 'text') {
- ele.text($.t(key, { defaultValue: ele.text() }));
+ optionsToUse = $.extend({ defaultValue: ele.text() }, options);
+ ele.text($.t(key, optionsToUse));
} else {
- ele.attr(attr, $.t(key, { defaultValue: ele.attr(attr) }));
+ optionsToUse = $.extend({ defaultValue: ele.attr(attr) }, options);
+ ele.attr(attr, $.t(key, optionsToUse));
}
}
- function localize(ele) {
+ function localize(ele, options) {
var key = ele.attr('data-i18n');
if (!key) return;
if (key.indexOf(';') <= key.length-1) {
var keys = key.split(';');
$.each(keys, function(m, k) {
- parse(ele, k);
+ parse(ele, k, options);
});
} else {
- parse(ele, key);
+ parse(ele, key, options);
}
}
// fn
$.fn.i18n = function (options) {
return this.each(function() {
// localize element itself
- localize($(this));
+ localize($(this), options);
// localize childs
var elements = $(this).find('[data-i18n]');
elements.each(function() {
- localize($(this));
+ localize($(this), options);
});
});
};
}
- function applyReplacement(str, replacementHash) {
+ function applyReplacement(str, replacementHash, nestedKey) {
f.each(replacementHash, function(key, value) {
- str = str.replace([o.interpolationPrefix, key, o.interpolationSuffix].join(''), value);
+ if (typeof value === 'object') {
+ str = applyReplacement(str, value, key);
+ } else {
+ str = str.replace([o.interpolationPrefix, nestedKey ? nestedKey + '.' + key : key, o.interpolationSuffix].join(''), value);
+ }
});
return str;
}
View
@@ -396,6 +396,50 @@ asyncTest("extended binding (.i18n()) test", function() {
});
});
+asyncTest("basic binding (.i18n()) test with options", function() {
+ $.i18n.init({
+ lng: 'en-US',
+ lowerCaseLng: false,
+ ns: 'translation',
+ useLocalStorage: false,
+ resStore: {
+ 'en-US': { translation: { 'simpleTest': '__firstoption__' } }
+ }
+ }, function(t) {
+ // given
+ $('#qunit-fixture').append('<button id="testBtn" data-i18n="simpleTest"></button>');
+
+ // when
+ $('#qunit-fixture').i18n({"firstoption":"ok_via_options"});
+
+ // then
+ equals($('#testBtn').text(),'ok_via_options', 'set text via fn .i18n(options)');
+ start();
+ });
+});
+
+asyncTest("binding (.i18n()) test with nested options", function() {
+ $.i18n.init({
+ lng: 'en-US',
+ lowerCaseLng: false,
+ ns: 'translation',
+ useLocalStorage: false,
+ resStore: {
+ 'en-US': { translation: { 'simpleTest': '__rootoption.first__ and then __rootoption.second__' } }
+ }
+ }, function(t) {
+ // given
+ $('#qunit-fixture').append('<button id="testBtn" data-i18n="simpleTest"></button>');
+
+ // when
+ $('#qunit-fixture').i18n({"rootoption":{"first":"ok_first_nested", "second":"ok_second_nested"}});
+
+ // then
+ equals($('#testBtn').text(),'ok_first_nested and then ok_second_nested', 'set text via fn .i18n(nested.options)');
+ start();
+ });
+});
+
asyncTest("switching lng", function() {
$.i18n.init({
lng: 'en-US',