# @translator-app/react

> The translator app react library, is a react context that creates a `@translator-app/core` instance and adds some utilities on top to make it easier to use with react. Such as asynchronously loading translation files and simplifying the overall API of cor

Latest version **0.1.0** (published 2020-09-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @translator-app/react
pnpm add @translator-app/react
yarn add @translator-app/react
bun add @translator-app/react
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2020-09-29 |
| First published | 2020-09-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 13.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | jasperdm |

## Links

- npm: https://www.npmjs.com/package/@translator-app/react
- npm.io page: https://npm.io/package/@translator-app/react

## Dependencies (1)

- [@translator-app/core](https://npm.io/package/@translator-app/core.md) ^0.1.0

## Recent versions

- 0.1.0 (latest) — 2020-09-29
- 0.0.10 — 2020-09-12
- 0.0.9 — 2020-09-12
- 0.0.8 — 2020-09-12
- 0.0.7 — 2020-09-12
- 0.0.6 — 2020-09-12

## README

# @translator-app/react

The translator app react library, is a react context that creates a `@translator-app/core` instance and adds some utilities on top to make it easier to use with react. Such as asynchronously loading translation files and simplifying the overall API of core into provider properties and callback functions.

## Usage

To use this library you can import `TranslatorProvider`, `useTranslator` and/or `TranslatorConsumer`.

The Provider takes in the following properties:

```tsx
// React child nodes
children: React.ReactNode;
// The currently displayed language
language: string;
// A language to fallback to, if non is defined it will fallback to the key name
fallbackLanguage?: string;
// An object that contains a library of translations with the key being the language key and value an object of translation templates
library?: RawTranslationLibrary;
// A function that gets called if it exists and the defined language does not exist in the library. It should return the translation file for that language
getTranslationRegistry?: (language: string) => Promise<RawTranslationRegistry> | RawTranslationRegistry;
```

Provider values (the values you receive when using `useTranslator` and/or `TranslatorConsumer`):

```tsx
// The currently configured language, same as provided property...
language: string;
// A function to translate a key with an optional values field, values can be an object or array. Always returns a string.
translate: (key: string, values?: any) => string;
// The current version of the library, this is just a count of the amount of times getTranslationRegistry is called
version: number;
```

## Example

```tsx
import React from 'react';

const Example = () => {
  let { translate } = useTranslator();

  return <div>{translate('test', { name: 'Bob' })}</div>;
};

const App = () => {
  let { translationKey, values } = props;
  let { translate } = useTranslator();

  return (
    <TranslatorProvider
      language="nl"
      fallbackLanguage="en"
      library={{
        en: {
          test: 'This is a test value',
        },
      }}
      // This can also return a promise (or be an async function)
      getTranslationRegistry={(language) => {
        if (language !== 'nl') {
          throw new Error('This should not be requested...');
        }

        return {
          test: 'Dit is een test waarde',
        };
      }}
    >
      <Example translationKey="test" />
    </TranslatorProvider>
  );
};
```

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