Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/houhoucoop/react-i18n

i18n for React handling translations
https://github.com/houhoucoop/react-i18n

Last synced: 9 days ago
JSON representation

i18n for React handling translations

Awesome Lists containing this project

README

        

# react-i18n

> i18n utilities for React handling translations.

[![NPM](https://img.shields.io/npm/v/react-i18n.svg)](https://www.npmjs.com/package/react-i18n) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

## Install

```bash
npm install --save react-i18n
```

## Example
```jsx
import { IntlProvider, useIntl } from 'react-i18n'

const translations = {
EN: {
sample: {
hello: 'Hello World!'
},
},
FR: {
sample: {
hello: 'Bonjour Monde!'
},
},
};

const Example = () => (



)

const MyComponent = () => {
const { t, setLocale } = useIntl()

return (
<>

{t('sample.hello')}


setLocale('fr')}>Switch to `FR`
>
)
}
```
## Usage
### IntlProvider

> Component used to provide i18n context to child components.

- `locale` - The locale language, it will be converted to uppercase in order to matches the keys in translations.
- `translations` - This should be an object
where keys are locales, and values are maps of message keys to translated
strings.

### useIntl

> Provides a React hook which lets you
call into the translate function directly.

- `t` - The locale to translate things into.
- `setLocale` - Set current locale to another.

```jsx
import { Intl } from 'react-i18n'

const Component = () => {
const { t, setLocale } = useIntl()
return (
<>

{t('sample.hello')}


setLocale('fr')}>Switch to `FR`
>
);
}
```
### Intal

> Get the translate function in render props way.

- `t` - The locale to translate things into.
- `setLocale` - Set current locale to another.

```jsx
import { useIntl } from 'react-i18n'

const Component = () => (

{({ t, setLocale }) => {
return (

{t('sample.hello')}


setLocale('fr')}>Switch to `FR`
)
}}

)
```
## License

MIT © [@houhoucoop](https://github.com/@houhoucoop)