-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
withLocalize.js
executable file
·179 lines (150 loc) · 5.47 KB
/
withLocalize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React, {createContext, forwardRef} from 'react';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import getComponentDisplayName from '../libs/getComponentDisplayName';
import ONYXKEYS from '../ONYXKEYS';
import * as Localize from '../libs/Localize';
import DateUtils from '../libs/DateUtils';
import * as LocalePhoneNumber from '../libs/LocalePhoneNumber';
import * as NumberFormatUtils from '../libs/NumberFormatUtils';
import * as LocaleDigitUtils from '../libs/LocaleDigitUtils';
import CONST from '../CONST';
const LocaleContext = createContext(null);
const withLocalizePropTypes = {
/** Returns translated string for given locale and phrase */
translate: PropTypes.func.isRequired,
/** Formats number formatted according to locale and options */
numberFormat: PropTypes.func.isRequired,
/** Converts a datetime into a localized string representation that's relative to current moment in time */
datetimeToRelative: PropTypes.func.isRequired,
/** Formats a datetime to local date and time string */
datetimeToCalendarTime: PropTypes.func.isRequired,
/** Returns a locally converted phone number without the country code */
toLocalPhone: PropTypes.func.isRequired,
/** Returns an internationally converted phone number with the country code */
fromLocalPhone: PropTypes.func.isRequired,
/** Gets the standard digit corresponding to a locale digit */
fromLocaleDigit: PropTypes.func.isRequired,
/** Gets the locale digit corresponding to a standard digit */
toLocaleDigit: PropTypes.func.isRequired,
};
const localeProviderPropTypes = {
/** The user's preferred locale e.g. 'en', 'es-ES' */
preferredLocale: PropTypes.string,
/* Actual content wrapped by this component */
children: PropTypes.node.isRequired,
};
const localeProviderDefaultProps = {
preferredLocale: CONST.DEFAULT_LOCALE,
};
class LocaleContextProvider extends React.Component {
/**
* The context this component exposes to child components
* @returns {object} translation util functions and locale
*/
getContextValue() {
return {
translate: this.translate.bind(this),
numberFormat: this.numberFormat.bind(this),
datetimeToRelative: this.datetimeToRelative.bind(this),
datetimeToCalendarTime: this.datetimeToCalendarTime.bind(this),
fromLocalPhone: this.fromLocalPhone.bind(this),
toLocalPhone: this.toLocalPhone.bind(this),
fromLocaleDigit: this.fromLocaleDigit.bind(this),
toLocaleDigit: this.toLocaleDigit.bind(this),
preferredLocale: this.props.preferredLocale,
};
}
/**
* @param {String} phrase
* @param {Object} [variables]
* @returns {String}
*/
translate(phrase, variables) {
return Localize.translate(this.props.preferredLocale, phrase, variables);
}
/**
* @param {Number} number
* @param {Intl.NumberFormatOptions} options
* @returns {String}
*/
numberFormat(number, options) {
return NumberFormatUtils.format(this.props.preferredLocale, number, options);
}
/**
* @param {String} datetime
* @returns {String}
*/
datetimeToRelative(datetime) {
return DateUtils.datetimeToRelative(this.props.preferredLocale, datetime);
}
/**
* @param {String} datetime - ISO-formatted datetime string
* @param {Boolean} [includeTimezone]
* @returns {String}
*/
datetimeToCalendarTime(datetime, includeTimezone) {
return DateUtils.datetimeToCalendarTime(
this.props.preferredLocale,
datetime,
includeTimezone,
);
}
/**
* @param {Number} number
* @returns {String}
*/
toLocalPhone(number) {
return LocalePhoneNumber.toLocalPhone(this.props.preferredLocale, number);
}
/**
* @param {Number} number
* @returns {String}
*/
fromLocalPhone(number) {
return LocalePhoneNumber.fromLocalPhone(this.props.preferredLocale, number);
}
/**
* @param {String} digit
* @returns {String}
*/
toLocaleDigit(digit) {
return LocaleDigitUtils.toLocaleDigit(this.props.preferredLocale, digit);
}
/**
* @param {String} localeDigit
* @returns {String}
*/
fromLocaleDigit(localeDigit) {
return LocaleDigitUtils.fromLocaleDigit(this.props.preferredLocale, localeDigit);
}
render() {
return (
<LocaleContext.Provider value={this.getContextValue()}>
{this.props.children}
</LocaleContext.Provider>
);
}
}
LocaleContextProvider.propTypes = localeProviderPropTypes;
LocaleContextProvider.defaultProps = localeProviderDefaultProps;
const Provider = withOnyx({
preferredLocale: {
key: ONYXKEYS.NVP_PREFERRED_LOCALE,
},
})(LocaleContextProvider);
Provider.displayName = 'withOnyx(LocaleContextProvider)';
export default function withLocalize(WrappedComponent) {
const WithLocalize = forwardRef((props, ref) => (
<LocaleContext.Consumer>
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
{translateUtils => <WrappedComponent {...translateUtils} {...props} ref={ref} />}
</LocaleContext.Consumer>
));
WithLocalize.displayName = `withLocalize(${getComponentDisplayName(WrappedComponent)})`;
return WithLocalize;
}
export {
withLocalizePropTypes,
Provider as LocaleContextProvider,
};