Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/hyogman/react-translations-provider

React render props component for setting translations
https://github.com/hyogman/react-translations-provider

frontend props react render translation

Last synced: 3 months ago
JSON representation

React render props component for setting translations

Awesome Lists containing this project

README

        

# React Translations Provider

![download](https://user-images.githubusercontent.com/6344422/37416628-95937100-27ae-11e8-9e55-7d6a39150319.jpg)

A React render prop component for setting translations

`yarn add react-translations-provider`

## Example

```js
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 (

{({ setLanguage, language }) => (


setLanguage(e.target.value)}>
English
German
Spanish




)}

);
}

function DisplayStuff() {
// For the rest of app simply wrap with the Translate component to get translations
return (

{({ translations }) => (


{translations.hello}


{translations.working}



{moment()
.locale(translations.locale)
.format("L")}



)}

);
}

function SimpleWeather() {
// Or use the Text component api and pass the key path in translateKey prop to return
// translation from translations object
return (









);
}

export default App;
```

![demo3](https://user-images.githubusercontent.com/6344422/36433396-adf62034-165c-11e8-9a15-cbb61ea330d2.gif)