All Projects β†’ yankouskia β†’ Localize React

yankouskia / Localize React

Licence: mit
✈️ Lightweight React Localization Library πŸ‡ΊπŸ‡Έ

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Localize React

translation
πŸ‘… Translations (symfony/translation) to Nette Framework (@nette)
Stars: ✭ 55 (+5.77%)
Mutual labels:  localization, l10n
i18n
Package i18n is for app Internationalization and Localization.
Stars: ✭ 79 (+51.92%)
Mutual labels:  localization, l10n
rosetta
A blazing fast internationalization (i18n) library for Crystal with compile-time key lookup.
Stars: ✭ 23 (-55.77%)
Mutual labels:  localization, l10n
i18n
internationalize projects to Arabic
Stars: ✭ 67 (+28.85%)
Mutual labels:  localization, l10n
Laravel Js Localization
🌐 Convert your Laravel messages and consume them in the front-end!
Stars: ✭ 451 (+767.31%)
Mutual labels:  localization, l10n
inlang
Open Source Localization Solution for Software.
Stars: ✭ 160 (+207.69%)
Mutual labels:  localization, l10n
python-fluent
Python implementation of Project Fluent
Stars: ✭ 142 (+173.08%)
Mutual labels:  localization, l10n
awesome-translations
😎 Awesome lists about Internationalization & localization stuff. l10n, g11n, m17n, i18n. Translations! 🌎🌍
Stars: ✭ 54 (+3.85%)
Mutual labels:  localization, l10n
Tower
i18n & L10n library for Clojure/Script
Stars: ✭ 264 (+407.69%)
Mutual labels:  localization, l10n
Mojito
An automation platform that enables continuous localization.
Stars: ✭ 256 (+392.31%)
Mutual labels:  localization, l10n
go-locale
GoLang library used to retrieve the current locale(s) of the operating system.
Stars: ✭ 16 (-69.23%)
Mutual labels:  localization, l10n
Fluent.js
JavaScript implementation of Project Fluent
Stars: ✭ 622 (+1096.15%)
Mutual labels:  localization, l10n
lisan
🌈i18n, Reimagined! πŸš€A blazing fast and super small i18n library for Javascript
Stars: ✭ 85 (+63.46%)
Mutual labels:  localization, l10n
Angular-Gulp-Boilerplate
Clean but full-featured AngularJS boilerplate using Gulp workflow and best practices
Stars: ✭ 30 (-42.31%)
Mutual labels:  localization, l10n
stone.js
gettext-like client-side Javascript Internationalization Library
Stars: ✭ 20 (-61.54%)
Mutual labels:  localization, l10n
i18n-tag-schema
Generates a json schema for all i18n tagged template literals in your project
Stars: ✭ 15 (-71.15%)
Mutual labels:  localization, l10n
android-studio-plugin
Integrate your Android project with Crowdin
Stars: ✭ 52 (+0%)
Mutual labels:  localization, l10n
I18N
I18N Library for .NET, and Delphi
Stars: ✭ 48 (-7.69%)
Mutual labels:  localization, l10n
labels
Bolt Labels extension - Translatable labels for Bolt
Stars: ✭ 18 (-65.38%)
Mutual labels:  localization, l10n
Fluent Rs
Rust implementation of Project Fluent
Stars: ✭ 503 (+867.31%)
Mutual labels:  localization, l10n

CircleCI Codecov Coverage PRs Welcome GitHub license GitHub stars

NPM

localize-react

✈️ Lightweight React Localization Library πŸ‡ΊπŸ‡Έ

Motivation

Creating really simple lightweight library for localization in React applications without any dependencies, which is built on top of new React Context Api

Library has just 737 Bytes gzipped size

Installation

npm:

npm install localize-react --save

yarn:

yarn add localize-react

API

Provider & Consumer

LocalizationProvider is used to provide data for translations into React context. The root application component should be wrapped into LocalizationProvider. Component has the next props:

  • children - children to render
  • locale - [OPTIONAL] locale to be used for translations. If locale is not specified regular translations object will be used as map of { key: translations }
  • translations - object with translations
  • disableCache - boolean variable to disable cache on runtime (false by default). Setting this to true could affect runtime performance, but could be useful for development.

Example:

import React from 'react';
import ReactDOM from 'react-dom';
import { LocalizationConsumer, LocalizationProvider } from 'localize-react';

const TRANSLATIONS = {
  en: {
    name: 'Alex',
  },
};

const App = () => (
  <LocalizationProvider
    disableCache
    locale="en"
    translations={TRANSLATIONS}
  >
    <LocalizationConsumer>
      {({ translate }) => translate('name')}
    </LocalizationConsumer>
  </LocalizationProvider>
);

