Skip to content
Open
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
10 changes: 4 additions & 6 deletions docs/how_tos/i18n.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ These steps will allow your application to accept translation strings. See `fron

#. For places in your code where you need a display string, and it has to be a plain JavaScript string (e.g., a button label), you will need to do the following:

#. Inject the ``intl`` object into your component:
#. Use a hook to access the ``intl`` object within your component:

#. ``import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';``;
#. ``import { useIntl } from '@openedx/frontend-base';``;

#. add ``intl: intlShape.isRequired`` to your component's ``propTypes``.

#. instead of ``export Foo``, ``export injectIntl(Foo)`` .
#. write ``const intl = useIntl();`` near the beginning of your component.

#. Define your messages using ``defineMessages``. This function doesn't actually do anything; it's just a hook for the translation pipeline to be able to find your translation strings. You can call ``defineMessages`` wherever you want, but if you have a lot of them you might want to move them to a separate file. Either ``MyAppName.messages.js`` (if your entire app has only a few strings) or ``SomeComponent.messages.js`` will work. Your file should look like the example below. For your own sanity, using a short camel-case string for the property name is fine as long as ``id`` is globally unique in the MFE. Example::

import { defineMessages } from '@edx/frontend-platform/i18n';
import { defineMessages } from '@openedx/frontend-base';

const messages = defineMessages({
'cartPayNow': {
Expand Down
348 changes: 57 additions & 291 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
"prop-types": "^15.8.1",
"react-dev-utils": "12.0.1",
"react-focus-on": "^3.10.2",
"react-intl": "^6.6.6",
"react-intl": "^10.1.20",
"react-refresh": "0.18.0",
"react-refresh-typescript": "^2.0.9",
"react-responsive": "^10.0.0",
Expand Down Expand Up @@ -160,7 +160,7 @@
"nodemon": "^3.1.4"
},
"peerDependencies": {
"@openedx/paragon": "^23.20.0",
"@openedx/paragon": "^23.23.0",
"@tanstack/react-query": "^5.81.2",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
72 changes: 5 additions & 67 deletions runtime/i18n/index.js → runtime/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,66 +23,6 @@
*
*/

/**
* @name createIntl
* @kind function
* @see {@link https://formatjs.io/docs/react-intl/api#createIntl Intl}
*/

/**
* @name FormattedDate
* @kind class
* @see {@link https://formatjs.io/docs/react-intl/components/#formatteddate Intl}
*/

/**
* @name FormattedTime
* @kind class
* @see {@link https://formatjs.io/docs/react-intl/components/#formattedtime Intl}
*/

/**
* @name FormattedRelativeTime
* @kind class
* @see {@link https://formatjs.io/docs/react-intl/components/#formattedrelativetime Intl}
*/

/**
* @name FormattedNumber
* @kind class
* @see {@link https://formatjs.io/docs/react-intl/components/#formattednumber Intl}
*/

/**
* @name FormattedPlural
* @kind class
* @see {@link https://formatjs.io/docs/react-intl/components/#formattedplural Intl}
*/

/**
* @name FormattedMessage
* @kind class
* @see {@link https://formatjs.io/docs/react-intl/components/#formattedmessage Intl}
*/

/**
* @name IntlProvider
* @kind class
* @see {@link https://formatjs.io/docs/react-intl/components/#intlprovider Intl}
*/

/**
* @name defineMessages
* @kind function
* @see {@link https://formatjs.io/docs/react-intl/api#definemessagesdefinemessage Intl}
*/

/**
* @name useIntl
* @kind function
* @see {@link https://formatjs.io/docs/react-intl/api#useIntl Intl}
*/

export {
createIntl,
defineMessages,
Expand All @@ -93,7 +33,10 @@ export {
FormattedRelativeTime,
FormattedTime,
IntlProvider,
useIntl
useIntl,
type IntlConfig,
type ResolvedIntlConfig,
type IntlShape,
} from 'react-intl';

export {
Expand All @@ -104,14 +47,9 @@ export {
getPrimaryLanguageSubtag,
getSupportedLanguageList,
handleRtl,
intlShape,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

runtime/i18n/lib.ts still declares it. Worth deleting there too, along with the then-unused prop-types import at the top.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah good catch. Done.

isRtl,
LOCALE_CHANGED,
LOCALE_TOPIC,
mergeMessages,
updateLocale
updateLocale,
} from './lib';

export {
default as injectIntl
} from './injectIntlWithShim';
48 changes: 0 additions & 48 deletions runtime/i18n/injectIntlWithShim.jsx

This file was deleted.

13 changes: 0 additions & 13 deletions runtime/i18n/lib.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import merge from 'lodash/merge';
import PropTypes from 'prop-types';
import { MessageFormatElement } from 'react-intl';
import Cookies from 'universal-cookie';

Expand Down Expand Up @@ -51,18 +50,6 @@ const rtlLocales = [

let messages: Record<string, Record<string, string> | Record<string, MessageFormatElement[]> | undefined>;

/**
* @memberof module:Internationalization
*
* Prior versions of react-intl (our primary implementation of the i18n service) included a
* PropTypes-based 'shape' for its `intl` object. This has since been removed. For legacy
* compatibility, we include an `intlShape` export that is set to PropTypes.object. Usage of this
* export is deprecated.
*
* @deprecated
*/
export const intlShape = PropTypes.object;

/**
* @memberof module:Internationalization
*/
Expand Down
7 changes: 4 additions & 3 deletions runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,16 @@ export {
getPrimaryLanguageSubtag,
getSupportedLanguageList,
handleRtl,
injectIntl,
IntlProvider,
intlShape,
isRtl,
LOCALE_CHANGED,
LOCALE_TOPIC,
mergeMessages,
updateLocale,
useIntl
useIntl,
type IntlConfig,
type ResolvedIntlConfig,
type IntlShape,
} from './i18n';

export {
Expand Down
2 changes: 1 addition & 1 deletion runtime/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = {
'<rootDir>/setupTest.js',
],
transformIgnorePatterns: [
'/node_modules/(?!(@openedx|@edx)/)',
'/node_modules/(?!(@openedx|@edx|react-intl|@formatjs|intl-messageformat)/)',
],
modulePathIgnorePatterns: [
'/dist/',
Expand Down
2 changes: 2 additions & 0 deletions shell/header/helpButtonSlotOperation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ describe('helpButtonSlotOperation', () => {
mergeAppConfig(TEST_APP_ID, { SUPPORT_URL: 'https://help.example.com/test' });

const op = helpButtonSlotOperation({ appId: TEST_APP_ID, role: TEST_ROLE });
// The following is for TypeScript, since `WidgetRendererProps` sometimes has 'element' and sometimes 'component'.
if (!('element' in op)) throw new Error(`Expected ${JSON.stringify(op)} to have an 'element'.`);
expect(isValidElement(op.element)).toBe(true);

const getUrl = (op.element as React.ReactElement<{ getUrl: () => string | undefined }>).props.getUrl;
Expand Down
2 changes: 1 addition & 1 deletion shell/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
'setupTest.js',
],
transformIgnorePatterns: [
'/node_modules/(?!(@openedx|@edx)/)',
'/node_modules/(?!(@openedx|@edx|react-intl|@formatjs|intl-messageformat)/)',
],
testPathIgnorePatterns: [
'/site.config.test.tsx',
Expand Down
2 changes: 1 addition & 1 deletion tools/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
'cli/**/*.{js,jsx,ts,tsx}',
],
transformIgnorePatterns: [
'/node_modules/(?!(@openedx|@edx)/)',
'/node_modules/(?!(@openedx|@edx|react-intl|@formatjs|intl-messageformat)/)',
],
modulePathIgnorePatterns: [
'/dist/',
Expand Down
2 changes: 1 addition & 1 deletion tools/jest/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
'/node_modules/',
],
transformIgnorePatterns: [
'/node_modules/(?!(@openedx|@edx)/)',
'/node_modules/(?!(@openedx|@edx|react-intl|@formatjs|intl-messageformat)/)',
],
modulePathIgnorePatterns: [
'/dist/',
Expand Down