7.0.1 • Published 9 months ago

@gasket/react-intl v7.0.1

Weekly downloads
22
License
MIT
Repository
github
Last release
9 months ago

@gasket/react-intl

React component library to enable localization for Gasket apps. Loads and manages locale files from @gasket/plugin-intl.

Installation

npm i @gasket/react-intl

Components

withMessagesProvider

Use this to wrap a component to provide messages through context and/or set up an intl provider such as react-intl.

Signature

  • withMessagesProvider(intlManager, options?)(Component)

Props

  • intlManager - (object) An instance of the intl manager created by @gasket/plugin-intl.
  • [options] - (object) Optional configuration

Example PageRouter with react-intl

// pages/_app.js
import { useRouter } from 'next/router';
import { IntlProvider } from 'react-intl';
import { withMessagesProvider } from '@gasket/react-intl';
import intlManager from '../path/to/intl.js';

const IntlMessagesProvider = withMessagesProvider(intlManager)(IntlProvider);

export default function App({ Component, pageProps }) {
  const router = useRouter();
  
  return (
    <IntlMessagesProvider locale={router.locale}>
      <Component {...pageProps} />
    </IntlMessagesProvider>
  );
}

Example with custom provider

You can wrap any intl provider, passing the messages and locale as props.

import { withMessagesProvider } from '@gasket/react-intl';
import intlManager from '../path/to/intl.js';

const IntlMessagesProvider = withMessagesProvider(intlManager)(
  function CustomWrapper({ locale, messages, children }) {
    return <CustomIntlProvider messages={messages} locale={locale}>
        {children}
      </CustomIntlProvider>
  }
);

withLocaleFileRequired

Higher-order component to wrap pages or components in an app. This checks to see if a locale file has been loaded, and fetches it if not. Once loaded, the wrapped component will be rendered.

Signature

  • withLocaleFileRequired(localeFilePath, options)

Props

  • localeFilePath - (string|string[]) The locale file path to load.
  • [options] - (object) Optional configuration
    • loading - (string|node) Content to render while loading, otherwise null.
    • forwardRef - (boolean) Add a ref to the connected wrapper component.

Example

import { withLocaleFileRequired } from '@gasket/react-intl';
import { FormattedMessage } from 'react-intl';

const Component = props => <h1><FormattedMessage id='welcome'/></h1>

export default withLocaleFileRequired('/locales/extra')(Component);

LocaleFileRequired

This component can also require locale files. This can be useful for components that want to render certain content quickly, while deferring rendering other content until a dynamic locale file loads.

Signature

  • <LocaleFileRequired { ...props } />

Props

  • localeFilePath - (string|string[]) The locale file path to load.
  • loading - (string|node) Content to render while loading, otherwise null.
import { LocaleFileRequired } from '@gasket/react-intl';
import { FormattedMessage } from 'react-intl';

const Component = props => (
  <>
    <LocaleFileRequired localeFilePath='/locales'>
      <h1><FormattedMessage id='welcome'/></h1>
    </LocaleFileRequired>
    <LocaleFileRequired localeFilePath='/locales/paragraphs' loading='Loading...'>
      <p><FormattedMessage id='long-description'/></p>
    </LocaleFileRequired>
  </>
)

export default Component;

Hooks

useMessages

This hook will return an object containing all messages for the current locale.

Signature

  • useMessages(): Messages
import {
  useMessages
} from '@gasket/react-intl';

export default function MyComponent(props) {
  const messages = useMessages();

  return <p>{ messages.welcome }</p>;
}

useLocaleFile

Use this hook when you need more control versus what the components provide. The hook will return the current loading status of the dynamic locale file(s).

Signature

  • useLocaleFile(...localeFilePath): loadState

Props

import {
  useLocaleFile,
  LocaleFileStatus,
  useMessages
} from '@gasket/react-intl';

export default function MyComponent(props) {
  const status = useLocaleFile('/locales/custom');
  const messages = useMessages();
  
  if (status === LocaleFileStatus.error) return 'Could not translate.';
  if (status !== LocaleFileStatus.loaded) return 'Fetching translations...';

  return <p>{ messages.custom_welcome }</p>;
}

License

MIT

7.0.0

9 months ago

7.0.1

9 months ago

6.47.4

9 months ago

6.47.5

9 months ago

7.0.0-next.69

9 months ago

7.0.0-next.70

9 months ago

7.0.0-next.67

9 months ago

7.0.0-next.68

9 months ago

7.0.0-next.66

9 months ago

7.0.0-next.65

9 months ago

7.0.0-next.64

9 months ago

7.0.0-next.63

9 months ago

7.0.0-next.62

10 months ago

7.0.0-next.61

10 months ago

6.47.2

11 months ago

6.47.1

1 year ago

7.0.0-next.44

11 months ago

7.0.0-next.45

11 months ago

7.0.0-next.46

11 months ago

7.0.0-next.47

