#React Native Globalize
Simple globalization library for React Native. Provides access to all formatting options as well as easy-to-use React Native components.
How does it work?
Because it's based on the jQuery Globalize project, React Native Globalize can format and parse numbers, format and parse dates, format currency, and format messages (using the ICU message pattern) using the correct plural rules for the language/locale. Take a look at https://github.com/jquery/globalize for more information.
The important thing to note is all this functionality depends entirely on CLDR data. While a huge number of languages and locales are available in this data, only some are loaded by default. This is done for performance reasons as React Native currently bundles everything into one large JS bundle, and with CLDR data being hundreds of megabytes, it causes some issues. The default languages are listed below, but you can always pass additional CLDR data if you need additional language/locale support:
var locales = [
'am',
'ar',
'bg',
'bn',
'ca',
'cs',
'da',
'de',
'el',
'en',
'en-GB',
'en-US',
'es',
'es-419',
'et',
'fa',
'fi',
'fil',
'fr',
'gu',
'he',
'hi',
'hr',
'hu',
'id',
'it',
'ja',
'kn',
'ko',
'lt',
'lv',
'ml',
'mr',
'ms',
'nb',
'nl',
'pl',
'pt',
'pt-PT',
'ro',
'ru',
'sk',
'sl',
'sr',
'sv',
'sw',
'ta',
'te',
'th',
'tr',
'uk',
'vi',
'zh',
'zh-Hans',
'zh-Hant',
];
Usage
Use the included wrapper at the root of your application to propagate the required context to all components. Alternatively, include getChildContext()
in your own component (see FormattedWrapper for an example).
var RNGlobalize = require('react-native-globalize');
var {
FormattedWrapper,
} = RNGlobalize;
class MyApp extends Component {
render() {
return (
<FormattedWrapper locale="en" currency="USD">
<AppComponent />
</FormattedWrapper>
);
}
}
Then you can use the included components anywhere you like.
var RNGlobalize = require('react-native-globalize');
var {
FormattedDate,
} = RNGlobalize;
class MyComponent extends Component {
render() {
return (
<View style={styles.awesomeBackground}>
<FormattedDate value={new Date()} skeleton="yMMMdhm" />
</View>
);
}
}
FormattedMessage can even take components as replacement values. You can pass values as individual named props or pass a values
object prop (or both - named props will override values
).
var RNGlobalize = require('react-native-globalize');
var {
Globalize,
FormattedDate,
FormattedMessage,
} = RNGlobalize;
var messages = {
en: {
today: 'The date today is: {date}',
},
};
Globalize.loadMessages(messages);
class MyComponent extends Component {
render() {
return (
<View style={styles.awesomeBackground}>
<FormattedMessage
message="today"
date={<FormattedDate value={new Date()} skeleton="yMMMdhm" />} />
</View>
);
}
}
You can also access the formatting functions directly from the context.
class MyComponent extends Component {
doStuff() {
var dateFormatter = this.context.globalize.getDateFormatter({skeleton: "yMMMdhm"});
var formattedDate = dateFormatter(new Date());
}
}
See https://github.com/jquery/globalize for all available formatting options.