All Projects โ†’ amsul โ†’ React Translated

amsul / React Translated

Licence: mit
A dead simple way to add complex translations (i18n) in a React (DOM/Native) project ๐ŸŒŽ๐ŸŒ๐ŸŒ

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Translated

React Intl Hooks
React hooks for internationalization without the hassle โš›๏ธ๐ŸŒ
Stars: โœญ 64 (-63.64%)
Mutual labels:  translation, i18n, localization, internationalization, translate
Easy localization
Easy and Fast internationalizing your Flutter Apps
Stars: โœญ 407 (+131.25%)
Mutual labels:  translation, i18n, localization, internationalization
React Localize Redux
Dead simple localization for your React components
Stars: โœญ 384 (+118.18%)
Mutual labels:  translation, i18n, localization, internationalization
Traduora
Everยฎ Traduora - Open-Source Translation Management Platform
Stars: โœญ 1,580 (+797.73%)
Mutual labels:  translation, localization, internationalization, i18n
Eo Locale
๐ŸŒInternationalize js apps ๐Ÿ‘”Elegant lightweight library based on Internationalization API
Stars: โœญ 290 (+64.77%)
Mutual labels:  translation, i18n, localization, internationalization
Js Lingui
๐ŸŒ๐Ÿ“– A readable, automated, and optimized (5 kb) internationalization for JavaScript
Stars: โœญ 3,249 (+1746.02%)
Mutual labels:  translation, i18n, localization, internationalization
Phabricator zh hans
Phabricator zh-Hans Translation & Tools.
Stars: โœญ 113 (-35.8%)
Mutual labels:  translation, i18n, localization, internationalization
plate
Internationalization library for Python
Stars: โœญ 31 (-82.39%)
Mutual labels:  i18n, translation, internationalization, localization
Fluent
Fluent โ€” planning, spec and documentation
Stars: โœญ 818 (+364.77%)
Mutual labels:  translation, localization, internationalization, translate
Texterify
The localization management system.
Stars: โœญ 37 (-78.98%)
Mutual labels:  i18n, localization, internationalization, translate
Mojito
An automation platform that enables continuous localization.
Stars: โœญ 256 (+45.45%)
Mutual labels:  translation, i18n, localization, internationalization
Formatjs
The monorepo home to all of the FormatJS related libraries, most notably react-intl.
Stars: โœญ 12,869 (+7211.93%)
Mutual labels:  translation, i18n, localization, internationalization
labels
Bolt Labels extension - Translatable labels for Bolt
Stars: โœญ 18 (-89.77%)
Mutual labels:  i18n, translation, internationalization, localization
Pseudo Localization
Dynamic pseudo-localization in the browser and nodejs
Stars: โœญ 109 (-38.07%)
Mutual labels:  translation, i18n, localization, internationalization
react-translator-component
React language translation module for developing a multilingual project.
Stars: โœญ 13 (-92.61%)
Mutual labels:  internationalization, localization, react-component, translate
Fluent.js
JavaScript implementation of Project Fluent
Stars: โœญ 622 (+253.41%)
Mutual labels:  translation, i18n, localization, internationalization
inlang
Open Source Localization Solution for Software.
Stars: โœญ 160 (-9.09%)
Mutual labels:  i18n, translation, internationalization, localization
rosetta
A blazing fast internationalization (i18n) library for Crystal with compile-time key lookup.
Stars: โœญ 23 (-86.93%)
Mutual labels:  i18n, translation, internationalization, localization
Frenchkiss.js
The blazing fast lightweight internationalization (i18n) module for javascript
Stars: โœญ 776 (+340.91%)
Mutual labels:  translation, i18n, localization, internationalization
Dom I18n
Provides a very basic HTML multilingual support using JavaScript
Stars: โœญ 125 (-28.98%)
Mutual labels:  translation, i18n, localization, internationalization

react-translated

A dead simple way to add complex translations in a React project ๐ŸŒŽ๐ŸŒ๐ŸŒ

Features

  • ๐Ÿ’ฅ Data interpolation
  • โ˜„ Component interpolation
  • โ“‚ Markdown inline-manipulations
  • ๐Ÿ”€ Custom manipulations, pluralizations, and grammar rules based on input-data
  • โš› Component-level translation files (enables loading only required translations)

Example

Write this:

<Translate
  text="{difficulty} *translations* in React <ReactLogo>"
  data={{ difficulty: 'Simple' }}
  renderMap={{
    renderReactLogo: () => <ReactLogo size={14} />,
  }}
/>

To render this:

Support

React DOM and React Native ๐Ÿ”ฅ

Try

Play around with the library in your browser through the CodeSandbox.


Table of Contents


Installation

Whatever floats your boat:

  • Yarn: yarn add react-translated
  • npm: npm install react-translated

Setup

Step 1: Create the translations file

Create a file that will contain a mapping of keys to the string in each language you support.

To keep things simple, use the strings of your default language as the key:

// translation.js

export default {
  'Hi, World!': {
    en: 'Hi, World!',
    fr: 'Bonjour le monde!',
  },
  // ...
}

NOTE: There is no enforcement on the key used for a language. In these examples, 2-digit country codes (en, fr, etc) are used. Decide on a convention and use that for all translations.

Step 2: Connect the Provider

Wrap your top-level component with the <Provider> and set the translation and language props:

// index.js

