Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/alii/react-text-translator

An experimental way to translate text inside React components with context
https://github.com/alii/react-text-translator

context i18n react text translate

Last synced: 15 days ago
JSON representation

An experimental way to translate text inside React components with context

Awesome Lists containing this project

README

        

# `react-text-translator`

A typed way to translate react apps with context

Firstly, define your translator as follows

```tsx
import {createTranslations} from 'react-text-translator';

export const translator = createTranslations({
'Hello World': {
SPANISH: 'Hola mundo',
BRITISH: 'Hello World',
},
'Contact Us': {
BRITISH: 'Contact Us',
SPANISH: 'Contáctanos',
},
});

export const Text = translator.Text;
```

Second, initialize your context and provide state for the active language

```tsx
import {translator} from './translator';

function LangWrapper() {
const [lang, setLang] = useState('BRITISH');

return (



);
}
```

Then, you are able to use the `` component as you would any other by passing a string as it's children (and only a string).

````tsx
import {Text} from './translator';

export default function Child() {
return (


Hello World
Contact Us

);
}```
````