rtp
react-translations-provider
npm i react-translations-provider
rtp

react-translations-provider

React render props component for setting translations

by Henry Yogman

1.1.2 (see all)License:MITTypeScript:Not Found
npm i react-translations-provider
Readme

React Translations Provider

A React render prop component for setting translations

yarn add react-translations-provider

Example

import React from "react";
import TranslationProvider, {
  Translate,
  Text,
} from "react-translations-provider";
import moment from "moment";

require("moment/locale/de");
require("moment/locale/es");

const en = {
  locale: "en",
  hello: "Hello World!",
  working: "Is this working?",
  weather: {
    weather: "Weather",
    sunny: "sunny",
    cloudy: "cloudy",
  },
};

const de = {
  locale: "de",
  hello: "Hallo Welt!",
  working: "Funktioniert das?",
  weather: {
    weather: "Wetter",
    sunny: "sonnig",
    cloudy: "bewölkt",
  },
};

const es = {
  locale: "es",
  hello: "¡Hola Mundo!",
  working: "¿Esto funciona?",
  weather: {
    weather: "Clima",
    sunny: "soleado",
    cloudy: "nublado",
  },
};

const translations = {
  en,
  de,
  es,
};

function App() {
  // At root level of app set the language with the TranslationProvider
  return (
    <TranslationProvider translations={translations} default="en">
      {({ setLanguage, language }) => (
        <div>
          <select value={language} onChange={e => setLanguage(e.target.value)}>
            <option value="en">English</option>
            <option value="de">German</option>
            <option value="es">Spanish</option>
          </select>
          <DisplayStuff />
          <SimpleWeather />
        </div>
      )}
    </TranslationProvider>
  );
}

function DisplayStuff() {
  // For the rest of app simply wrap with the Translate component to get translations
  return (
    <Translate>
      {({ translations }) => (
        <div>
          <h1>{translations.hello}</h1>
          <h2>{translations.working}</h2>
          <h3>
            {moment()
              .locale(translations.locale)
              .format("L")}
          </h3>
        </div>
      )}
    </Translate>
  );
}

function SimpleWeather() {
  // Or use the Text component api and pass the key path in translateKey prop to return
  // translation from translations object
  return (
    <div>
      <h1>
        <Text translateKey="weather.weather" />
      </h1>
      <h3>
        <Text translateKey="weather.sunny" />
      </h3>
    </div>
  );
}

export default App;

demo3

Downloads/wk

1

GitHub Stars

35

LAST COMMIT

5yrs ago

MAINTAINERS

1

CONTRIBUTORS

0

OPEN ISSUES

0

OPEN PRs

0
VersionTagPublished
1.1.2
latest
5yrs ago
No alternatives found
No tutorials found
Add a tutorial

Rate & Review

100
No reviews found
Be the first to rate