-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
withLocalize.js
executable file
·88 lines (80 loc) · 3.38 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
import React from 'react';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import getComponentDisplayName from '../libs/getComponentDisplayName';
import compose from '../libs/compose';
import ONYXKEYS from '../ONYXKEYS';
import {translate} from '../libs/translate';
import DateUtils from '../libs/DateUtils';
import {toLocalPhone, fromLocalPhone} from '../libs/LocalePhoneNumber';
import numberFormat from '../libs/numberFormat';
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 timestamp into a localized string representation that's relative to current moment in time */
timestampToRelative: PropTypes.func.isRequired,
/** Formats a timestamp to local date and time string */
timestampToDateTime: 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,
};
function withLocalizeHOC(WrappedComponent) {
const WithLocalize = (props) => {
const translations = {
translate: (phrase, variables) => translate(props.preferredLocale, phrase, variables),
numberFormat: (number, options) => numberFormat(props.preferredLocale, number, options),
timestampToRelative: timestamp => DateUtils.timestampToRelative(props.preferredLocale, timestamp),
timestampToDateTime: (timestamp, includeTimezone) => DateUtils.timestampToDateTime(
props.preferredLocale,
timestamp,
includeTimezone,
),
toLocalPhone: number => toLocalPhone(props.preferredLocale, number),
fromLocalPhone: number => fromLocalPhone(props.preferredLocale, number),
};
return (
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
ref={props.forwardedRef}
translate={translations.translate}
numberFormat={translations.numberFormat}
timestampToRelative={translations.timestampToRelative}
timestampToDateTime={translations.timestampToDateTime}
toLocalPhone={translations.toLocalPhone}
fromLocalPhone={translations.fromLocalPhone}
/>
);
};
WithLocalize.displayName = `WithLocalize(${getComponentDisplayName(WrappedComponent)})`;
WithLocalize.propTypes = {
preferredLocale: PropTypes.string,
forwardedRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({current: PropTypes.instanceOf(React.Component)}),
]),
};
WithLocalize.defaultProps = {
preferredLocale: 'en',
forwardedRef: undefined,
};
return React.forwardRef((props, ref) => (
// eslint-disable-next-line react/jsx-props-no-spreading
<WithLocalize {...props} forwardedRef={ref} />
));
}
export default compose(
withOnyx({
preferredLocale: {
key: ONYXKEYS.PREFERRED_LOCALE,
},
}),
withLocalizeHOC,
);
export {
withLocalizePropTypes,
};