Skip to content

Commit

Permalink
fix: fix incorrect parsing of interpolation variables with spaces in …
Browse files Browse the repository at this point in the history
…Plural type keywords
  • Loading branch information
eyelly-wu committed Aug 1, 2024
1 parent f231eac commit ba89d1b
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 37 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![logo](https://s3.bmp.ovh/imgs/2022/06/25/3a1c742f283cf28e.png "logo")](https://github.com/i18n-pro/core "github")


English | [简体中文](https://github.com/i18n-pro/core/blob/v2.1.0/README_zh-CN.md)
English | [简体中文](https://github.com/i18n-pro/core/blob/v2.1.1/README_zh-CN.md)


<p style="font-size: 18px;">Lightweight, simple, flexible, automatic translation internationalization tool for JavaScript</p>
Expand Down Expand Up @@ -54,15 +54,15 @@ To make internationalization easy and enjoyable 😄💪🏻

# Principle

>Taking `Translation Text` as key is the key to all the functions of the library. If you have any questions about this, [Please see](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/Q&A.md)
>Taking `Translation Text` as key is the key to all the functions of the library. If you have any questions about this, [Please see](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/Q&A.md)
The library is mainly composed of two parts
* Command Line Tool
* Function API

**Command Line Tool**:Parse the text that needs to be translated based on specified rules (regular expressions), translate the text to the specified target language through a translation platform, and finally generate language pack files

An example of parsing text using [Matching Rules](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/MATCH_RULE.md) is as follows:
An example of parsing text using [Matching Rules](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/MATCH_RULE.md) is as follows:
```js
/** Normal string */

Expand Down Expand Up @@ -103,14 +103,14 @@ t('I have {p0 apple}, {p1 banana} and {p2 pear}', 5, 4, 3)
Therefore, `Command Line Tool` and `Function API` work better together. It is precisely because of this structural design that `i18n-pro` library can be easily integrated into any `JavaScript` project
# Help Document

* [Quick Start](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/USAGE.md)
* [Command Line](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/COMMAND_LINE.md)
* [API](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/API.md)
* [Matching Rules](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/MATCH_RULE.md)
* [Translation Log](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/OUTPUT_LOG.md)
* [Q&A](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/Q&A.md)
* [Quick Start](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/USAGE.md)
* [Command Line](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/COMMAND_LINE.md)
* [API](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/API.md)
* [Matching Rules](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/MATCH_RULE.md)
* [Translation Log](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/OUTPUT_LOG.md)
* [Q&A](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/Q&A.md)
* [Contribution Guidelines](https://github.com/i18n-pro/core/blob/dev/docs/dist/CONTRIBUTION_GUIDELINES.md)
* [Changelog](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/CHANGELOG.md)
* [Changelog](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/CHANGELOG.md)


# License
Expand Down
20 changes: 10 additions & 10 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![logo](https://s3.bmp.ovh/imgs/2022/06/25/3a1c742f283cf28e.png "logo")](https://github.com/i18n-pro/core "github")


[English](https://github.com/i18n-pro/core/tree/v2.1.0#readme) | 简体中文
[English](https://github.com/i18n-pro/core/tree/v2.1.1#readme) | 简体中文


<p style="font-size: 18px;">适用于 JavaScript 的轻量、简单、灵活、自动翻译的国际化工具</p>
Expand Down Expand Up @@ -54,15 +54,15 @@

# 原理

>`翻译文案` 作为key是该库所有功能实现的关键,如果对此有任何疑问,[请查看](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/Q&A_zh-CN.md)
>`翻译文案` 作为key是该库所有功能实现的关键,如果对此有任何疑问,[请查看](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/Q&A_zh-CN.md)
该库主要由两部分构成
* 命令行工具
* 函数API

**命令行工具**:根据指定规则(正则匹配)解析出需要翻译的文案,并通过翻译平台将文案翻译到指定目标语言,最后生成语言包文件

解析文案的 [匹配规则](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/MATCH_RULE_zh-CN.md) 简易示例如下
解析文案的 [匹配规则](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/MATCH_RULE_zh-CN.md) 简易示例如下
```js
/** 普通字符串 */

Expand Down Expand Up @@ -103,14 +103,14 @@ t('我有{p0个苹果},{p1个香蕉}和{p2个梨}', 5, 4, 3)
所以 `命令行工具``函数API` 这两者搭配使用效果更佳,也正是由于这样的结构设计,使得 `i18n-pro` 库可以很方便集成到任何的 `JavaScript` 项目中
# 帮助文档

* [快速上手](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/USAGE_zh-CN.md)
* [命令行](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/COMMAND_LINE_zh-CN.md)
* [API](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/API_zh-CN.md)
* [匹配规则](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/MATCH_RULE_zh-CN.md)
* [翻译日志](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/OUTPUT_LOG_zh-CN.md)
* [Q&A](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/Q&A_zh-CN.md)
* [快速上手](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/USAGE_zh-CN.md)
* [命令行](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/COMMAND_LINE_zh-CN.md)
* [API](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/API_zh-CN.md)
* [匹配规则](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/MATCH_RULE_zh-CN.md)
* [翻译日志](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/OUTPUT_LOG_zh-CN.md)
* [Q&A](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/Q&A_zh-CN.md)
* [贡献指南](https://github.com/i18n-pro/core/blob/dev/docs/dist/CONTRIBUTION_GUIDELINES_zh-CN.md)
* [更新日志](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/CHANGELOG_zh-CN.md)
* [更新日志](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/CHANGELOG_zh-CN.md)


# License
Expand Down
2 changes: 1 addition & 1 deletion docs/dist/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Get Internationalization Text<br />The internal will obtain `Translation Text`
<tr>
<td>text</td>
<td>
The text to be translated should meet specific <a href="https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/MATCH_RULE.md">Matching Rules</a> requirements
The text to be translated should meet specific <a href="https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/MATCH_RULE.md">Matching Rules</a> requirements
</td>
</tr>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion docs/dist/API_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
<tr>
<td>text</td>
<td>
待翻译的文案,该文案需满足特定 <a href="https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/MATCH_RULE_zh-CN.md">匹配规则</a>
待翻译的文案,该文案需满足特定 <a href="https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/MATCH_RULE_zh-CN.md">匹配规则</a>
</td>
</tr>
<tr>
Expand Down
21 changes: 19 additions & 2 deletions docs/dist/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
# Changelog


English | [简体中文](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/CHANGELOG_zh-CN.md)
English | [简体中文](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/CHANGELOG_zh-CN.md)


<details >
<summary>Table of Contents</summary>

&emsp;&emsp;[[2.1.1] - 2024-08-01](#211---2024-08-01)<br/>
&emsp;&emsp;&emsp;&emsp;[API](#211-api)<br/>
&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;[Fixed](#211-api-fixed)<br/>
&emsp;&emsp;[[2.1.0] - 2023-10-12](#210---2023-10-12)<br/>
&emsp;&emsp;&emsp;&emsp;[Command Line Tool](#210-command-line-tool)<br/>
&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;[Added](#210-command-line-tool-added)<br/>
Expand Down Expand Up @@ -68,6 +71,20 @@ English | [简体中文](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/

</details>

## [2.1.1] - 2024-08-01

<h3 id="211-api">API</h3>

<h4 id="211-api-fixed">Fixed</h4>

* Fix the issue where `Interpolation Variable` are incorrectly parsed when there are spaces in keywords of the `Plural` type
```js
// Similar to the following
t('there have {p0 Country or Region} in the list', 100)
```



## [2.1.0] - 2023-10-12

<h3 id="210-command-line-tool">Command Line Tool</h3>
Expand Down Expand Up @@ -256,7 +273,7 @@ English | [简体中文](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/

<h4 id="120-documentation-added">Added</h4>

* Added documentation for [Translation Log](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/OUTPUT_LOG.md)
* Added documentation for [Translation Log](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/OUTPUT_LOG.md)


## [1.1.1] - 2022-06-25
Expand Down
21 changes: 19 additions & 2 deletions docs/dist/CHANGELOG_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
# 更新日志


[English](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/CHANGELOG.md) | 简体中文
[English](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/CHANGELOG.md) | 简体中文


<details >
<summary>目录</summary>

&emsp;&emsp;[[2.1.1] - 2024-08-01](#211---2024-08-01)<br/>
&emsp;&emsp;&emsp;&emsp;[API](#211-api)<br/>
&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;[Fixed](#211-api-fixed)<br/>
&emsp;&emsp;[[2.1.0] - 2023-10-12](#210---2023-10-12)<br/>
&emsp;&emsp;&emsp;&emsp;[命令行工具](#210-命令行工具)<br/>
&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;[Added](#210-命令行工具-added)<br/>
Expand Down Expand Up @@ -68,6 +71,20 @@

</details>

## [2.1.1] - 2024-08-01

<h3 id="211-api">API</h3>

<h4 id="211-api-fixed">Fixed</h4>

* 修复 `复数` 类型中关键字存在空格时解析 `插值变量` 不正确
```js
// 类似如下
t('there have {p0 Country or Region} in the list', 100)
```



## [2.1.0] - 2023-10-12

<h3 id="210-命令行工具">命令行工具</h3>
Expand Down Expand Up @@ -256,7 +273,7 @@

<h4 id="120-文档-added">Added</h4>

* 新增[翻译日志](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/OUTPUT_LOG_zh-CN.md)文档说明
* 新增[翻译日志](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/OUTPUT_LOG_zh-CN.md)文档说明


## [1.1.1] - 2022-06-25
Expand Down
6 changes: 3 additions & 3 deletions docs/dist/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const text = t('hello world')


## 3. Initialize Command Line Configuration File
Enter the following command at the command line terminal, [more commands](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/COMMAND_LINE.md#command-list)
Enter the following command at the command line terminal, [more commands](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/COMMAND_LINE.md#command-list)
```bash
npx i18n init
```
Expand Down Expand Up @@ -112,7 +112,7 @@ module.exports = {


## 4. Adjust `i18nrc.js` Configuration
Adjust the configuration items in the configuration file according to the requirements, [Description](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/COMMAND_LINE.md#1--i18nrcjs--configuration) of configuration items
Adjust the configuration items in the configuration file according to the requirements, [Description](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/COMMAND_LINE.md#1--i18nrcjs--configuration) of configuration items

## 5. Execute Translation Command

Expand Down Expand Up @@ -186,7 +186,7 @@ setI18n({
If this library is used directly in a frontend application, when switching languages on the page, only the entire page can see the translated effect through **refresh directly**. A version of `React``Vue``SolidJS``Svelte` related UI library will be released later, which can achieve switching languages without refreshing the page by combining with the characteristics of the corresponding library. Stay tuned

## 8. Demo
Real code examples can be found in the [Live Demo](https://github.com/i18n-pro/core/tree/v2.1.0#live-demo) section of the `README` documentation, and the console output of the current library `Command Line Tool` has also been internationalized
Real code examples can be found in the [Live Demo](https://github.com/i18n-pro/core/tree/v2.1.1#live-demo) section of the `README` documentation, and the console output of the current library `Command Line Tool` has also been internationalized

You can see the Chinese version by executing the command `npx i18n h -L zh`
![demo](https://s3.bmp.ovh/imgs/2023/05/02/cc60f507a8f76a81.gif "demo")
6 changes: 3 additions & 3 deletions docs/dist/USAGE_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const text = t('hello world')


## 3. 初始化命令行配置文件
在命令行终端输入如下命令,[更多命令](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/COMMAND_LINE_zh-CN.md#命令列表)
在命令行终端输入如下命令,[更多命令](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/COMMAND_LINE_zh-CN.md#命令列表)
```bash
npx i18n init
```
Expand Down Expand Up @@ -112,7 +112,7 @@ module.exports = {


## 4. 调整 `i18nrc.js` 配置
根据需求自行调整配置文件中的配置项,配置项的[说明](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/COMMAND_LINE_zh-CN.md#1--i18nrcjs-配置)
根据需求自行调整配置文件中的配置项,配置项的[说明](https://github.com/i18n-pro/core/blob/v2.1.1/docs/dist/COMMAND_LINE_zh-CN.md#1--i18nrcjs-配置)

## 5. 执行翻译命令

Expand Down Expand Up @@ -186,7 +186,7 @@ setI18n({
如果是直接在前端应用中使用该库,在页面上切换语言时,只能通过**直接刷新**整个页面才能看到翻译后的效果,后续会推出 `React``Vue``SolidJS``Svelte` 相关UI库的版本,结合对应库的特性可以做到不刷新页面切换语言,敬请期待

## 8. Demo
真实代码示例可参考 `README` 文档中的 [Live Demo](https://github.com/i18n-pro/core/blob/v2.1.0/README_zh-CN.md#live-demo) ,当前库 `命令行工具` 的控制台输出也接入了国际化
真实代码示例可参考 `README` 文档中的 [Live Demo](https://github.com/i18n-pro/core/blob/v2.1.1/README_zh-CN.md#live-demo) ,当前库 `命令行工具` 的控制台输出也接入了国际化

通过命令 `npx i18n h -L zh` 就能看中文版了
![demo](https://s3.bmp.ovh/imgs/2023/05/02/cc60f507a8f76a81.gif "demo")
27 changes: 27 additions & 0 deletions docs/src/changelog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,32 @@ function V_2_1_0() {
)
}

function V_2_1_1() {
return (
<Template
version="2.1.1"
date="2024-08-01"
api={{
fixed: [
'U',
<>
{tr(
'修复{0}类型中关键字存在空格时解析{1}不正确',
` \`${tr('复数')}\` `,
getInterpolationVariable(),
)}
<CodeBlock
code={`
// ${tr('类似如下')}
t('there have {p0 Country or Region} in the list', 100)`}
/>
</>,
],
}}
/>
)
}

export default function ChangeLog(props) {
initI18n(props)

Expand All @@ -443,6 +469,7 @@ export default function ChangeLog(props) {
<H1 skip>{tr('更新日志')}</H1>
{renderLanguage('CHANGELOG')}
<TableOfContents text={tr('目录')} open={false} />
<V_2_1_1 />
<V_2_1_0 />
<V_2_0_0 />
<V_1_3_2 />
Expand Down
4 changes: 3 additions & 1 deletion docs/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -390,5 +390,7 @@
"对你有帮助": "it's helpful for you",
"可以通过{0}语法来筛选需要被翻译的文件": "Files that need translation can be filtered using {0} syntax",
"配置该属性后,{0}和{1}将会失效,可根据应用场景决定使用哪种方式": "Once this property is configured, {0} and {1} will no longer apply, so decide which approach makes sense for your use case",
"命令行配置添加{0}配置,支持通过{1}语法来筛选文件": "The command line configuration adds {0} config, which supports filtering files using {1} syntax"
"命令行配置添加{0}配置,支持通过{1}语法来筛选文件": "The command line configuration adds {0} config, which supports filtering files using {1} syntax",
"修复{0}类型中关键字存在空格时解析{1}不正确": "Fix the issue where {1} are incorrectly parsed when there are spaces in keywords of the {0} type",
"类似如下": "Similar to the following"
}
2 changes: 1 addition & 1 deletion i18nrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ module.exports = {
googlexConfig: {
from: 'zh-CN',
to: ['en'],
proxy: 'http://127.0.0.1:1087',
proxy: 'http://127.0.0.1:7997',
},
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "i18n-pro",
"version": "2.1.0",
"version": "2.1.1",
"description": "Lightweight, simple, flexible, automatic translation internationalization tool for JavaScript(适用于 JavaScript 的轻量、简单、灵活、自动翻译的国际化工具)",
"files": [
"dist/src",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const commonFormatterRegex = /(\{([ncdt])number\})/
/**
* 匹配复数格式化文案的正则
*/
export const pluralFormatterRegex = /(\{(p)number[ ]{0,}([^ ]+?)\})/
export const pluralFormatterRegex = /(\{(p)number([^}]+)\})/

/**
* 匹配非正确复数格式化文案的正则
Expand Down
24 changes: 23 additions & 1 deletion test/lib/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,14 @@ describe('格式化复数', () => {
resText = `no pear`
}
break
case 'Country or Region':
resText =
payload === 0
? `no Country or Region`
: payload > 1
? `${payload} Countries or Regions`
: `${payload} Country or Region`
break
}
break
case 'zh':
Expand All @@ -957,14 +965,16 @@ describe('格式化复数', () => {
const lastWarnMsg = getNoFormatterWarn(text, 'p2个梨', formatterName)
const spyWarn = vi.spyOn(console, 'warn')

expect(t('测试{p0 a b }存在空格的场景', 1)).toBe('测试1 a b 存在空格的场景')

// 未配置时,默认走正常的匹配逻辑
expect(t(text, 5, 4, 3)).toBe(trZhText)

// 最后一次输出内容匹配
expect(spyWarn).toHaveBeenLastCalledWith(lastWarnMsg)

// 3次警告输出
expect(spyWarn).toHaveBeenCalledTimes(3)
expect(spyWarn).toHaveBeenCalledTimes(4)
})

it('正确配置 formatPlural', () => {
Expand Down Expand Up @@ -1008,6 +1018,18 @@ describe('格式化复数', () => {
// 清除之前的记录
spyWarn.mockClear()

expect(t('there have {p0 Country or Region} in the list', 100)).toBe(
'there have 100 Countries or Regions in the list',
)

expect(t('there have {p0 Country or Region} in the list', 1)).toBe(
'there have 1 Country or Region in the list',
)

expect(t('there have {p0 Country or Region} in the list', 0)).toBe(
'there have no Country or Region in the list',
)

// 根据格式化回调的值返回,这里匹配英文对应的文案
expect(t(text, 5, 4, 3)).toBe(trEnTextWithLocale)

Expand Down
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export default defineConfig({
provider: 'istanbul',
reporter: ['json', 'html', 'text-summary', 'text'],
},
// include: ['**/test/lib/index.test.[jt]s'],
},
})

0 comments on commit ba89d1b

Please sign in to comment.