GithubHelp home page GithubHelp logo

hhy5277 / react-intl-context Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alanwei/react-intl-context

0.0 1.0 0.0 124 KB

Tiny React Component binds language files with React Context.

JavaScript 100.00%

react-intl-context's Introduction

react-intl-context

npm v npm dm

Tiny React Component binds language files with React Context.

Installation

yarn add react-intl-context react

Usage

Single Intl

Add IntlProvider at the top of your app

Recommend to include your language file during the building process

import React from 'react';
import PropTypes from 'prop-types';
import { ConnectedRouter } from 'react-router-redux';
import { IntlProvider } from 'react-intl-context';
import App from './app';

/**
 * locale value in String
 * e.g. "en-us"
 */
const LOCALE = process.env.BUILD_LOCALE;
/**
 * locale messages in JSON or Object
 * e.g. {
 *   "test": "test",
 *   "roleInfo": "I am a {role}.",
 * }
 */
const MESSAGES = process.env.BUILD_LOCALE_MESSAGES;

const propTypes = {
  history: PropTypes.object.isRequired,
};

const Router = props => (
  <ConnectedRouter history={props.history}>
    <IntlProvider
      locale={LOCALE}
      messages={MESSAGES}
    >
      <App />
    </IntlProvider>
  </ConnectedRouter>
);

Router.propTypes = propTypes;
export default Router;

Inject IntlConsumer to component that need translations with injectIntl HOC

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { injectIntl } from 'react-intl-context';

const propTypes = {
  intl: PropTypes.object.isRequired,
};

/**
 * this.props.intl = {
 *   locale: PropTypes.string.
 *   messages: PropTypes.objectOf(PropTypes.string),
 *   formatMessage: ({
 *     id: PropTypes.string,             // message key
 *     defaultMessage: PropTypes.string, // message defaultValue if message key is missing in locale files
 *   }, {
 *     [variableKey]: [variableValue],   // custom variables
 *     ...
 *   }) => PropTypes.string              // message value
 * }
 */
class View extends Component {
  render() {
    const roleMap = {
      'en-us': 'student',
      'zh-cn': '学生',
    };

    const { formatMessage, locale } = this.props.intl;

    return (
      <p>{formatMessage({ id: 'test' })}</p>
      <p>{formatMessage({ id: 'roleInfo' }, { role: roleMap[locale] })}</p>
    );
  }
}

View.propTypes = propTypes;
export default injectIntl(View);

Multi Intl

Add MultiIntlProvider at the top of your app

Recommend to include your language file during the building process

import React from 'react';
import PropTypes from 'prop-types';
import { ConnectedRouter } from 'react-router-redux';
import { MultiIntlProvider } from 'react-intl-context';
import App from './app';

/**
 * default locale value in String
 * e.g. "en-us"
 */
const DEFAULT_LOCALE = process.env.BUILD_DEFAULT_LOCALE;
/**
 * locale message map in JSON or Object
 * e.g. {
 *   "en-us": {
 *     "test": "test",
 *     "roleInfo": "I am a {role}.",
 *   },
 *   "zh-cn": {
 *     "test": "测试",
 *     "roleInfo": "我是一个{role}。",
 *   }
 * }
 */
const MESSAGE_MAP = process.env.BUILD_LOCALE_MESSAGE_MAP;

const propTypes = {
  history: PropTypes.object.isRequired,
};

const Router = props => (
  <ConnectedRouter history={props.history}>
    <MultiIntlProvider
      defaultLocale={DEFAULT_LOCALE}
      messageMap={MESSAGE_MAP}
    >
      <App />
    </MultiIntlProvider>
  </ConnectedRouter>
);

Router.propTypes = propTypes;
export default Router;

Inject IntlConsumer to component that need translations with injectIntl HOC

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { injectIntl } from 'react-intl-context';

const propTypes = {
  intl: PropTypes.object.isRequired,
};

/**
 * this.props.intl = {
 *   locale: PropTypes.string.
 *   messages: PropTypes.objectOf(PropTypes.string),
 *   formatMessage: ({
 *     id: PropTypes.string,             // message key
 *     defaultMessage: PropTypes.string, // message defaultValue if message key is missing in locale files
 *   }, {
 *     [variableKey]: [variableValue],   // custom variables
 *     ...
 *   }) => PropTypes.string              // message value
 * }
 */
class View extends Component {
  render() {
    const roleMap = {
      'en-us': 'student',
      'zh-cn': '学生',
    };

    const { formatMessage, locale } = this.props.intl;

    return (
      <p>{formatMessage({ id: 'test' })}</p>
      <p>{formatMessage({ id: 'roleInfo' }, { role: roleMap[locale] })}</p>
    );
  }
}

View.propTypes = propTypes;
export default injectIntl(View);

react-intl-context's People

Contributors

alanwei avatar alanweilazada avatar

Watchers

 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.