Permalink
Browse files

added functionality to set new options in reuse block

  • Loading branch information...
jamuhl committed Feb 12, 2013
1 parent b6c5006 commit 658ac65e9863b5fc07794001d8a9f9ce6da775dc
@@ -774,7 +774,11 @@
return str; return str;
} }
function applyReuse(translated, options){ function applyReuse(translated, options) {
var comma = ',';
var options_open = '{';
var options_close = '}';
var opts = f.extend({}, options); var opts = f.extend({}, options);
delete opts.postProcess; delete opts.postProcess;
@@ -785,6 +789,21 @@
var index_of_end_of_closing = translated.indexOf(o.reuseSuffix, index_of_opening) + o.reuseSuffix.length; var index_of_end_of_closing = translated.indexOf(o.reuseSuffix, index_of_opening) + o.reuseSuffix.length;
var token = translated.substring(index_of_opening, index_of_end_of_closing); var token = translated.substring(index_of_opening, index_of_end_of_closing);
var token_without_symbols = token.replace(o.reusePrefix, '').replace(o.reuseSuffix, ''); var token_without_symbols = token.replace(o.reusePrefix, '').replace(o.reuseSuffix, '');
if (token_without_symbols.indexOf(comma) != -1) {
var index_of_token_end_of_closing = token_without_symbols.indexOf(comma);
if (token_without_symbols.indexOf(options_open, index_of_token_end_of_closing) != -1 && token_without_symbols.indexOf(options_close, index_of_token_end_of_closing) != -1) {
var index_of_opts_opening = token_without_symbols.indexOf(options_open, index_of_token_end_of_closing);
var index_of_opts_end_of_closing = token_without_symbols.indexOf(options_close, index_of_opts_opening) + options_close.length;
try {
opts = f.extend(opts, JSON.parse(token_without_symbols.substring(index_of_opts_opening, index_of_opts_end_of_closing)));
token_without_symbols = token_without_symbols.substring(0, index_of_token_end_of_closing);
} catch (e) {
}
}
}
var translated_token = _translate(token_without_symbols, opts); var translated_token = _translate(token_without_symbols, opts);
translated = translated.replace(token, translated_token); translated = translated.replace(token, translated_token);
} }
@@ -893,7 +912,7 @@
'returned a object instead of string.'; 'returned a object instead of string.';
f.log(value); f.log(value);
} else { } else {
var copy = {}; var copy = {}; // apply child translation on a copy
for (var m in value) { for (var m in value) {
// apply translation on childs // apply translation on childs
copy[m] = _translate(ns + o.nsseparator + key + o.keyseparator + m, options); copy[m] = _translate(ns + o.nsseparator + key + o.keyseparator + m, options);
Binary file not shown.
@@ -770,7 +770,11 @@
return str; return str;
} }
function applyReuse(translated, options){ function applyReuse(translated, options) {
var comma = ',';
var options_open = '{';
var options_close = '}';
var opts = f.extend({}, options); var opts = f.extend({}, options);
delete opts.postProcess; delete opts.postProcess;
@@ -781,6 +785,21 @@
var index_of_end_of_closing = translated.indexOf(o.reuseSuffix, index_of_opening) + o.reuseSuffix.length; var index_of_end_of_closing = translated.indexOf(o.reuseSuffix, index_of_opening) + o.reuseSuffix.length;
var token = translated.substring(index_of_opening, index_of_end_of_closing); var token = translated.substring(index_of_opening, index_of_end_of_closing);
var token_without_symbols = token.replace(o.reusePrefix, '').replace(o.reuseSuffix, ''); var token_without_symbols = token.replace(o.reusePrefix, '').replace(o.reuseSuffix, '');
if (token_without_symbols.indexOf(comma) != -1) {
var index_of_token_end_of_closing = token_without_symbols.indexOf(comma);
if (token_without_symbols.indexOf(options_open, index_of_token_end_of_closing) != -1 && token_without_symbols.indexOf(options_close, index_of_token_end_of_closing) != -1) {
var index_of_opts_opening = token_without_symbols.indexOf(options_open, index_of_token_end_of_closing);
var index_of_opts_end_of_closing = token_without_symbols.indexOf(options_close, index_of_opts_opening) + options_close.length;
try {
opts = f.extend(opts, JSON.parse(token_without_symbols.substring(index_of_opts_opening, index_of_opts_end_of_closing)));
token_without_symbols = token_without_symbols.substring(0, index_of_token_end_of_closing);
} catch (e) {
}
}
}
var translated_token = _translate(token_without_symbols, opts); var translated_token = _translate(token_without_symbols, opts);
translated = translated.replace(token, translated_token); translated = translated.replace(token, translated_token);
} }
@@ -889,7 +908,7 @@
'returned a object instead of string.'; 'returned a object instead of string.';
f.log(value); f.log(value);
} else { } else {
var copy = {}; var copy = {}; // apply child translation on a copy
for (var m in value) { for (var m in value) {
// apply translation on childs // apply translation on childs
copy[m] = _translate(ns + o.nsseparator + key + o.keyseparator + m, options); copy[m] = _translate(ns + o.nsseparator + key + o.keyseparator + m, options);
Binary file not shown.
@@ -771,7 +771,11 @@
return str; return str;
} }
function applyReuse(translated, options){ function applyReuse(translated, options) {
var comma = ',';
var options_open = '{';
var options_close = '}';
var opts = f.extend({}, options); var opts = f.extend({}, options);
delete opts.postProcess; delete opts.postProcess;
@@ -782,6 +786,21 @@
var index_of_end_of_closing = translated.indexOf(o.reuseSuffix, index_of_opening) + o.reuseSuffix.length; var index_of_end_of_closing = translated.indexOf(o.reuseSuffix, index_of_opening) + o.reuseSuffix.length;
var token = translated.substring(index_of_opening, index_of_end_of_closing); var token = translated.substring(index_of_opening, index_of_end_of_closing);
var token_without_symbols = token.replace(o.reusePrefix, '').replace(o.reuseSuffix, ''); var token_without_symbols = token.replace(o.reusePrefix, '').replace(o.reuseSuffix, '');
if (token_without_symbols.indexOf(comma) != -1) {
var index_of_token_end_of_closing = token_without_symbols.indexOf(comma);
if (token_without_symbols.indexOf(options_open, index_of_token_end_of_closing) != -1 && token_without_symbols.indexOf(options_close, index_of_token_end_of_closing) != -1) {
var index_of_opts_opening = token_without_symbols.indexOf(options_open, index_of_token_end_of_closing);
var index_of_opts_end_of_closing = token_without_symbols.indexOf(options_close, index_of_opts_opening) + options_close.length;
try {
opts = f.extend(opts, JSON.parse(token_without_symbols.substring(index_of_opts_opening, index_of_opts_end_of_closing)));
token_without_symbols = token_without_symbols.substring(0, index_of_token_end_of_closing);
} catch (e) {
}
}
}
var translated_token = _translate(token_without_symbols, opts); var translated_token = _translate(token_without_symbols, opts);
translated = translated.replace(token, translated_token); translated = translated.replace(token, translated_token);
} }
@@ -890,7 +909,7 @@
'returned a object instead of string.'; 'returned a object instead of string.';
f.log(value); f.log(value);
} else { } else {
var copy = {}; var copy = {}; // apply child translation on a copy
for (var m in value) { for (var m in value) {
// apply translation on childs // apply translation on childs
copy[m] = _translate(ns + o.nsseparator + key + o.keyseparator + m, options); copy[m] = _translate(ns + o.nsseparator + key + o.keyseparator + m, options);
View
@@ -1,4 +1,5 @@
### 1.5.11pre ### 1.5.11pre
- function to set new options for nesting functionality
- function to add resources after init - function to add resources after init
- option to lookup in default namespace if value is not found in given namespace - option to lookup in default namespace if value is not found in given namespace
- option to change interpolation prefix and suffix via translation options - option to change interpolation prefix and suffix via translation options
@@ -128,21 +128,21 @@ describe('translation functionality', function() {
expect(i18n.t('nesting_default', {defaultValue: '0 $t(nesting1)'})).to.be('0 1 2 3'); expect(i18n.t('nesting_default', {defaultValue: '0 $t(nesting1)'})).to.be('0 1 2 3');
}); });
// describe('with setting new options', function() { describe('with setting new options', function() {
// var resStore = { var resStore = {
// dev: { translation: { nesting1_plural: '$t(nesting2, {"count": {girls}}) and {count} boys' } }, dev: { translation: { nesting1_plural: '$t(nesting2, {"count": __girls__}) and __count__ boys' } },
// en: { translation: { nesting2_plural: '{count} girls' } } en: { translation: { nesting2_plural: '__count__ girls' } }
// }; };
// beforeEach(function(done) { beforeEach(function(done) {
// i18n.init( $.extend(opts, { resStore: resStore }), i18n.init( $.extend(opts, { resStore: resStore }),
// function(t) { done(); }); function(t) { done(); });
// }); });
// it('it should translate nested value and set new options', function() { it('it should translate nested value and set new options', function() {
// expect(i18n.t('nesting1', {count: 2, girls: 3})).to.be('3 girls and 2 boys'); expect(i18n.t('nesting1', {count: 2, girls: 3})).to.be('3 girls and 2 boys');
// }); });
// }); });
}); });
View
@@ -15,7 +15,11 @@ function applyReplacement(str, replacementHash, nestedKey, options) {
return str; return str;
} }
function applyReuse(translated, options){ function applyReuse(translated, options) {
var comma = ',';
var options_open = '{';
var options_close = '}';
var opts = f.extend({}, options); var opts = f.extend({}, options);
delete opts.postProcess; delete opts.postProcess;
@@ -26,6 +30,21 @@ function applyReuse(translated, options){
var index_of_end_of_closing = translated.indexOf(o.reuseSuffix, index_of_opening) + o.reuseSuffix.length; var index_of_end_of_closing = translated.indexOf(o.reuseSuffix, index_of_opening) + o.reuseSuffix.length;
var token = translated.substring(index_of_opening, index_of_end_of_closing); var token = translated.substring(index_of_opening, index_of_end_of_closing);
var token_without_symbols = token.replace(o.reusePrefix, '').replace(o.reuseSuffix, ''); var token_without_symbols = token.replace(o.reusePrefix, '').replace(o.reuseSuffix, '');
if (token_without_symbols.indexOf(comma) != -1) {
var index_of_token_end_of_closing = token_without_symbols.indexOf(comma);
if (token_without_symbols.indexOf(options_open, index_of_token_end_of_closing) != -1 && token_without_symbols.indexOf(options_close, index_of_token_end_of_closing) != -1) {
var index_of_opts_opening = token_without_symbols.indexOf(options_open, index_of_token_end_of_closing);
var index_of_opts_end_of_closing = token_without_symbols.indexOf(options_close, index_of_opts_opening) + options_close.length;
try {
opts = f.extend(opts, JSON.parse(token_without_symbols.substring(index_of_opts_opening, index_of_opts_end_of_closing)));
token_without_symbols = token_without_symbols.substring(0, index_of_token_end_of_closing);
} catch (e) {
}
}
}
var translated_token = _translate(token_without_symbols, opts); var translated_token = _translate(token_without_symbols, opts);
translated = translated.replace(token, translated_token); translated = translated.replace(token, translated_token);
} }
View
@@ -832,21 +832,21 @@ describe('i18next', function() {
expect(i18n.t('nesting_default', {defaultValue: '0 $t(nesting1)'})).to.be('0 1 2 3'); expect(i18n.t('nesting_default', {defaultValue: '0 $t(nesting1)'})).to.be('0 1 2 3');
}); });
// describe('with setting new options', function() { describe('with setting new options', function() {
// var resStore = { var resStore = {
// dev: { translation: { nesting1_plural: '$t(nesting2, {"count": {girls}}) and {count} boys' } }, dev: { translation: { nesting1_plural: '$t(nesting2, {"count": __girls__}) and __count__ boys' } },
// en: { translation: { nesting2_plural: '{count} girls' } } en: { translation: { nesting2_plural: '__count__ girls' } }
// }; };
// beforeEach(function(done) { beforeEach(function(done) {
// i18n.init( $.extend(opts, { resStore: resStore }), i18n.init( $.extend(opts, { resStore: resStore }),
// function(t) { done(); }); function(t) { done(); });
// }); });
// it('it should translate nested value and set new options', function() { it('it should translate nested value and set new options', function() {
// expect(i18n.t('nesting1', {count: 2, girls: 3})).to.be('3 girls and 2 boys'); expect(i18n.t('nesting1', {count: 2, girls: 3})).to.be('3 girls and 2 boys');
// }); });
// }); });
}); });

0 comments on commit 658ac65

Please sign in to comment.