Skip to content

Commit

Permalink
Added the ability nest reuse with new options
Browse files Browse the repository at this point in the history
'mans_and_girls_plural_5': '$t(girls, {"count": {girls}}) and {count} mans',
'girls_plural_5': '{count} girls'

t('mans_and_girls', {count: 5, girls: 5}); //5 girls and 5 mans
  • Loading branch information
orloffv committed Jan 17, 2013
1 parent 10410ee commit 42c57a1
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/i18next.translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ function applyReplacement(str, replacementHash, nestedKey) {
}

function applyReuse(translated, options){
var comma = ',';
var options_open = '{';
var options_close = '}';
var opts = f.extend({}, options);
delete opts.postProcess;

Expand All @@ -22,6 +25,20 @@ function applyReuse(translated, options){
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, '');

if (token_sans_symbols.indexOf(comma) != -1) {
var index_of_token_end_of_closing = token_sans_symbols.indexOf(comma);
if (token_sans_symbols.indexOf(options_open, index_of_token_end_of_closing) != -1 && token_sans_symbols.indexOf(options_close, index_of_token_end_of_closing) != -1) {
var index_of_opts_opening = token_sans_symbols.indexOf(options_open, index_of_token_end_of_closing);
var index_of_opts_end_of_closing = token_sans_symbols.indexOf(options_close, index_of_opts_opening) + options_close.length;
try {
opts = f.extend(opts, JSON.parse(token_sans_symbols.substring(index_of_opts_opening, index_of_opts_end_of_closing)));
token_sans_symbols = token_sans_symbols.substring(0, index_of_token_end_of_closing);
} catch (e) {
}
}
}

var translated_token = _translate(token_sans_symbols, opts);
translated = translated.replace(token, translated_token);
}
Expand Down

0 comments on commit 42c57a1

Please sign in to comment.