All Projects → Sqooba → mobx-react-intl

Sqooba / mobx-react-intl

Licence: MIT license
A connector between mobx-react and react-intl

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to mobx-react-intl

Transloco
🚀 😍 The internationalization (i18n) library for Angular
Stars: ✭ 1,185 (+3603.13%)
Mutual labels:  i18n, translation, internationalization
Dom I18n
Provides a very basic HTML multilingual support using JavaScript
Stars: ✭ 125 (+290.63%)
Mutual labels:  i18n, translation, internationalization
Eslint Plugin I18n Json
Fully extendable eslint plugin for JSON i18n translation files.
Stars: ✭ 101 (+215.63%)
Mutual labels:  i18n, translation, internationalization
Django Rosetta
Rosetta is a Django application that eases the translation process of your Django projects
Stars: ✭ 806 (+2418.75%)
Mutual labels:  i18n, translation, internationalization
React Translated
A dead simple way to add complex translations (i18n) in a React (DOM/Native) project 🌎🌍🌏
Stars: ✭ 176 (+450%)
Mutual labels:  i18n, translation, internationalization
Translatedjs
Internationalization and localization for JavaScript and Node.js
Stars: ✭ 17 (-46.87%)
Mutual labels:  i18n, translation, internationalization
Phabricator zh hans
Phabricator zh-Hans Translation & Tools.
Stars: ✭ 113 (+253.13%)
Mutual labels:  i18n, translation, internationalization
Fluent.js
JavaScript implementation of Project Fluent
Stars: ✭ 622 (+1843.75%)
Mutual labels:  i18n, translation, internationalization
Node Gettext
A JavaScript implementation of gettext, a localization framework.
Stars: ✭ 175 (+446.88%)
Mutual labels:  i18n, translation, internationalization
Formatjs
The monorepo home to all of the FormatJS related libraries, most notably react-intl.
Stars: ✭ 12,869 (+40115.63%)
Mutual labels:  i18n, translation, internationalization
Frenchkiss.js
The blazing fast lightweight internationalization (i18n) module for javascript
Stars: ✭ 776 (+2325%)
Mutual labels:  i18n, translation, internationalization
Weblate
Web based localization tool with tight version control integration.
Stars: ✭ 2,719 (+8396.88%)
Mutual labels:  i18n, translation, internationalization
React I18next
Internationalization for react done right. Using the i18next i18n ecosystem.
Stars: ✭ 6,942 (+21593.75%)
Mutual labels:  i18n, translation, internationalization
React Intl Hooks
React hooks for internationalization without the hassle ⚛️🌍
Stars: ✭ 64 (+100%)
Mutual labels:  i18n, translation, internationalization
I18next
i18next: learn once - translate everywhere
Stars: ✭ 5,971 (+18559.38%)
Mutual labels:  i18n, translation, internationalization
Pseudo Localization
Dynamic pseudo-localization in the browser and nodejs
Stars: ✭ 109 (+240.63%)
Mutual labels:  i18n, translation, internationalization
Easy localization
Easy and Fast internationalizing your Flutter Apps
Stars: ✭ 407 (+1171.88%)
Mutual labels:  i18n, translation, internationalization
Gettext
PHP library to collect and manipulate gettext (.po, .mo, .php, .json, etc)
Stars: ✭ 578 (+1706.25%)
Mutual labels:  i18n, translation, internationalization
Jquery I18next
i18next plugin for jquery usage
Stars: ✭ 143 (+346.88%)
Mutual labels:  i18n, translation, internationalization
I18next Express Middleware
[deprecated] can be replaced with i18next-http-middleware
Stars: ✭ 195 (+509.38%)
Mutual labels:  i18n, translation, internationalization

Internationalization with mobx and react-intl

A library to use react-intl along with a mobx store for the selected locale with typescript support.

npm version CircleCI semantic-release

Example

Since an example is worth a thousand words.

You can see this example running on Stackblitz

import React, { Component } from 'react';
import { render } from 'react-dom';
import { Provider, inject, observer } from "mobx-react"; 
import { MobxIntlProvider, LocaleStore } from "mobx-react-intl"; 
import {addLocaleData, injectIntl, FormattedMessage} from "react-intl";
import enLocale from 'react-intl/locale-data/en';
import deLocale from 'react-intl/locale-data/de';
addLocaleData([...deLocale, ...enLocale]);


const translations = {
  en: {
    hello: "Hello", 
    world: "World"
  }, 
  de: {
    hello: "Hallo", 
    world: "Wereld"
  }
}

// Internationalization
const localeStore = new LocaleStore("en", translations);

const store = {
    locale: localeStore, // The locale store has to be called locale. 
};

const _Home = ({intl: {formatMessage}, locale}) => <div>
    <h1>{formatMessage({id: "hello"})}</h1>
    <FormattedMessage id="world" />
    <br/>
    <select value={locale.value} onChange={(event) => locale.value = event.target.value}>
        <option value="de">Deutsch</option>
        <option value="en">English</option>
    </select>
</div>
const Home = inject("locale")(injectIntl(observer(_Home)));

const App = () => <div>
    <Provider {...store}> 
        <MobxIntlProvider>
            <Home />
        </MobxIntlProvider>
    </Provider>
</div>;

render(<App />, document.getElementById('root'));

I18n mobx store

LocaleStore is a mobx store that contains the locale data and persists the locale to the browser LocalStorage (if it exists).

The store expects the default locale and the translations for all supported locales as arguments.

Intl Provider

MobxIntlProvider.js: Creates the I18n provider for mobx. Note that it relies on IntlProvider from react-intl.

This component has the same interface as IntlProvider except that the locale and messages attributes are injected through mobx.

Using locale data in other stores.

It is possible to also use the i18n without being in the react scope. For example when initializing a store that needs locale data, just pass the locale store as a prop. You can see that a formatMessage method is implemented in the LocaleStore for this case.

Example

A running example with some file structure is provided in examples/simple-app.

Run the example locally:

To run this create-react-app project, run the following commands (yarn can be replaced by npm)

yarn install 
yarn start

Introduction

To have internationalization (i18n) working with react, mobx and react-intl, we need to:

  1. Create a store for the i18n data (locale used and locale data)
  2. Uhe use a custom provider to provide internationalization from the mobx store.
  3. Inject the provider to UI components.
  4. Use the provided code.

I18n translations.

We create two files to maintain locale data:

./translations/en.js and ./translations/de.js provide translations for English and German.

Language Switcher

./Toolbar.js is the component used to switch from one language to another.

Home

./Home.js is a UI component that uses i18n to display texts.

Application initializer

In ./App.js we finally wrap up all components.

The app is initialized initialized with the Provider and shows the Home component.

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