From 84f8db8a9ad5114e1c1cbc46eaf224750028fa68 Mon Sep 17 00:00:00 2001 From: Tim Barton Date: Mon, 19 Feb 2024 15:15:12 +0000 Subject: [PATCH 1/2] feat(conditionalIntl): added useNativeIntl to updateLocale --- __tests__/intl/__snapshots__/index.spec.js.snap | 7 +++++++ __tests__/intl/index.spec.js | 8 ++++++++ src/intl/index.js | 16 ++++++++++++---- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/__tests__/intl/__snapshots__/index.spec.js.snap b/__tests__/intl/__snapshots__/index.spec.js.snap index 73a8d16..bc37fdb 100644 --- a/__tests__/intl/__snapshots__/index.spec.js.snap +++ b/__tests__/intl/__snapshots__/index.spec.js.snap @@ -21,6 +21,13 @@ exports[`intl duck actions loadLanguagePack should throw when the error does not exports[`intl duck actions loadLanguagePack should throw when there is a non-404 error response 1`] = `[Error: Internal Server Error (https://example.com/cdn/foo-bar/1.0.0/bd-lc/foo-bar.json)]`; +exports[`intl duck actions should not call getLocalePack when useNativeIntl env var is true 1`] = ` +{ + "locale": "en-GB", + "type": "@americanexpress/one-app-ducks/intl/UPDATE_LOCALE", +} +`; + exports[`intl duck actions updateLocale actions test should allow any valid BCP 47 locale if enableAllIntlLocales is true 1`] = ` { "locale": "be-BY", diff --git a/__tests__/intl/index.spec.js b/__tests__/intl/index.spec.js index 1380ed8..aceb1fa 100644 --- a/__tests__/intl/index.spec.js +++ b/__tests__/intl/index.spec.js @@ -1072,6 +1072,14 @@ describe('intl duck', () => { }); }); }); + + it('should not call getLocalePack when useNativeIntl env var is true', async () => { + window.useNativeIntl = true; + const store = mockStore({ config: fromJS({ enableAllIntlLocales: 'true' }) }); + await store.dispatch(updateLocale('en-GB')); + expect(store.getActions()[0]).toMatchSnapshot(); + expect(loadedLocales.has('en-GB')).toEqual(false); + }); }); describe('getLocalePack', () => { diff --git a/src/intl/index.js b/src/intl/index.js index 1f93db0..9ccd87e 100644 --- a/src/intl/index.js +++ b/src/intl/index.js @@ -432,13 +432,21 @@ export function updateLocale(locale) { return Promise.reject(new Error('No locale was given')); } + const action = { + type: UPDATE_LOCALE, + locale, + }; + + if (window?.useNativeIntl || process.env.ONE_CONFIG_USE_NATIVE_INTL === 'true') { + return Promise.resolve().then(() => { + dispatch(action); + }); + } + // supporting files for locale dependent libraries return getLocalePack(locale) .then(() => { - dispatch({ - type: UPDATE_LOCALE, - locale, - }); + dispatch(action); }); }; } From 315720dc1b3dee3b99a128824c758af5b6b76a08 Mon Sep 17 00:00:00 2001 From: Tim Barton Date: Tue, 20 Feb 2024 10:03:29 +0000 Subject: [PATCH 2/2] feat(conditionalIntl): updated docs --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 224f72f..23aaf91 100644 --- a/README.md +++ b/README.md @@ -409,7 +409,7 @@ const loadDataAsProps = ({ store: { dispatch } }) => ({ This action creator is used to set the active locale for One App. The promise will resolve once the locale bundle (containing locale data for `Intl.js`, -[`React Intl`](https://www.npmjs.com/package/react-intl)) and +[`React Intl`](https://www.npmjs.com/package/react-intl) and [`Moment.js`](http://momentjs.com/)) is loaded and the active locale is set. It will reject if [Lean-Intl](https://github.com/sebastian-software/lean-intl) does not have a bundle for the locale. @@ -417,6 +417,8 @@ The locale bundles don't need to be an exact match. For instance setting the locale to `'en-XA'` will load `i18n/en.js` and `'zh-Hans-XB'` will load `i18n/zh-Hans.js`. +If the Environment Variable `ONE_CONFIG_USE_NATIVE_INTL` is set to `true`, then `updateLocale` will not try to add the Lean-Intl bundle, but will still update the active locale. + This action creator can take the following argument: | Argument | Type | Description |