Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Localize HOC #2208

Merged
merged 4 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
"dependencies": {
"@babel/plugin-proposal-class-properties": "^7.12.1",
"@babel/preset-flow": "^7.12.13",
"@formatjs/intl-getcanonicallocales": "^1.5.8",
"@formatjs/intl-locale": "^2.4.21",
"@formatjs/intl-numberformat": "^6.2.5",
"@formatjs/intl-pluralrules": "^4.0.13",
"@react-native-community/async-storage": "^1.11.0",
"@react-native-community/cli": "4.13.1",
"@react-native-community/clipboard": "^1.5.1",
Expand Down
63 changes: 63 additions & 0 deletions src/components/withLocalize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 = {
parasharrajat marked this conversation as resolved.
Show resolved Hide resolved
// 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 = {
parasharrajat marked this conversation as resolved.
Show resolved Hide resolved
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,
};
14 changes: 14 additions & 0 deletions src/libs/numberFormat/index.android.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// we only need polyfills for Mobile.
import '@formatjs/intl-getcanonicallocales/polyfill';
import '@formatjs/intl-locale/polyfill';
import '@formatjs/intl-pluralrules/polyfill';
import '@formatjs/intl-numberformat/polyfill';

// Load en Locale data
import '@formatjs/intl-numberformat/locale-data/en';

function numberFormat(locale, number, options) {
return new Intl.NumberFormat(locale, options).format(number);
}
Comment on lines +10 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you test if importing and using the function defined in index.js works properly please?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I missed this comment


export default numberFormat;
5 changes: 5 additions & 0 deletions src/libs/numberFormat/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function numberFormat(locale, number, options) {
return new Intl.NumberFormat(locale, options).format(number);
}

export default numberFormat;