Skip to content

Commit 764c8ee

Browse files
committed
README.md: Update to new mixin API
1 parent e1beb1e commit 764c8ee

File tree

1 file changed

+49
-25
lines changed

1 file changed

+49
-25
lines changed

README.md

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
# react-globalize
22

3-
Easy to use [React](http://facebook.github.io/react/) components that provide internationalization features out of the box via [Globalize](https://github.com/jquery/globalize). With a quick update of a value, variable or locale, you get instant UI updates.
3+
Easy to use [React](http://facebook.github.io/react/) mixins that provide internationalization features to any React component via [Globalize](https://github.com/jquery/globalize). With a little initialization, you get instantly internationalized values in your components.
44

55
## Install
66
1. `npm install react-globalize`
77
2. In your application just:
8-
`var FormatCurrency = require('react-globalize').FormatCurrency;`
9-
3. Then follow the usage instructions for each component below
10-
11-
## Components
12-
Components are the heart of React and the whole point of react-globalize. These components provide simple, easily updatable elements in your page to display things like currency, dates, numbers and messages, formatted or translated to the current locale set by your application.
13-
14-
### FormatCurrency
8+
```JS
9+
var ReactGlobalize = require('react-globalize');
10+
var Globalize = require('globalize');
11+
12+
// Initialize Globalize and load your CLDR data
13+
// See https://github.com/jquery/globalize for details on Globalize usage
14+
15+
// In your component, this example just places the formatted value in a span
16+
var CurrencyComponent = React.createClass({
17+
mixins: [ReactGlobalize.formatCurrency],
18+
...
19+
render: function() {
20+
return (
21+
<span>{this.state.formattedValue}</span>
22+
);
23+
}
24+
});
25+
26+
// Then to use your currency component (with JSX)
27+
React.render(
28+
<CurrencyComponent locale="en" currency="USD" value={150}/>
29+
);
30+
// Which would render <span>$150.00</span>
31+
```
32+
33+
3. Further info about each mixin and its available props below
34+
35+
## Mixins
36+
These mixins provide a simple way to display things like currency, dates, numbers and messages, formatted or translated to the current locale set by your application. Each mixin has a set of props, both required and optional, to be included in your component. The mixin then uses the values of those props, to add a `formattedValue` to your component's state. Below is a listing of each mixin, its props and a usage example.
37+
38+
### formatCurrency
1539
#### Props
1640
- **locale** - required
1741
- A string value representing the CLDR indicator for the desired locale used in formatting the amount. Most apps will pull this value from the component's state or the state of one of its parent compenents.
@@ -22,20 +46,20 @@ Components are the heart of React and the whole point of react-globalize. These
2246
- **options**
2347
- An optional set of options to further format the value. See the [numberFormatter](https://github.com/jquery/globalize/blob/master/doc/api/number/number-formatter.md) docs in Globalize for more info on specific options
2448

25-
#### Usage
49+
#### Usage via an example FormatCurrency component
2650
- Default format with USD in English
2751

2852
`<FormatCurrency locale="en" currency="USD" value={150} />`
2953

30-
Result: $150.00
54+
Result: this.state.formattedValue = $150.00
3155

3256
- Accounting format with EUR in Portuguese
3357

3458
`<FormatCurrency locale="pt-BR" currency="EUR" value={-150} options={{ style: "accounting" }} />`
3559

36-
Result: (€150,00)
60+
Result: this.state.formattedValue = (€150,00)
3761

38-
### FormatDate
62+
### formatDate
3963
#### Props
4064
- **locale** - required
4165
- A string value representing the CLDR indicator for the desired locale used in formatting the date. Most apps will pull this value from the component's state or the state of one of its parent compenents.
@@ -44,20 +68,20 @@ Result: (€150,00)
4468
- **pattern** - required
4569
- An string or object which defines how to format the date. See the [dateFormatter](https://github.com/jquery/globalize/blob/master/doc/api/date/date-formatter.md) docs in Globalize for more info on supported patterns
4670

47-
#### Usage
71+
#### Usage via an example FormatDate component
4872
- Simple string skeleton in English
4973

5074
`<FormatDate locale="en" value={new Date()} pattern="GyMMMd" />`
5175

52-
Result: Feb 27, 2015 AD
76+
Result: this.state.formattedValue = Feb 27, 2015 AD
5377

5478
- Medium length date and time in Portuguese
5579

5680
`<FormatDate locale="pt-BR" value={new Date()} pattern={{ datetime: 'medium' }} />`
5781

58-
Result: 27 de fev de 2015 11:17:10
82+
Result: this.state.formattedValue = 27 de fev de 2015 11:17:10
5983

60-
### FormatMessage
84+
### formatMessage
6185
#### Props
6286
- **locale** - required
6387
- A string value representing the CLDR indicator for the desired locale used in formatting the message. Most apps will pull this value from the component's state or the state of one of its parent compenents.
@@ -66,7 +90,7 @@ Result: 27 de fev de 2015 11:17:10
6690
- **variables**
6791
- An array (where variables are represented as indeces) or object (for named variables) which contains values for variable replacement within a message.
6892

69-
#### Usage
93+
#### Usage via an example FormatMessage component
7094
Below message JSON used in these examples:
7195
```JS
7296
{
@@ -96,23 +120,23 @@ Below message JSON used in these examples:
96120

97121
Hi in English: `<FormatMessage locale="en" path="salutations/hi" />`
98122

99-
Result: Hi
123+
Result: this.state.formattedValue = Hi
100124

101125
Hi in Portuguese: `<FormatMessage locale="pt-BR" path="salutations/hi" />`
102126

103-
Result: Oi
127+
Result: this.state.formattedValue = Oi
104128

105129
- Variable Replacement
106130

107131
Array in English: `<FormatMessage locale="en" path="variables/hello" variables={["Wolfgang", "Amadeus", "Mozart"]} />`
108132

109-
Result: Hello, Wolfgang Amadeus Mozart
133+
Result: this.state.formattedValue = Hello, Wolfgang Amadeus Mozart
110134

111135
Object in Portuguese: `<FormatMessage locale="pt-BR" path="variables/hey" variables={{first:"Wolfgang", middle:"Amadeus", last:"Mozart"}} />`
112136

113-
Result: Ei, Wolfgang Amadeus Mozart
137+
Result: this.state.formattedValue = Ei, Wolfgang Amadeus Mozart
114138

115-
### FormatNumber
139+
### formatNumber
116140
#### Props
117141
- **locale** - required
118142
- A string value representing the CLDR indicator for the desired locale used in formatting the date. Most apps will pull this value from the component's state or the state of one of its parent compenents.
@@ -121,18 +145,18 @@ Result: Ei, Wolfgang Amadeus Mozart
121145
- **options**
122146
- An optional set of options to further format the value. See the [numberFormatter](https://github.com/jquery/globalize/blob/master/doc/api/number/number-formatter.md) docs in Globalize for more info on specific options
123147

124-
#### Usage
148+
#### Usage via an example FormatNumber component
125149
- Default format pi in English
126150

127151
`<FormatNumber locale="en" value={Math.PI} />`
128152

129-
Result: 3.142
153+
Result: this.state.formattedValue = 3.142
130154

131155
- Show at least 2 decimal places in Portuguese
132156

133157
`<FormatNumber locale="pt-BR" value={10000} options={{ minimumFractionDigits: 2 }} />`
134158

135-
Result: 10.000,00
159+
Result: this.state.formattedValue = 10.000,00
136160

137161
## License
138162
This project is distributed under the [MIT license](https://www.tldrlegal.com/l/mit).

0 commit comments

Comments
 (0)