-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
withLocalize.js
63 lines (60 loc) · 2.28 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
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 = {
// Translations functions using current User's preferred locale
translations: PropTypes.shape({
translate: PropTypes.func.isRequired,
numberFormat: PropTypes.func.isRequired,
timestampToRelative: PropTypes.func.isRequired,
timestampToDateTime: PropTypes.func.isRequired,
toLocalPhone: PropTypes.func.isRequired,
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}
translations={translations}
/>
);
};
withLocalize.displayName = `withLocalize(${getComponentDisplayName(WrappedComponent)})`;
withLocalize.defaultProps = {
preferredLocale: 'en',
};
return withLocalize;
}
export default compose(
withOnyx({
preferredLocale: {
key: ONYXKEYS.PREFERRED_LOCALE,
},
}),
withLocalizeHOC,
);
export {
withLocalizePropTypes,
};