2.1.0 • Published 6 years ago

react-translate-json v2.1.0

Weekly downloads
25
License
ISC
Repository
github
Last release
6 years ago

Make react speak your language.

npm version codecov.io Code Coverage Build Status

Overview

Instalation

npm install --save react-translate-json
# or
yarn add react-translate-json

Getting started

Just think of it in these steps:

  1. Define your translation directory
  2. Define the current user locale code, e.g. 'en', 'de', 'fr' and so on.
  3. Optionally use a fallback language code. Usually 'en'.

Then, use the thin, built-in component, to translate your strings.

Example

The following examples are based on create-react-app results.

// This is our json file, with translations
{
  "HELLO": "Hi {{user}}!",
  "PAGES": {
    "HOME": {
      "TITLE": "Home Page Title"
    }
  }
}
// This is usually our index.js
import React from 'react';
import ReactDOM from 'react-dom';
// Import the TranslateProvider function to provide the service with your settings
import { TranslateProvider } from 'react-translate-json';
import App from './App';

const translationOptions = {
    pathPrefix: '/translations', // Path to your translations
    locale: 'en', // User current locale
    fallbackLocale: 'en' // Fallback locale
};

// That's it! You are all set!

ReactDOM.render(
    <TranslateProvider {...translationOptions}>
        <App />
    </TranslateProvider>,
    document.getElementById('root')
);

Now, you can easily add in your translations by importing the component.

// App.js
import React, { Component } from 'react';
import { TranslateComponent } from 'react-translate-json/react';

class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          
          {/* Render-prop based */}
          <TranslateComponent label="PAGES.HOME.TITLE" render={(res) => (
            <h1 className="App-title">{res}</h1>
          )}/>
          
          {/* Regular component usage */}
          <h2 className="greet">
              <TranslateComponent label="HELLO" params={{user: 'John'}}/>
          </h2>

        </header>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
    );
  }
}

export default App;

Preact

// App.js
import { Component, h } from 'preact';
import { TranslateComponent } from 'react-translate-json/preact';

class App extends Component {
  render() {
    return (
      <div className="App">
          <h2 className="greet">
              <TranslateComponent label="HELLO" params={{user: 'John'}}/>
          </h2>
      </div>
    );
  }
}

export default App;