Skip to content

Commit

Permalink
Merge pull request #15 from relayrides/b/numbers-in-t-params
Browse files Browse the repository at this point in the history
fix: convert numbers to strings in params before replace
  • Loading branch information
francescarpi authored Jan 13, 2017
2 parents 2018fae + 545dc21 commit 99507f4
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion dist/component/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/component/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
13 changes: 13 additions & 0 deletions test/component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -54,6 +55,14 @@ describe('component test', function() {
</Provider>
))

this.withNumberParamsNode = ReactDOM.findDOMNode(TestUtils.renderIntoDocument(
<Provider store={this.store}>
<I18n translations={translations}>
<TransWithNumberParams/>
</I18n>
</Provider>
))

this.dates = ReactDOM.findDOMNode(TestUtils.renderIntoDocument(
<Provider store={this.store}>
<I18n translations={translations}>
Expand Down Expand Up @@ -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!')
Expand Down
15 changes: 15 additions & 0 deletions test/components/TransWithNumberParams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from "react"

class TransWithNumberParams extends React.Component {
render() {
return (
<div>{this.context.t("{number} things!", {number: 13})}</div>
)
}
}

TransWithNumberParams.contextTypes = {
t: React.PropTypes.func.isRequired
}

export default TransWithNumberParams

0 comments on commit 99507f4

Please sign in to comment.