Git Product home page Git Product logo

react-lingua's Introduction

react-lingua Build

A simple i18n engine for React.

Installation

yarn add react-lingua
npm i react-lingua

Main goals

  • Lightweight & fast
  • No dependencies
  • Ability to dynamically change locale
  • Hooks!
  • Placeholder replacement
  • Ability to embed HTML & JSX inside translations
  • Simple namespacing (using namespaceName:translationId notation)

Usage

// src/App.js
import * as React from 'react';
import { render } from 'react-dom';
import { I18nProvider } from 'react-lingua';
import { Main } from './Main';

const translations = {
  'en-US': {
    helloWorld: 'Hello World!',
    welcome: 'Nice to meet you, {name}!',
    info: 'Example paragraph {someComp}.',
    infoPart: 'with JSX inside',
  },
  'pl-PL': {
    helloWorld: 'Witaj Świecie!',
    welcome: 'Miło Cię poznać, {name}!',
    info: 'Przykładowo paragraf {someComp}.',
    infoPart: 'z JSXem w środku',
  },
};

const App = () => (
  <I18nProvider
    initialLocale="en-US"
    translations={translations}
    onChange={(prevLocale, newLocale) => console.log(`Changed from ${prevLocale} to ${newLocale}`)}
  >
    <Main />
  </I18nProvider>
);

render(<App />, document.getElementById('app'));
// src/Main.js
import * as React from 'react';
import { Translation, useTranslation } from 'react-lingua';

export const Main = () => {
  const { t, locale, setLocale } = useTranslation();

  return (
    <main>
      <h1>{t('helloWorld')}</h1>
      <h2>{t('welcome', { name: 'Dominika' })}</h2>
      <p>
        <Translation
          id="info"
          values={{
            someComp: <strong>{t('infoPart')}</strong>
          }}
        />
      </p>
    </main>
  );
};

TODO

  • Prepare live examples (preferably on codesandbox.io)
  • Figure out the most efficient way of importing locales that are not used
  • Maybe a better directory structure
  • A bit more tests
  • A bit better typings
  • Add a few comments here and there for better DX
  • Add API section to README

react-lingua's People

Contributors

visualfanatic avatar dependabot-preview[bot] avatar dependabot[bot] avatar

Stargazers

 avatar Wojciech Baczyński avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.