import { Provider } from 'react-translated'
import translation from './translation'
const App = (
  <Provider language="en" translation={translation}>
    <MyApplicationRoot />
  </Provider>
)

NOTE: The value of the language prop must be one of the keys used for a language defined in Step 1.

Step 3: Start translating

That is all!

Continue reading below to see how to handle the various translation scenarios.


Usage

The library can be imported in whatever way you find suitable:

import ReactTranslated from 'react-translated'
import * as ReactTranslated from 'react-translated'

<ReactTranslated.Translate /*...*/ />

Or:

import { Provider, Translate, Translator } from 'react-translated'

<Translate /*...*/ />

Translate vs Translator

The Translate component should always be used when the translation is rendered as a child component; such as buttons, paragraphs, headings, etc.

The Translator component should only be used when the translation is needed as a string; such as placeholders, alternate text, etc.

Translation scenarios


Static text

This is pretty self-explanatory:

// translation.js
export default {
  'Hi, World!': {
    en: 'Hi, World!',
    fr: 'Bonjour le monde!',
  },
}

// any component file
<Translate text='Hi, World!' />

Renders as:

Templated text

To use dynamic text, the text can be templated:

// translation.js
export default {
  'Hi, {firstName}!': {
    en: 'Hi, {firstName}!',
    fr: 'Salut {firstName}!',
  },
}

// any component file
<Translate
  text='Hi, {firstName}!'
  data={{ firstName: 'Sergey' }}
  />

Renders as:

Dynamically templated text

Sometimes just dynamic text is not enough and the template itself needs to be dynamic (for example pluralization). That can be achieved using a function call:

// translation.js
export default {
  'There are {catsCount} cats in this room.': {
    en({ catsCount }) {
      if (catsCount === 1) {
        return 'There is {catsCount} cat in this room.'
      }
      return 'There are {catsCount} cats in this room.'
    },
    // ...
  },
}

// any component file
<Translate
  text='There are {catsCount} cats in this room.'
  data={{ catsCount: 2 }}
  />
<Translate
  text='There are {catsCount} cats in this room.'
  data={{ catsCount: 1 }}
  />

Renders as:

Since these templates are simple function calls, things more complex than pluralization can be done too:

// translation.js
export default {
  'This is a {fruit}': {
    en({ fruit }) {
      if (/^[aeiou]/.test(fruit)) {
        return 'This is an {fruit}'
      }
      return 'This is a {fruit}'
    },
    // ...
  },
}

// any component file
<Translate
  text='This is a {fruit}'
  data={{ fruit: 'banana' }}
  />
<Translate
  text='This is a {fruit}'
  data={{ fruit: 'apple' }}
  />

Renders as:

Styled text

The translated text can also have some basic styling applied:

// translation.js
export default {
  'Hi, *World*!': {
    en: 'Hi, *World*!',
    fr: 'Bonjour *le monde*!',
  },
}

// any component file
<Translate text='Hi, *World*!' />

Renders as:

And of course the same can be done with dynamic templates:

// translation.js
export default {
  'Hi, *{firstName}*!': {
    en: 'Hi, *{firstName}*!',
    fr: 'Salut *{firstName}*!',
  },
}

// any component file
<Translate
  text='Hi, *{firstName}*!'
  data={{ firstName: 'Sergey' }}
  />

Renders as:

Component within text

For more advanced uses where Markdown and Emojis donโ€™t suffice, components can be rendered within the text:

// translation.js
export default {
  'Tap the <StarIcon> to add': {
    en: 'Tap the <StarIcon> to add',
    fr: 'Appuyez sur la <StarIcon> pour ajouter',
  },
}

// any component file
<Translate
  text='Tap the <StarIcon> to add!'
  renderMap={{
    renderStarIcon: () => <StarIcon size={14} />
  }}
  />

Renders as:

Another practical application of this is nested translations - text that requires data that also needs to be translated:

// translation.js
export default {
  'I was born in <MonthName>': {
    en: 'I was born in <MonthName>',
    fr: 'Je suis nรฉ en <MonthName>',
  },
  'August': {
    en: 'August',
    fr: 'aoรปt',
  },
}

// any component file
const monthName = 'August'
<Translate
  text='I was born in <MonthName>'
  renderMap={{
    renderMonthName: () => <Translate text={monthName} />
  }}
  />

Renders as:


Translated text as string

Added v2.2.0

In scenarios where the translated text is required as a string, such as with text inputs placeholders or accessibility labels, the Translator can be used:

// translation.js
export default {
  'Enter your age {firstName}': {
    en: 'Enter your age {firstName}',
    fr: 'entrez votre รขge {firstName}',
  },
}

// any component file
<Translator>
  {({ translate }) => (
    <input
      placeholder={translate({
        text: 'Enter your age {firstName}',
        data: { firstName: 'Sergey' },
      })}
      />
  )}
</Translator>

Renders as:


Contributors


amsul

๐Ÿ’ป ๐ŸŽจ ๐Ÿ“– ๐Ÿ’ก ๐Ÿ”ง

Johnson Su

๐Ÿ› ๐Ÿ’ป

๐Ÿ‘‹ Interested becoming a contributor too?

Awesome! This project follows the all-contributors specification. Contributions of any kind are welcome!

You may also want to take a look at our TODOs below and make sure to give our Contributing guide a read.


TODOs

  • Add tests using Jest

License

Licensed under MIT.

ยฉ 2019 Amsul

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].