diff --git a/CHANGELOG.md b/CHANGELOG.md index 43fcf86..eeb56f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -88,4 +88,8 @@ - Escapes any '$' characters passed to *t* function via params object to prevent unexpected behavior with string.replace(). Thanks to *gannoncurran* (https://github.com/APSL/redux-i18n/pull/14) -## 1.0.11 (Not created yet) +## 1.0.11 + +- Fix: make sure numbers passed to *t* function via params object are converted to strings so .replace() won't fail + +## 1.0.12 (Not created yet) diff --git a/dist/component/component.js b/dist/component/component.js index f08afff..eb1b913 100644 --- a/dist/component/component.js +++ b/dist/component/component.js @@ -48,7 +48,7 @@ var I18n = function (_React$Component) { var reg = new RegExp('\{' + k + '\}', 'g'); // Escape possible '$' in params to prevent unexpected behavior with .replace() // especially important for IE11, which misinterprets '$0' as a regex command - var param = _params[k].replace(/\$/g, '$$$$'); + var param = _params[k].toString().replace(/\$/g, '$$$$'); text = text.replace(reg, param); } } diff --git a/package.json b/package.json index f2c29a1..b6ed17b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "redux-i18n", - "version": "1.0.10", + "version": "1.0.11", "description": "A simple and powerful package for translate your react applications.", "main": "./dist/index.js", "repository": { diff --git a/src/component/component.js b/src/component/component.js index fc7be2c..f8cc637 100644 --- a/src/component/component.js +++ b/src/component/component.js @@ -20,7 +20,7 @@ class I18n extends React.Component { let reg = new RegExp('\{' + k + '\}', 'g') // Escape possible '$' in params to prevent unexpected behavior with .replace() // especially important for IE11, which misinterprets '$0' as a regex command - let param = params[k].replace(/\$/g, '$$$$') + let param = params[k].toString().replace(/\$/g, '$$$$') text = text.replace(reg, param) } } diff --git a/test/component.spec.js b/test/component.spec.js index f24a06b..ab4ebb8 100644 --- a/test/component.spec.js +++ b/test/component.spec.js @@ -12,6 +12,7 @@ import {setLanguage} from '../dist/actions' import TransWithoutParams from './components/TransWithoutParams' import TransWithParams from './components/TransWithParams' import TransWithDollarSignParams from './components/TransWithDollarSignParams' +import TransWithNumberParams from './components/TransWithNumberParams' import Dates from './components/Dates' const translations = { @@ -54,6 +55,14 @@ describe('component test', function() { )) + this.withNumberParamsNode = ReactDOM.findDOMNode(TestUtils.renderIntoDocument( + + + + + + )) + this.dates = ReactDOM.findDOMNode(TestUtils.renderIntoDocument( @@ -87,6 +96,10 @@ describe('component test', function() { expect(this.withDollarSignParamsNode.textContent).toEqual('We should have two dollar signs $$!') }) + it('text with number params', function() { + expect(this.withNumberParamsNode.textContent).toEqual('13 things!') + }) + it('changing language in text with params', function() { this.store.dispatch(setLanguage('es')) expect(this.withParamsNode.textContent).toEqual('Hola Francesc!') diff --git a/test/components/TransWithNumberParams.js b/test/components/TransWithNumberParams.js new file mode 100644 index 0000000..1ed9b90 --- /dev/null +++ b/test/components/TransWithNumberParams.js @@ -0,0 +1,15 @@ +import React from "react" + +class TransWithNumberParams extends React.Component { + render() { + return ( +
{this.context.t("{number} things!", {number: 13})}
+ ) + } +} + +TransWithNumberParams.contextTypes = { + t: React.PropTypes.func.isRequired +} + +export default TransWithNumberParams