# @traduki/react

> Traduki React helpers

Latest version **0.11.0** (published 2021-08-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install @traduki/react
pnpm add @traduki/react
yarn add @traduki/react
bun add @traduki/react
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.11.0 |
| Published | 2021-08-04 |
| First published | 2020-07-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 14.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | havelaer |
| Maintainers | havelaer |
| Keywords | messageformat, pluralformat, icu, i18n, L10n, code-splitting, translations, react |

## Links

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

## Dependencies (1)

- [@traduki/runtime](https://npm.io/package/@traduki/runtime.md) ^0.11.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.11.0 (latest) — 2021-08-04
- 0.8.0 (0.8.0-beta.1) — 2021-01-15
- 0.10.5 — 2021-06-28
- 0.10.2 — 2021-06-05
- 0.10.1 — 2021-05-10
- 0.10.0 — 2021-05-08
- 0.9.1 — 2021-01-16
- 0.7.0 — 2020-09-06
- 0.6.5 — 2020-08-30
- 0.6.3 — 2020-08-23
- 0.6.2 — 2020-08-22
- 0.6.0 — 2020-08-22
- 0.5.3 — 2020-08-19
- 0.5.2 — 2020-08-15
- 0.5.1 — 2020-08-14
- … 8 more at https://npm.io/package/@traduki/react/versions

## README

# @traduki/react

[![npm](https://img.shields.io/npm/v/@traduki/react.svg)](https://www.npmjs.com/package/@traduki/react)

See main github repository [readme.md](https://github.com/havelaer/traduki)

## Examples

- [Webpack example](https://github.com/havelaer/traduki/tree/master/examples/webpack-react-app-setup)
- [Vite example](https://github.com/havelaer/traduki/tree/master/examples/vite-react-app-setup)

## Install

- For Webpack see the: [Traduki Webpack plugin](https://github.com/havelaer/traduki/blob/master/packages/webpack-plugin/README.md)
- For Vite see the: [Traduki Vite plugin](https://github.com/havelaer/traduki/blob/master/packages/vite-plugin/README.md)

## Usage

### \<TradukiProvider />

Make sure to wrap you application in the `TradukiProvider`:

```js
import { TradukiProvider } from '@traduki/react';
import App from './App';

render(
    <TradukiProvider initialLocale="en">
        <App />
    </TradukiProvider>
    document.getElementById('root'),
);
```

### useTranslator

Use the `useTranslator` React hook to translate messages.

```js
import { useTranslator } from '@traduki/react';
import messages from './Component.messages.yaml';

function Component() {
    const t = useTranslator();

    return (
        <div>
            <p>{t(messages.welcome)}</p>
        </div>
    );
}

export default Component;
```

### useLocale

Read the current locale or switch to another locale with the `useLocale` React hook.

```js
import { useLocale } from '@traduki/react';
import messages from './Component.messages.yaml';

function Component() {
    const [locale, setLocale] = useLocale();

    return (
        <div>
            <p>Current locale: {locale}</p>
            <p>
                <button onClick={() => setLocale('en-US')}>en</button>
                <button onClick={() => setLocale('nl-NL')}>nl</button>
            </p>
        </div>
    );
}

export default Component;
```

### waitForMessages

Traduki is build with code splitting in mind. The preact package provides a `waitForMessages` function.

Use `waitForMessages` when using `lazy` to make sure the chunk's messages are also loaded before rendering the chunk's component. This is prevent a flash of unlocalized texts.

```js
// AsyncComponent.js
import { useTranslator } from '@traduki/preact';
import messages from './AsyncComponent.messages.yaml';

function AsyncComponent() {
    const t = useTranslator();

    return <div>{t(messages.hello)}</div>;
}

export default Component;


// Component.js
import { lazy } from 'react';
import { waitForMessages, useTranslator } from '@traduki/react';
import messages from './Component.messages.yaml';

const AsyncComponent = lazy(() => import('./AsyncComponent').then(waitForMessages));

function Component() {
    const t = useTranslator();

    return (
        <div>
            <Suspense fallback={<div>{t(messages.loading)}</div>}>
                <AsyncComponent />
            </Suspense>
        </div>
    );
}

export default Component;

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