-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2208 from parasharrajat/parasharrajat/translations
- Loading branch information
Showing
5 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = { | ||
// 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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
export default numberFormat; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |