# @cabify/redux-polyglot 中文文档

规范地址：https://npmcn.com/package/%40cabify/redux-polyglot/

版本：0\.9\.0
npm 元数据更新时间：2025-07-08T11:42:12.355Z
中文译文更新时间：2026-11-08T23:22:32.376Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40cabify/redux-polyglot
原文对照：https://npmcn.com/package/%40cabify/redux-polyglot/?lang=original
项目主页：https://github.com/Tiqa/redux-polyglot
源码仓库：https://github.com/Tiqa/redux-polyglot

弃用说明：Package no longer supported\. Contact Support at https://www\.npmjs\.com/support for more info\.

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：30.5 kB，16 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：5 个
- peer 依赖：react \>= 16\.8\.0，prop-types ^15\.7\.2，react-redux \>= 7\.0\.0
- 首次发布：2021-03-03T13:08:33.477Z
- 最新版本发布：2021-09-23T15:40:45.354Z
- 近 30 天下载量：2186（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：4（近 12 个月 0 个）

## 中文简介

用 Redux 使用 Polyglot.js 的工具

## 安装

```sh
npm install @cabify/redux-polyglot
```

也可以使用：`pnpm add @cabify/redux-polyglot`、`yarn add @cabify/redux-polyglot`、`bun add @cabify/redux-polyglot`

## README

# 红外闪光

[![Codeship Status for Tiqa/redux-polyglot](https://app.codeship.com/projects/21623df0-8959-0134-75f2-0e35097499a9/status?branch=master)](https://app.codeship.com/projects/184177)

工具器(动作,还原器,中件,增强器,选择器),可以帮助使用有Redux的Polyglot.

## 安装

```
    npm install --save redux-polyglot
```

## 设置

首先,你需要减压器中的多块减压器:

```javascript
import { createStore, combineReducers } from 'redux';
import { polyglotReducer } from 'redux-polyglot';

const rootReducer = combineReducers({
    ...yourReducers,
    polyglot: polyglotReducer,
});
const store = createStore(rootReducer, {});

```

## 使用量

### 设置语言

#### 没有中间软件

你可以使用redux-polyglot 没有他的中间软件, 为此你需要 `setLanguage()` 动作创建者 :

-   `setLanguage :: (String, Object) -> Action`

示例

```javascript
import { setLanguage } from 'redux-polyglot';

store.dispatch(setLanguage('en', { yolo: 'yolo' }));
```

第二个参数应为: `polyglot phrases` (见 [多块文档](http://airbnb.io/polyglot.js/)(中文(简体) )

注意: 如果语言短语已经存在, 这将覆盖相应的对象状态 .

#### 有中间软件

这个 `createPolyglotMiddleware()` 函数允许您通过收听特定动作自动更新语言和短语.

在有效载荷中找到位置,然后\[同步\]装入 `polyglot phrases` (与承诺.

它需要3个参数并返回一个中间软件:

-   一、导 言 `actionToCatch :: String | Array<String>`
    -   捕获动作的类型
-   2 - 电话 `getLocale :: Object -> String`
    -   一个函数,将被捕获的动作作为参数并返回新语言.
-   3 - 3 (中文(简体) ) `getPhrases :: String -> Promise Object`
    -   使用语言的函数(由 `getLocale`)并返回对象的许诺(对象应为: `polyglot phrases` (中文(简体) )

中间软件会抓住 `actionToCatch`; 注意 : 当一个匹配动作被发出时, 它会返回这个被调用的承诺, 这样您就可以等待它( 在 SSR 上非常有用 )

```javascript
import { createStore, combineReducers, applyMiddleware } from 'redux';

const polyglotMiddleware = createPolyglotMiddleware(
    'ACTION_TO_CATCH',
    action => action.payload.locale,
    locale => new Promise(resolve => {
        // perform async here
        resolve({
            hello: 'bonjour',
        });
    }),
)

const store = createStore(rootReducer, {}, applyMiddleware(polyglotMiddleware));
```

您可以捕捉一个以上的动作,通过一系列动作类型:

```javascript
const polyglotMiddleware = createPolyglotMiddleware(
    ['FIRST_ACTION_TO_CATCH', 'SECOND_ACTION_TO_CATCH'],
    getLocale,
    getPhrases,
)
```

注:如果语言没有改变,则无事可做.

### 翻译

#### 带有 eep () 选择器

你可以用这个 `getP(state)` 选择器.

它返回内有4个函数的对象 :

-   t: 字符串 - > 字符串 : 翻译(原始多块) `t` 函数)
-   tc: 字符串 - > 字符串: 翻译资本化
-   tt: 字符串 - > 字符串: 翻译标题
-   tu: 字符串 - > 字符串 : 翻译大写
-   tm: (string - > 字符串) - > 字符串 - > 字符串 : 使用自定义形态翻译

(见 [多块文档](http://airbnb.io/polyglot.js/)(中文(简体) )

有可选参数可获取P( ) . 这是允许您在短语对象中自动使用“ aim” 范围 `polyglotScope` 属性.

例如:

```js
store.dispatch(setLanguage('en', {
    some: { nested: { data: { hello: 'hello' } } }
}));
const p = getP(store.getState(), { polyglotScope: 'some.nested.data' });
console.log(p.tc('hello')) // => will return 'Hello'
```

#### 获取当前区域

`getLocale(state)` 选择器返回当前语言.

#### 如果您使用反应

你用得着 `connect()` 从 `react-redux`,然后是 eCP () 选择器,以获取 `p` 您组件中的道具 .

宣传类型 :

```javascript
p: PropTypes.shape({
    t: PropTypes.func.isRequired,
    tc: PropTypes.func.isRequired,
    tu: PropTypes.func.isRequired,
    tm: PropTypes.func.isRequired,
}),
```

##### 翻译( ) 增强器

`props.p` 也可以方便地向一个组件提供翻译增强器:

```javascript
import translate from 'redux-polyglot/translate';
const DummyComponentWithPProps = translate(DummyComponent);
```

您可以选择 `polyglotScope` 与 `translate('scope', Component)`

```js
// all this lines return an enhanced Dummy component
translate(Dummy);
translate('catalog', Dummy); // with polyglotScope
translate()(Dummy); // curried
translate('catalog')(Dummy); // curried with polyglotScope.
translate({ polyglotScope : 'some.nested.data', ownPhrases: 'some.nested.data.hello': 'Hi !', ... })(Dummy); // curried with object configuration.
```

##### 在组件中获取本地地址

你可以用这个 `getLocale()` 选择器在 a 内 [地图状态选项](https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options) 从反应- redux.

宣传类型 : `locale: PropTypes.string,`

### 覆盖短语

在某些情况下,您应该能够将一些默认短语替换为其他短语.

为此, 您必须定义包含您覆盖的短语的对象 . 本物体由下列物体组成: `{ 'some.nested.data': 'phrase', ... }` 地点 `key` 是您想要替换的目标路径 `value` ...新的价值.

##### 与 _获取P( )_ 选择器

简单添加 `ownPhrases` 属性,并设置类似上面的新配置来覆盖 :

```js
store.dispatch(setLanguage('en', {
    some: { nested: { data: { hello: 'hello' } } }
}));
const p = getP(store.getState(), {
    polyglotScope: 'some.nested.data',
    ownPhrases: { 'some.nested.data.hello': 'Hi !' }
});
console.log(p.tc('hello')) // => will return 'Hi !'
```

##### 与 _翻译( )_ 增强器

而不是只经过 _字符串_ 作为参数 : `translate('catalog', Dummy)`,通过平地 _对象_ 包含 `polyglotScope` 财务报告和审定财务报表 `ownPhrases` 属性 :

```js
translate({
    polyglotScope : 'some.nested.data',
    ownPhrases: { 'some.nested.data.catalog': 'Cars' }
}, Dummy);
console.log(p.tc('catalog')) // => will return 'Cars'
```

### 使用多块选项

如果您想要使用的话 `onMissingKey`, (中文(简体) ), `allowMissing` 或者说 `warn` [多块](http://airbnb.io/polyglot.js/) 选项,您可以使用 `createGetP` 创建自定义的工厂选择器 `getP`。 。 。 .

用途 :

```js
import { createGetP } from 'redux-polyglot';

const options = {
  allowMissing: true,
}

export const getP = createGetP(options);
```

请注意您无法使用自定义的翻译 `getP` 选择器.

## 团队

这些人继续推动这个项目

-   杰雷米·文森特(签名)[@jvincent42 (英语)](https://github.com/jvincent42)\- 开发者
-   贾利勒·阿尔法维(Jalil Arfaoui)[@Jalil-Arfaoui (日语)](https://github.com/JalilArfaoui)\- 开发者
-   纪尧姆·阿尔默(签名)[@guillaumearm (英语)](https://github.com/guillaumearm/)\- 开发者

## 常见问题

### @cabify/redux-polyglot 是什么？

用 Redux 使用 Polyglot\.js 的工具注意：该包已被作者标记为弃用——Package no longer supported\. Contact Support at https://www\.npmjs\.com/support for more info\.

### 如何安装 @cabify/redux-polyglot？

在项目目录运行 npm install @cabify/redux-polyglot，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：react（\>= 16\.8\.0）、prop-types（^15\.7\.2）、react-redux（\>= 7\.0\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @cabify/redux-polyglot 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @cabify/redux-polyglot 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @cabify/redux-polyglot 可以商用吗？

@cabify/redux-polyglot 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @cabify/redux-polyglot 的下载量有多少？

根据 npm 官方下载统计，@cabify/redux-polyglot 在 2026/10/09 – 2026/11/07 这 30 天内被下载 2,186 次，日均约 73 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @cabify/redux-polyglot 还在维护吗？

最新版本 0\.9\.0 发布于 2021/09/23（5 年前）；至今共发布 4 个版本，其中过去 12 个月发布了 0 个；首次发布于 2021/03/03。作者已将其标记为弃用，新项目建议寻找替代方案。

