Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Feat/conditional native intl #76

Merged
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,14 +409,16 @@ 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.

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 |
Expand Down
7 changes: 7 additions & 0 deletions __tests__/intl/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions __tests__/intl/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,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', () => {
Expand Down
16 changes: 12 additions & 4 deletions src/intl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,13 +433,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);
});
};
}
Loading