Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

set interpolation prefix/suffix inside translation options

  • Loading branch information...
commit 56cef66ef3b20f3e8ffc41800c7de2aaa69ac90f 1 parent cd62e4c
@jamuhl jamuhl authored
View
16 release/i18next-1.5.11pre.js
@@ -744,14 +744,18 @@
};
}
- function applyReplacement(str, replacementHash, nestedKey) {
- if (str.indexOf(o.interpolationPrefix) < 0) return str;
+ function applyReplacement(str, replacementHash, nestedKey, options) {
+ options = options || replacementHash; // first call uses replacement hash combined with options
+ if (str.indexOf(options.interpolationPrefix || o.interpolationPrefix) < 0) return str;
+
+ var prefix = options.interpolationPrefix ? f.regexEscape(options.interpolationPrefix) : o.interpolationPrefixEscaped
+ , suffix = options.interpolationSuffix ? f.regexEscape(options.interpolationSuffix) : o.interpolationSuffixEscaped;
f.each(replacementHash, function(key, value) {
if (typeof value === 'object' && value !== null) {
- str = applyReplacement(str, value, nestedKey ? nestedKey + '.' + key : key);
+ str = applyReplacement(str, value, nestedKey ? nestedKey + o.keyseparator + key : key, options);
} else {
- str = str.replace(new RegExp([o.interpolationPrefixEscaped, nestedKey ? nestedKey + '.' + key : key, o.interpolationSuffixEscaped].join(''), 'g'), value);
+ str = str.replace(new RegExp([prefix, nestedKey ? nestedKey + o.keyseparator + key : key, suffix].join(''), 'g'), value);
}
});
return str;
@@ -767,8 +771,8 @@
var index_of_opening = translated.indexOf(o.reusePrefix);
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_sans_symbols = token.replace(o.reusePrefix, '').replace(o.reuseSuffix, '');
- var translated_token = _translate(token_sans_symbols, opts);
+ var token_without_symbols = token.replace(o.reusePrefix, '').replace(o.reuseSuffix, '');
+ var translated_token = _translate(token_without_symbols, opts);
translated = translated.replace(token, translated_token);
}
return translated;
View
BIN  release/i18next-1.5.11pre.zip
Binary file not shown
View
16 release/i18next.amd-1.5.11pre.js
@@ -740,14 +740,18 @@
};
}
- function applyReplacement(str, replacementHash, nestedKey) {
- if (str.indexOf(o.interpolationPrefix) < 0) return str;
+ function applyReplacement(str, replacementHash, nestedKey, options) {
+ options = options || replacementHash; // first call uses replacement hash combined with options
+ if (str.indexOf(options.interpolationPrefix || o.interpolationPrefix) < 0) return str;
+
+ var prefix = options.interpolationPrefix ? f.regexEscape(options.interpolationPrefix) : o.interpolationPrefixEscaped
+ , suffix = options.interpolationSuffix ? f.regexEscape(options.interpolationSuffix) : o.interpolationSuffixEscaped;
f.each(replacementHash, function(key, value) {
if (typeof value === 'object' && value !== null) {
- str = applyReplacement(str, value, nestedKey ? nestedKey + '.' + key : key);
+ str = applyReplacement(str, value, nestedKey ? nestedKey + o.keyseparator + key : key, options);
} else {
- str = str.replace(new RegExp([o.interpolationPrefixEscaped, nestedKey ? nestedKey + '.' + key : key, o.interpolationSuffixEscaped].join(''), 'g'), value);
+ str = str.replace(new RegExp([prefix, nestedKey ? nestedKey + o.keyseparator + key : key, suffix].join(''), 'g'), value);
}
});
return str;
@@ -763,8 +767,8 @@
var index_of_opening = translated.indexOf(o.reusePrefix);
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_sans_symbols = token.replace(o.reusePrefix, '').replace(o.reuseSuffix, '');
- var translated_token = _translate(token_sans_symbols, opts);
+ var token_without_symbols = token.replace(o.reusePrefix, '').replace(o.reuseSuffix, '');
+ var translated_token = _translate(token_without_symbols, opts);
translated = translated.replace(token, translated_token);
}
return translated;
View
BIN  release/i18next.amd-1.5.11pre.zip
Binary file not shown
View
16 release/i18next.amd.withJQuery-1.5.11pre.js
@@ -741,14 +741,18 @@
};
}
- function applyReplacement(str, replacementHash, nestedKey) {
- if (str.indexOf(o.interpolationPrefix) < 0) return str;
+ function applyReplacement(str, replacementHash, nestedKey, options) {
+ options = options || replacementHash; // first call uses replacement hash combined with options
+ if (str.indexOf(options.interpolationPrefix || o.interpolationPrefix) < 0) return str;
+
+ var prefix = options.interpolationPrefix ? f.regexEscape(options.interpolationPrefix) : o.interpolationPrefixEscaped
+ , suffix = options.interpolationSuffix ? f.regexEscape(options.interpolationSuffix) : o.interpolationSuffixEscaped;
f.each(replacementHash, function(key, value) {
if (typeof value === 'object' && value !== null) {
- str = applyReplacement(str, value, nestedKey ? nestedKey + '.' + key : key);
+ str = applyReplacement(str, value, nestedKey ? nestedKey + o.keyseparator + key : key, options);
} else {
- str = str.replace(new RegExp([o.interpolationPrefixEscaped, nestedKey ? nestedKey + '.' + key : key, o.interpolationSuffixEscaped].join(''), 'g'), value);
+ str = str.replace(new RegExp([prefix, nestedKey ? nestedKey + o.keyseparator + key : key, suffix].join(''), 'g'), value);
}
});
return str;
@@ -764,8 +768,8 @@
var index_of_opening = translated.indexOf(o.reusePrefix);
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_sans_symbols = token.replace(o.reusePrefix, '').replace(o.reuseSuffix, '');
- var translated_token = _translate(token_sans_symbols, opts);
+ var token_without_symbols = token.replace(o.reusePrefix, '').replace(o.reuseSuffix, '');
+ var translated_token = _translate(token_without_symbols, opts);
translated = translated.replace(token, translated_token);
}
return translated;
View
34 spec/functions.translation.spec.js
@@ -184,6 +184,40 @@ describe('translation functionality', function() {
});
+ describe('default i18next way - different prefix/suffix via options', function() {
+
+ var resStore = {
+ dev: { translation: { } },
+ en: { translation: { } },
+ 'en-US': {
+ translation: {
+ interpolationTest1: 'added *toAdd*',
+ interpolationTest2: 'added *toAdd* *toAdd* twice',
+ interpolationTest3: 'added *child.one* *child.two*',
+ interpolationTest4: 'added *child.grandChild.three*'
+ }
+ }
+ };
+
+ beforeEach(function(done) {
+ i18n.init( $.extend(opts, {
+ resStore: resStore
+ }), function(t) { done(); });
+ });
+
+ it('it should replace passed in key/values', function() {
+ expect(i18n.t('interpolationTest1', {toAdd: 'something', interpolationPrefix: '*', interpolationSuffix: '*'})).to.be('added something');
+ expect(i18n.t('interpolationTest2', {toAdd: 'something', interpolationPrefix: '*', interpolationSuffix: '*'})).to.be('added something something twice');
+ expect(i18n.t('interpolationTest3', { child: { one: '1', two: '2'}, interpolationPrefix: '*', interpolationSuffix: '*'})).to.be('added 1 2');
+ expect(i18n.t('interpolationTest4', { child: { grandChild: { three: '3'}}, interpolationPrefix: '*', interpolationSuffix: '*'})).to.be('added 3');
+ });
+
+ it('it should replace passed in key/values on defaultValue', function() {
+ expect(i18n.t('interpolationTest5', {defaultValue: 'added *toAdd*', toAdd: 'something', interpolationPrefix: '*', interpolationSuffix: '*'})).to.be('added something');
+ });
+
+ });
+
describe('using sprintf', function() {
var resStore = {
View
16 src/i18next.translate.js
@@ -1,11 +1,15 @@
-function applyReplacement(str, replacementHash, nestedKey) {
- if (str.indexOf(o.interpolationPrefix) < 0) return str;
+function applyReplacement(str, replacementHash, nestedKey, options) {
+ options = options || replacementHash; // first call uses replacement hash combined with options
+ if (str.indexOf(options.interpolationPrefix || o.interpolationPrefix) < 0) return str;
+
+ var prefix = options.interpolationPrefix ? f.regexEscape(options.interpolationPrefix) : o.interpolationPrefixEscaped
+ , suffix = options.interpolationSuffix ? f.regexEscape(options.interpolationSuffix) : o.interpolationSuffixEscaped;
f.each(replacementHash, function(key, value) {
if (typeof value === 'object' && value !== null) {
- str = applyReplacement(str, value, nestedKey ? nestedKey + '.' + key : key);
+ str = applyReplacement(str, value, nestedKey ? nestedKey + o.keyseparator + key : key, options);
} else {
- str = str.replace(new RegExp([o.interpolationPrefixEscaped, nestedKey ? nestedKey + '.' + key : key, o.interpolationSuffixEscaped].join(''), 'g'), value);
+ str = str.replace(new RegExp([prefix, nestedKey ? nestedKey + o.keyseparator + key : key, suffix].join(''), 'g'), value);
}
});
return str;
@@ -21,8 +25,8 @@ function applyReuse(translated, options){
var index_of_opening = translated.indexOf(o.reusePrefix);
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_sans_symbols = token.replace(o.reusePrefix, '').replace(o.reuseSuffix, '');
- var translated_token = _translate(token_sans_symbols, opts);
+ var token_without_symbols = token.replace(o.reusePrefix, '').replace(o.reuseSuffix, '');
+ var translated_token = _translate(token_without_symbols, opts);
translated = translated.replace(token, translated_token);
}
return translated;
View
34 test/test.js
@@ -839,6 +839,40 @@ describe('i18next', function() {
});
+ describe('default i18next way - different prefix/suffix via options', function() {
+
+ var resStore = {
+ dev: { translation: { } },
+ en: { translation: { } },
+ 'en-US': {
+ translation: {
+ interpolationTest1: 'added *toAdd*',
+ interpolationTest2: 'added *toAdd* *toAdd* twice',
+ interpolationTest3: 'added *child.one* *child.two*',
+ interpolationTest4: 'added *child.grandChild.three*'
+ }
+ }
+ };
+
+ beforeEach(function(done) {
+ i18n.init( $.extend(opts, {
+ resStore: resStore
+ }), function(t) { done(); });
+ });
+
+ it('it should replace passed in key/values', function() {
+ expect(i18n.t('interpolationTest1', {toAdd: 'something', interpolationPrefix: '*', interpolationSuffix: '*'})).to.be('added something');
+ expect(i18n.t('interpolationTest2', {toAdd: 'something', interpolationPrefix: '*', interpolationSuffix: '*'})).to.be('added something something twice');
+ expect(i18n.t('interpolationTest3', { child: { one: '1', two: '2'}, interpolationPrefix: '*', interpolationSuffix: '*'})).to.be('added 1 2');
+ expect(i18n.t('interpolationTest4', { child: { grandChild: { three: '3'}}, interpolationPrefix: '*', interpolationSuffix: '*'})).to.be('added 3');
+ });
+
+ it('it should replace passed in key/values on defaultValue', function() {
+ expect(i18n.t('interpolationTest5', {defaultValue: 'added *toAdd*', toAdd: 'something', interpolationPrefix: '*', interpolationSuffix: '*'})).to.be('added something');
+ });
+
+ });
+
describe('using sprintf', function() {
var resStore = {

0 comments on commit 56cef66

Please sign in to comment.
Something went wrong with that request. Please try again.