New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

react-lingua

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-lingua

A simple i18n engine for React

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9
increased by200%
Maintainers
1
Weekly downloads
 
Created
Source

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 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 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

Keywords

FAQs

Package last updated on 08 Jun 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc