@5rabbits/react-polyglot 中文文档教程

发布于 5年前 浏览 25 项目主页 更新于 3年前

React Polyglot

提供用于将 Polyglot 与 React 结合使用的高阶组件

Installation

npm install --save react-polyglot

Usage

react-polyglot 导出包括一个名为 I18n 的包装器组件和一个名为 <代码>翻译。 装饰器提供了一个 t 属性,它是 Polyglot 的实例。

您需要使用 I18n 包装您的根组件,并传递一个 locale,例如 enfr。 和包含字符串的 messages 对象。

import React from 'react';
import { render } from 'react-dom';
import { I18n } from 'react-polyglot';
import App from './components/app';

const locale = window.locale || 'en';
const messages = {
  "hello_name": "Hello, %{name}.",
  "num_cars": "%{smart_count} car |||| %{smart_count} cars",
}

render(
  <I18n locale={locale} messages={messages}>
    <App />
  </I18n>,
  document.getElementById('app')
);

然后在 AppApp 的子组件中,您可以执行以下操作:

import React from 'react';
import { translate } from 'react-polyglot';

const Greeter = ({ name, t }) => (
  <h3>{t('hello_name', { name })}</h3>
);

Greeter.propTypes = {
  name: React.PropTypes.string.isRequired,
  t: React.PropTypes.func.isRequired,
};

export default translate()(Greeter);

How to provide context in your tests

使用一个简单的帮助程序将您的组件包装在上下文中。

export const wrapWithContext = function (component, context, contextTypes) {
  const wrappedComponent = React.createClass({
    childContextTypes: contextTypes,
    getChildContext() {
      return context;
    },
    render() {
      return component;
    },
  });
  return React.createElement(wrappedComponent);
}

然后在你的测试中使用它。

import React from 'react';
import { renderToString } from 'react-dom/server';
import Polyglot from 'node-polyglot';
import Greeter from './greeter';
import { wrapWithContext } from './helpers';

const polyglot = new Polyglot({
  locale: 'en',
  phrases: {"hello_name": "Hello, %{name}."},
});

const greeterWithContext = wrapWithContext(
  <Greeter name="Batsy" />,
  { t: polyglot.t.bind(polyglot) },
  { t: React.PropTypes.func }
);

// use greeterWithContext in your tests
// here it is shown how to use it with renderToString
console.log(renderToString(greeterWithContext));

Work in progress

测试和贡献指南正在进行中。

Release History

  • 0.1.0 Initial Release
  • 0.2.0 Update the I18n component when the locale changes
  • 0.2.1 Add 'files' to keep in the package
  • 0.2.2 Add babel-cli for the commonjs build
  • 0.2.3 Add prop-types and start using that instead of React.PropTypes PR#6

React Polyglot

Provides higher order component for using Polyglot with React

Installation

npm install --save react-polyglot

Usage

react-polyglot exports consists for one wrapper component called I18n and one decorator called translate. The decorator provides a prop t which is instance of Polyglot.

You are required to wrap your root component with I18n and pass on a locale like en or fr. And messages object containing the strings.

import React from 'react';
import { render } from 'react-dom';
import { I18n } from 'react-polyglot';
import App from './components/app';

const locale = window.locale || 'en';
const messages = {
  "hello_name": "Hello, %{name}.",
  "num_cars": "%{smart_count} car |||| %{smart_count} cars",
}

render(
  <I18n locale={locale} messages={messages}>
    <App />
  </I18n>,
  document.getElementById('app')
);

Then inside App or a child component of App you can do:

import React from 'react';
import { translate } from 'react-polyglot';

const Greeter = ({ name, t }) => (
  <h3>{t('hello_name', { name })}</h3>
);

Greeter.propTypes = {
  name: React.PropTypes.string.isRequired,
  t: React.PropTypes.func.isRequired,
};

export default translate()(Greeter);

How to provide context in your tests

Use a simple helper to wrap your components in a context.

export const wrapWithContext = function (component, context, contextTypes) {
  const wrappedComponent = React.createClass({
    childContextTypes: contextTypes,
    getChildContext() {
      return context;
    },
    render() {
      return component;
    },
  });
  return React.createElement(wrappedComponent);
}

Then use it inside your tests.

import React from 'react';
import { renderToString } from 'react-dom/server';
import Polyglot from 'node-polyglot';
import Greeter from './greeter';
import { wrapWithContext } from './helpers';

const polyglot = new Polyglot({
  locale: 'en',
  phrases: {"hello_name": "Hello, %{name}."},
});

const greeterWithContext = wrapWithContext(
  <Greeter name="Batsy" />,
  { t: polyglot.t.bind(polyglot) },
  { t: React.PropTypes.func }
);

// use greeterWithContext in your tests
// here it is shown how to use it with renderToString
console.log(renderToString(greeterWithContext));

Work in progress

Tests and Contributing guides are in progress.

Release History

  • 0.1.0 Initial Release
  • 0.2.0 Update the I18n component when the locale changes
  • 0.2.1 Add 'files' to keep in the package
  • 0.2.2 Add babel-cli for the commonjs build
  • 0.2.3 Add prop-types and start using that instead of React.PropTypes PR#6
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文