11 months ago

7.0.0-next.48

10 months ago

7.0.0-next.49

10 months ago

7.0.0-next.40

12 months ago

7.0.0-next.41

11 months ago

7.0.0-next.42

11 months ago

7.0.0-next.54

10 months ago

7.0.0-next.55

10 months ago

7.0.0-next.56

10 months ago

7.0.0-next.57

10 months ago

7.0.0-next.58

10 months ago

7.0.0-next.59

10 months ago

7.0.0-next.50

10 months ago

7.0.0-next.51

10 months ago

7.2.0-canary.20

1 year ago

7.0.0-next.52

10 months ago

7.0.0-next.53

10 months ago

7.0.0-next.29

1 year ago

7.2.0-canary.0

1 year ago

7.2.0-canary.1

1 year ago

7.0.0-next.28

1 year ago

7.0.0-next.32

1 year ago

7.0.0-next.33

1 year ago

7.0.0-next.34

1 year ago

7.0.0-next.37

12 months ago

7.0.0-next.38

12 months ago

7.0.0-next.39

12 months ago

7.0.1-canary.12

1 year ago

7.0.1-canary.11

1 year ago

7.0.0-next.30

1 year ago

7.0.1-canary.10

1 year ago

7.0.0-next.31

1 year ago

7.3.0-canary.2

1 year ago

7.3.0-canary.1

1 year ago

7.3.0-canary.0

1 year ago

7.3.0-canary.4

1 year ago

7.3.0-canary.3

1 year ago

7.0.0-next.60

10 months ago

7.0.0-next.26

1 year ago

7.0.0-next.27

1 year ago

7.0.0-next.25

1 year ago

7.0.0-next.22

1 year ago

7.0.0-next.23

1 year ago

6.47.0

1 year ago

7.0.0-next.21

1 year ago

7.0.0-next.9

1 year ago

7.0.0-next.10

1 year ago

7.0.0-next.7

1 year ago

7.0.0-next.11

1 year ago

7.0.0-next.8

1 year ago

7.0.0-next.12

1 year ago

7.0.0-next.13

1 year ago

7.0.0-next.6

1 year ago

7.0.0-next.20

1 year ago

7.0.0-next.5

1 year ago

6.46.8

1 year ago

7.0.0-next.4

1 year ago

7.0.0-next.3

1 year ago

7.0.0-next.2

1 year ago

7.0.0-next.1

1 year ago

7.0.0-next.0

1 year ago

6.46.7

1 year ago

7.0.0-cli.7

1 year ago

7.0.0-cli.6

1 year ago

6.46.1-cli.0

1 year ago

7.0.0-cli.5

1 year ago

7.0.0-cli.4

1 year ago

7.0.0-cli.1

1 year ago

7.0.0-cli.0

1 year ago

7.0.0-cli.3

1 year ago

7.0.0-cli.2

1 year ago

6.46.4

1 year ago

6.46.3-cli.0

1 year ago

6.46.3

1 year ago

6.46.2

1 year ago

7.0.0-canary.1

1 year ago

6.46.2-esm.0

1 year ago

6.45.2

1 year ago

6.45.0

1 year ago

6.44.2

1 year ago

6.41.2

2 years ago

6.41.1

2 years ago

6.42.0-canary.1

2 years ago

6.42.0-canary.2

2 years ago

6.42.0-canary.0

2 years ago

6.42.0

2 years ago

6.38.6

2 years ago

6.39.0

2 years ago

6.38.1

2 years ago

6.36.1

2 years ago

6.37.0

2 years ago

6.38.0

2 years ago

6.34.6

3 years ago

6.35.0

3 years ago

6.34.3

3 years ago

6.34.4

3 years ago

6.34.2

3 years ago

6.34.0

3 years ago

6.33.1

3 years ago

6.32.0

3 years ago

6.34.1

3 years ago

6.26.0

3 years ago

6.26.1

3 years ago

6.30.0

3 years ago

6.28.1

3 years ago

6.21.0

3 years ago

6.24.2

3 years ago

6.24.0

3 years ago

6.20.4

3 years ago

6.20.3

3 years ago

6.20.2

3 years ago

6.17.0

3 years ago

6.17.1

3 years ago

6.10.1

3 years ago

6.10.0

3 years ago

6.14.0

3 years ago

6.8.0

4 years ago

6.7.3

4 years ago

6.7.2

4 years ago

6.7.1

4 years ago

6.0.15

4 years ago

6.0.14

4 years ago

6.0.12

4 years ago

6.0.11

4 years ago

6.0.10

4 years ago

6.0.9

4 years ago

6.0.7

4 years ago

6.0.6

4 years ago

6.0.2

4 years ago

6.0.0

4 years ago

6.0.0-canary.13

4 years ago

6.0.0-canary.9

4 years ago

6.0.0-canary.8

4 years ago

6.0.0-canary.7

5 years ago