ReactDOM.render(<App />, node); // "Alex" will be rendered

Message

Message component is used to provide translated message by specified key, which should be passed via props. Component has the next props:

  • descriptor - translation key (descriptor)
  • defaultMessage - message to be used in case translation is not provided (values object are applied to default message as well)
  • values - possible values to use with template string (Template should be passed in next format: Hello {{name}})

Example:

import React from 'react';
import ReactDOM from 'react-dom';
import { LocalizationProvider, Message } from 'localize-react';

const TRANSLATIONS = {
  en: {
    name: 'Alex',
  },
};

const App = () => (
  <LocalizationProvider
    locale="en"
    translations={TRANSLATIONS}
  >
    <Message descriptor="name" />
  </LocalizationProvider>
);

ReactDOM.render(<App />, node); // "Alex" will be rendered

To use with templates:

import React from 'react';
import ReactDOM from 'react-dom';
import { LocalizationProvider, Message } from 'localize-react';

const TRANSLATIONS = {
  en: {
    name: 'Hello, {{name}}!',
  },
};

const App = () => (
  <LocalizationProvider
    locale="en"
    translations={TRANSLATIONS}
  >
    <Message descriptor="name" values={{ name: 'Alex' }} />
  </LocalizationProvider>
);

ReactDOM.render(<App />, node); // "Alex" will be rendered

To use with default message:

import React from 'react';
import ReactDOM from 'react-dom';
import { LocalizationProvider, Message } from 'localize-react';

const TRANSLATIONS = {
  en: {},
};

const App = () => (
  <LocalizationProvider
    locale="en"
    translations={TRANSLATIONS}
  >
    <Message
      descriptor="name"
      defaultMessage="Hello, {{name}}!"
      values={{ name: 'Alex' }}
    />
  </LocalizationProvider>
);

ReactDOM.render(<App />, node); // "Alex" will be rendered

useLocalize

useLocalize hook is used to provide localization context, which can be used for translation.

NOTE

Keep in mind, that hooks are not supported in class components!

Example:

import React from 'react';
import ReactDOM from 'react-dom';
import { LocalizationProvider, useLocalize } from 'localize-react';

const TRANSLATIONS = {
  en: {
    name: 'Alex',
  },
};

function Test() {
  const { translate } = useLocalize();

  return translate('name');
}

const App = () => {

  return (
    <LocalizationProvider
      locale="en"
      translations={TRANSLATIONS}
    >
      <Test />
    </LocalizationProvider>
  );
}

ReactDOM.render(<App />, node); // "Alex" will be rendered

Templates

It's possible to use templates inside translation strings with highlighting templates using double curly braces. To pass correpospondent values:

  const translation = translate('My name is {{name}}. I am {{age}}', { name: 'Alex', age: 25 });

Or with React component:

  <Message descriptor="My name is {{name}}. I am {{age}}" values={{ name: 'Alex', age: 25 }} />

contextType

Alternative way of usage inside class components:

import React from 'react';
import { LocalizationContext, LocalizationProvider } from 'localize-react';

const TRANSLATIONS = {
  en: {
    name: 'Alex',
  },
};


class Translation extends React.PureComponent {
  render() {
    return (
      <span>
        {this.context.translate('name')}
      </span>
    )
  }
}

Translation.contextType = LocalizationContext;

const App = () => {
  return (
    <LocalizationProvider
      locale="en"
      translations={TRANSLATIONS}
    >
      <Translation />
    </LocalizationProvider>
  );
}

ReactDOM.render(<App />, node); // "Alex" will be rendered

locale

Locale could be passed in short or long option.

Valid examples:

en-us
EN_US
en
eN-uS

translations

Translations could be passed in any object form (plain or with deep properties)

Valid examples:

const translations = {
  n: {
    a: {
      m: {
        e: 'Alex',
      },
    },
  },
},

You could use key with dot delimiter to access that property:

<Message descriptor="n.a.m.e" /> // will print "Alex"

If there is no exact match in translations, then the value of locale will be sanitized and formatted to lower_case_separate_by_underscore. Make sure you provide translations object with keys in this format. If translations for long locale will not be found, and translations will be found for shorten alternative - that version will be used

Restriction

At least React 16.8.0 is required to use this library, because new React Context API & React Hooks

Contributing

localize-react is open-source library, opened for contributions

Tests

Current test coverage is 100%

jest is used for tests. To run tests:

yarn test

License

localize-react is MIT licensed

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