npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-mobx-i18n

v1.2.0

Published

Make React components translatable using MobX

Downloads

232

Readme

react-mobx-i18n

Info: This is a fork from react-mobx-translatable due to other opinions about the approach.

Make React components translatable using MobX. Can be used both on the server (SSR) and in the browser.

Note: This plugin depends on mobx-react features that are currently marked as experimental: Provider and inject.

Installation

npm install --save react-mobx-i18n

Usage

Checklist (see the example for more details)

  • Setup the i18n object in store
  • Initialize the i18n-harmony lib and translatable
  • Wrap your components with Provider component
  • Set the @translatable decorator on your components
  • Call this.t(translationKey, options) from the component
  • To change the language, change the locale variable in the store

Methods

init(injectFn)

Method receives injectFn - function that maps the i18n object from the store

  • The function receives the whole store object (from Provider)
  • The function should return an object that contains an i18n key with the i18n object from store (see the example)
  • Default: (store) => {i18n: i18n.store}
  • If you have the i18n object in the root of the store (the default function can map the value), you don't need to call init

translatable(Component|String[])

Method can receive either an array of strings or a React component.

  • Array of strings - Used for connecting the store from Provider to the component. Translatable is returning a new function that accepts a React component.
  • React component - The function will wrap the passed component

In both cases, the wrapped component will also be an observer. If using with other decorators, translatable should be the innermost one.

Example

The example assumes you're using the following:

This is however not a requirement.

Initialize store, i18n, and react-mobx-i18n

import {observable} from 'mobx';
import i18n from 'i18n-harmony';
import {init} from 'react-mobx-i18n';

const defaultLocale = 'en'; // Can be based on browser language or user selection (localStorage, cookies)

const store = {
  i18n: observable({locale: defaultLocale})
};

// For details, see i18n-harmony: https://github.com/DarkoKukovec/i18n-harmony
i18n.init({
  translations: {
    en: {hello: 'Hello world!'}
  }
});

init((store) => ({i18n: store.i18n}));

Wrap your React components inside of the Provider component and pass it the store

import {Provider} from 'mobx-react';
import store from './store';

ReactDOM.render(<Provider {...store}>
  <Router {...renderProps} />
</Provider>, document.getElementById('app'));

Translatable component

import {Component} from 'react';
import {translatable} from 'react-mobx-i18n';

@translatable
export default class MyComponent extends Component {
  render() {
    return <div>{this.t('hello')}</div>
  }
}

has method

import {Component} from 'react';
import {translatable} from 'translatable';

@translatable
export default class MyComponent extends Component {
  render() {
    return <div>{this.has('hello') && this.t('hello')}</div>
  }
}

License

MIT License