# react-polyglot

> Higher order React component for using Polyglot

Latest version **0.7.2** (published 2021-07-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-polyglot
pnpm add react-polyglot
yarn add react-polyglot
bun add react-polyglot
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.7.2 |
| Published | 2021-07-25 |
| First published | 2016-07-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 872.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 55 |
| Author | Nayaabkhan Khan |
| Maintainers | nayaabkhan |
| Keywords | react, airbnb, polyglot, i18n |

## Links

- npm: https://www.npmjs.com/package/react-polyglot
- Repository: https://github.com/nayaabkhan/react-polyglot
- Homepage: https://github.com/nayaabkhan/react-polyglot#readme
- Issues: https://github.com/nayaabkhan/react-polyglot/issues
- npm.io page: https://npm.io/package/react-polyglot

## Dependencies (2)

- [prop-types](https://npm.io/package/prop-types.md) ^15.5.8
- [hoist-non-react-statics](https://npm.io/package/hoist-non-react-statics.md) ^3.3.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.7.2 (latest) — 2021-07-25
- 0.7.1 — 2021-03-11
- 0.7.0 — 2020-05-21
- 0.6.1 — 2019-10-16
- 0.6.0 — 2019-10-01
- 0.5.0 — 2019-08-31
- 0.4.2 — 2019-08-09
- 0.4.1 — 2019-07-10
- 0.4.0 — 2019-03-06
- 0.2.6 — 2018-04-30
- 0.2.5 — 2017-10-11
- 0.2.4 — 2017-09-24
- 0.2.3 — 2017-06-20
- 0.2.2 — 2017-01-16
- 0.2.1 — 2016-10-04
- … 2 more at https://npm.io/package/react-polyglot/versions

## README

<a href="https://codeclimate.com/github/nayaabkhan/react-polyglot/maintainability"><img src="https://api.codeclimate.com/v1/badges/fd8c57e662c5f08ba77e/maintainability" /></a>
<a href="https://codeclimate.com/github/nayaabkhan/react-polyglot/test_coverage"><img src="https://api.codeclimate.com/v1/badges/fd8c57e662c5f08ba77e/test_coverage" /></a>
<a href="https://travis-ci.org/nayaabkhan/react-polyglot"><img src="https://travis-ci.org/nayaabkhan/react-polyglot.svg?branch=master" /></a>
<a href="https://bundlephobia.com/result?p=react-polyglot"><img src="https://badgen.net/bundlephobia/min/react-polyglot" /></a>
<a href="https://bundlephobia.com/result?p=react-polyglot"><img src="https://badgen.net/bundlephobia/minzip/react-polyglot" /></a>

# React Polyglot
Provides higher order component for using Polyglot with React

## Installation

```
npm install --save react-polyglot
```

## Usage

`react-polyglot` exports consists for one wrapper component called `I18n`, one decorator called
`translate` and one hook called `useTranslate`. The decorator provides a prop `t` which is instance of `Polyglot`.

You are required to wrap your root component with `I18n` and pass on a `locale` like `en` or `fr`.
And `messages` object containing the strings.

```js
import React from 'react';
import { render } from 'react-dom';
import { I18n } from 'react-polyglot';
import App from './components/app';

const locale = window.locale || 'en';
const messages = {
  "hello_name": "Hello, %{name}.",
  "num_cars": "%{smart_count} car |||| %{smart_count} cars",
}

render(
  <I18n locale={locale} messages={messages}>
    <App />
  </I18n>,
  document.getElementById('app')
);
```

Then inside `App` or a child component of `App` you can do:

```js
import React from 'react';
import { translate } from 'react-polyglot';

const Greeter = ({ name, t }) => (
  <h3>{t('hello_name', { name })}</h3>
);

Greeter.propTypes = {
  name: React.PropTypes.string.isRequired,
  t: React.PropTypes.func.isRequired,
};

export default translate()(Greeter);
```


or with React Hooks:

```js
import React from 'react';
import { useTranslate } from 'react-polyglot';

export default const Greeter = ({ name }) => {
  const t = useTranslate();

  return (
    <h3>{t('hello_name', { name })}</h3>
  );
};

Greeter.propTypes = {
  name: React.PropTypes.string.isRequired
};

```


## Live Examples

### Minimal example using class components

https://codesandbox.io/s/mq76ojk228

### Advance example with user changeable locales

https://codesandbox.io/s/px8n63v0m


## How to provide context in your tests

Use a simple helper to wrap your components in a context.

```js
export const wrapWithContext = function (component, context, contextTypes) {
  const wrappedComponent = React.createClass({
    childContextTypes: contextTypes,
    getChildContext() {
      return context;
    },
    render() {
      return component;
    },
  });
  return React.createElement(wrappedComponent);
}
```

Then use it inside your tests.

```js
import React from 'react';
import { renderToString } from 'react-dom/server';
import Polyglot from 'node-polyglot';
import Greeter from './greeter';
import { wrapWithContext } from './helpers';

const polyglot = new Polyglot({
  locale: 'en',
  phrases: {"hello_name": "Hello, %{name}."},
});

const greeterWithContext = wrapWithContext(
  <Greeter name="Batsy" />,
  { t: polyglot.t.bind(polyglot) },
  { t: React.PropTypes.func }
);

// use greeterWithContext in your tests
// here it is shown how to use it with renderToString
console.log(renderToString(greeterWithContext));
```


## Release History

Check the [Releases](https://github.com/nayaabkhan/react-polyglot/releases) tab.

---
_Source: https://npm.io/package/react-polyglot · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
