-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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 #15245 from Prince-Mendiratta/inconsistent-currenc…
…y-symbol [Intl] Polyfill Intl.NumberFormat if unexpected locale data on browser
- Loading branch information
Showing
7 changed files
with
87 additions
and
10 deletions.
There are no files selected for viewing
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,15 @@ | ||
import shouldPolyfill from './shouldPolyfill'; | ||
import polyfillNumberFormat from './polyfillNumberFormat'; | ||
|
||
/** | ||
* Polyfill the Intl API if the ICU version is old. | ||
* This ensures that the currency data is consistent across platforms and browsers. | ||
*/ | ||
export default function intlPolyfill() { | ||
if (!shouldPolyfill()) { | ||
return; | ||
} | ||
|
||
// Just need to polyfill Intl.NumberFormat for web based platforms | ||
polyfillNumberFormat(); | ||
} |
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,17 @@ | ||
import shouldPolyfill from './shouldPolyfill'; | ||
import polyfillNumberFormat from './polyfillNumberFormat'; | ||
|
||
/** | ||
* Polyfill the Intl API, always performed for native devices. | ||
*/ | ||
export default function polyfill() { | ||
if (!shouldPolyfill()) { | ||
return; | ||
} | ||
|
||
// Native devices require extra polyfills | ||
require('@formatjs/intl-getcanonicallocales/polyfill'); | ||
require('@formatjs/intl-locale/polyfill'); | ||
require('@formatjs/intl-pluralrules/polyfill'); | ||
polyfillNumberFormat(); | ||
} |
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,13 @@ | ||
/** | ||
* Common imports that are required for all platforms | ||
*/ | ||
|
||
function polyfillNumberFormat() { | ||
require('@formatjs/intl-numberformat/polyfill-force'); | ||
|
||
// Load en & es Locale data | ||
require('@formatjs/intl-numberformat/locale-data/en'); | ||
require('@formatjs/intl-numberformat/locale-data/es'); | ||
} | ||
|
||
export default polyfillNumberFormat; |
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,29 @@ | ||
import CONST from '../../CONST'; | ||
|
||
/** | ||
* Check if the locale data is as expected on the device. | ||
* Ensures that the currency data is consistent across devices. | ||
* @returns {Boolean} | ||
*/ | ||
function oldCurrencyData() { | ||
return ( | ||
new Intl.NumberFormat(CONST.DEFAULT_LOCALE, { | ||
style: CONST.POLYFILL_TEST.STYLE, | ||
currency: CONST.POLYFILL_TEST.CURRENCY, | ||
currencyDisplay: CONST.POLYFILL_TEST.FORMAT, | ||
}).format(CONST.POLYFILL_TEST.SAMPLE_INPUT) !== CONST.POLYFILL_TEST.EXPECTED_OUTPUT | ||
); | ||
} | ||
|
||
/** | ||
* Check for the existence of the Intl API and ensure results will | ||
* be as expected. | ||
* @returns {Boolean} | ||
*/ | ||
export default function shouldPolyfill() { | ||
return ( | ||
typeof Intl === 'undefined' | ||
|| !('NumberFormat' in Intl) | ||
|| oldCurrencyData() | ||
); | ||
} |
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