diff --git a/lib/helpers.js b/lib/helpers.js index 5000a56..d7912ee 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -38,7 +38,8 @@ } - var CONTEXT_KEY = 'intl'; + var CONTEXT_KEY = 'intl', + RE_NUMERIC_PARAM_KEY = /^v(\d+)$/; /** @@ -181,7 +182,9 @@ var formatOptions = {}, locales, msg, - formatter; + formatter, + key, + matches; params = params || {}; if (params.hasOwnProperty('_msg')) { @@ -200,6 +203,15 @@ return chunk; } + for (key in params) { + if (params.hasOwnProperty(key)) { + matches = key.match(RE_NUMERIC_PARAM_KEY); + if (matches) { + params[matches[1]] = params[key]; + } + } + } + formatOptions = _contextGet(context, [CONTEXT_KEY, 'formats']); locales = _getLocales(chunk, params, context); formatter = new IntlMessageFormat(msg, locales, formatOptions); diff --git a/tests/helpers.js b/tests/helpers.js index d686c40..96e47dc 100644 --- a/tests/helpers.js +++ b/tests/helpers.js @@ -483,6 +483,19 @@ describe('Helper `intlMessage`', function () { expect(out).to.equal(expected); }); }); + + it('should support numeric token replacements (with `v` prefix)', function () { + var tmpl = '{@intlMessage _msg=MSG v0=firstName v1=lastName /}', + ctx = { + MSG: 'Hi, my name is {0} {1}.', + firstName: 'Anthony', + lastName: 'Pipkin' + }, + expected = "Hi, my name is Anthony Pipkin."; + Dust.renderSource(tmpl, ctx, function(err, out) { + expect(out).to.equal(expected); + }); + }); });