Skip to content

xiaofeizao1996/react-simple-intl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

REACT-SIMPLE-INTL

Internationalize React apps.This library provides an API to handle translations.

Example

// index.js
import React from "react";
import ReactDOM from "react-dom";
import Global from "./Global";

ReactDOM.render(<Global />, document.getElementById("root"));

// Global.js
import React from "react";
import intl from "react-simple-intl";
import A from "./A";
import B from "./B";
intl.global({
  hello: {
    zh: "你好",
    en: "Hello"
  },
  greeting: {
    zh: "你好",
    en: "greeting"
  }
});
export default class extends React.PureComponent {
  render() {
    return (
      <intl.LocaleContextProvider>
        <A />
        <B />
      </intl.LocaleContextProvider>
    );
  }
}

// A.js
import React from "react";
import intl from "react-simple-intl";
const t = intl.load({
  greeting: {
    zh: "你好",
    en: "greeting"
  }
});
class A extends React.PureComponent {
  handleChangeLanguage(language) {
    intl.locale(language);
  }
  render() {
    return (
      <div>
        <button onClick={() => this.handleChangeLanguage("zh")}>中文</button>
        <button onClick={() => this.handleChangeLanguage("en")}>英文</button>
        <div>{t("hello")}</div>
        <div>{t("greeting")}</div>
      </div>
    );
  }
}
export default intl(A);

// B.js
import React from "react";
import intl from "react-simple-intl";
const t = intl.load({
  hey: {
    zh: "你好",
    en: "hey"
  }
});
class B extends React.PureComponent {
  render() {
    return (
      <div>
        <div>{t("hey")}</div>
        <div>{t("hello")}</div>
      </div>
    );
  }
}
export default intl(B);

Contribute

Let's make react-simple-intl better! If you're interested in helping, all contributions are welcome and appreciated.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published