@ff2005/react-translations
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

react-translations

MIT License React Typescript

Simple library for react translation with hooks (and typescript).

Installation

npm install @ff2005/react-translations

Usage

App.js

import { TranslationsProvider } from '@ff2005/react-translations';
import Page from "./Page";
import en from './en.json';
import fr from './fr.json';

function App() {
  return (
    <TranslationsProvider translations={{ en, fr }}>
      <Page />
    </TranslationsProvider>
  );
}

export default App;

Page.js

import { useTranslation, useTranslationLanguage } from "@ff2005/react-translations";

function Page() {
  const [translationLanguage, setTranslationLanguage] = useTranslationLanguage();
  const t = useTranslation();

  setTimeout(() => {
    setTranslationLanguage({
      languageTag: 'fr',
      isRTL: false
    })
  }, 3000);

  return (
    <>
      <div>{t('app.title')}</div>
      <div>{JSON.stringify(translationLanguage)}</div>
    </>
  );
}

export default Page;

en.json

{
  "app": {
    "title": "My Application"
  }
}

fr.json

{
  "app": {
    "title": "Mon application"
  }
}

TranslationsProvider attributes

  • translations - Object with each json language
  • fallback - Object for the fallback language (ex: { languageTag: 'en', isRTL: false })

Contributing

Contributions are very welcome. Please check out the contributing document.

License

MIT.

Package Sidebar

Install

npm i @ff2005/react-translations

Weekly Downloads

1

Version

0.0.2

License

MIT

Unpacked Size

46.4 kB

Total Files

14

Last publish

Collaborators

  • ff2005