Skip to content

Commit

Permalink
refactor: adjust parameter of withI18n
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelly-wu committed Apr 29, 2024
1 parent 275da07 commit f70404c
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 46 deletions.
4 changes: 1 addition & 3 deletions docs/dist/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ Get the <code>t</code> function independent of the main program order<br />It
<h4 id="withi18n-type">Type</h4>
<pre>
(
props:{
locale: string
}
locale: string
) => ({ <a href="#t">t</a> })
</pre>

Expand Down
4 changes: 1 addition & 3 deletions docs/dist/API_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@
<h4 id="withi18n-类型">类型</h4>
<pre>
(
props:{
locale: string
}
locale: string
) => ({ <a href="#t">t</a> })
</pre>

Expand Down
30 changes: 24 additions & 6 deletions docs/dist/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ English | [简体中文](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/
<details >
<summary>Table of Contents</summary>

&emsp;&emsp;[[2.2.0] - 2024-04-xx](#220---2024-04-xx)<br/>
&emsp;&emsp;&emsp;&emsp;[Command Line Tool](#220-command-line-tool)<br/>
&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;[Added](#220-command-line-tool-added)<br/>
&emsp;&emsp;[[3.0.0] - 2024-05-xx](#300---2024-05-xx)<br/>
&emsp;&emsp;&emsp;&emsp;[Command Line Tool](#300-command-line-tool)<br/>
&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;[Added](#300-command-line-tool-added)<br/>
&emsp;&emsp;&emsp;&emsp;[API](#300-api)<br/>
&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;[Changed](#300-api-changed)<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 @@ -71,15 +73,31 @@ English | [简体中文](https://github.com/i18n-pro/core/blob/v2.1.0/docs/dist/

</details>

## [2.2.0] - 2024-04-xx
## [3.0.0] - 2024-05-xx

<h3 id="220-command-line-tool">Command Line Tool</h3>
<h3 id="300-command-line-tool">Command Line Tool</h3>

<h4 id="220-command-line-tool-added">Added</h4>
<h4 id="300-command-line-tool-added">Added</h4>

* 命令行配置文件支持用 `TypeScript` 来编写,从版本 `v2.2.0` 开始,初始化命令执行生成的配置文件默认是 `i18nrc.ts` ,但老版的 `i18nrc.js` 依旧是兼容的


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

<h4 id="300-api-changed">Changed</h4>

* 调整 `withI18n` 参数结构

```diff
- function withI18n(props:{
- locale: string
- }): { t }

+ function withI18n(locale: string): { t }
```



## [2.1.0] - 2023-10-12

<h3 id="210-command-line-tool">Command Line Tool</h3>
Expand Down
30 changes: 24 additions & 6 deletions docs/dist/CHANGELOG_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
<details >
<summary>目录</summary>

&emsp;&emsp;[[2.2.0] - 2024-04-xx](#220---2024-04-xx)<br/>
&emsp;&emsp;&emsp;&emsp;[命令行工具](#220-命令行工具)<br/>
&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;[Added](#220-命令行工具-added)<br/>
&emsp;&emsp;[[3.0.0] - 2024-05-xx](#300---2024-05-xx)<br/>
&emsp;&emsp;&emsp;&emsp;[命令行工具](#300-命令行工具)<br/>
&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;[Added](#300-命令行工具-added)<br/>
&emsp;&emsp;&emsp;&emsp;[API](#300-api)<br/>
&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;[Changed](#300-api-changed)<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 @@ -71,15 +73,31 @@

</details>

## [2.2.0] - 2024-04-xx
## [3.0.0] - 2024-05-xx

<h3 id="220-命令行工具">命令行工具</h3>
<h3 id="300-命令行工具">命令行工具</h3>

<h4 id="220-命令行工具-added">Added</h4>
<h4 id="300-命令行工具-added">Added</h4>

* 命令行配置文件支持用 `TypeScript` 来编写,从版本 `v2.2.0` 开始,初始化命令执行生成的配置文件默认是 `i18nrc.ts` ,但老版的 `i18nrc.js` 依旧是兼容的


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

<h4 id="300-api-changed">Changed</h4>

* 调整 `withI18n` 参数结构

```diff
- function withI18n(props:{
- locale: string
- }): { t }

+ function withI18n(locale: string): { t }
```



## [2.1.0] - 2023-10-12

<h3 id="210-命令行工具">命令行工具</h3>
Expand Down
4 changes: 1 addition & 3 deletions docs/src/api/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ ${getFormatTypeString(' ')}
</>
}
type={`(
props:{
locale: string
}
locale: string
) => ({ ${getTitleToA('t')} })`}
props={{
locale: tr('指定当前语言'),
Expand Down
37 changes: 32 additions & 5 deletions docs/src/changelog/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { CodeBlock, H1, Link, render, TableOfContents } from 'jsx-to-md'
import {
Break,
CodeBlock,
H1,
InlineCode,
Link,
render,
TableOfContents,
} from 'jsx-to-md'
import {
getDocHref,
getInterpolationVariable,
Expand Down Expand Up @@ -435,11 +443,11 @@ function V_2_1_0() {
)
}

function V_2_2_0() {
function V_3_0_0() {
return (
<Template
version="2.2.0"
date="2024-04-xx"
version="3.0.0"
date="2024-05-xx"
commandLine={{
added: [
'U',
Expand All @@ -452,6 +460,25 @@ function V_2_2_0() {
),
],
}}
api={{
changed: [
'U',
<>
{tr('调整{0}参数结构', ' `withI18n` ')}
<Break />
<CodeBlock
langType="diff"
code={`
- function withI18n(props:{
- locale: string
- }): { t }
+ function withI18n(locale: string): { t }
`}
/>
</>,
],
}}
/>
)
}
Expand All @@ -464,7 +491,7 @@ export default function ChangeLog(props) {
<H1 skip>{tr('更新日志')}</H1>
{renderLanguage('CHANGELOG')}
<TableOfContents text={tr('目录')} open={false} />
<V_2_2_0 />
<V_3_0_0 />
<V_2_1_0 />
<V_2_0_0 />
<V_1_3_2 />
Expand Down
11 changes: 2 additions & 9 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,10 @@ function translate(
*
* Applicable to the server side, each interface response needs to do international processing
* @param namespace Current namespace
* @param props Specify configuration attributes
* @param locale current language
* @returns
*/
function withI18n(
namespace: string,
props: {
locale: string // 独立于主程序的语言
},
): { t: Translate } {
const { locale } = props

function withI18n(namespace: string, locale: string): { t: Translate } {
return {
t: translateImpl.bind(null, {
...getCurrentState(namespace),
Expand Down
10 changes: 2 additions & 8 deletions src/lib/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,7 @@ export type Translate = (
*
* Applicable to the server side, each interface response needs to do international processing
*
* @param props Specify configuration attributes
* @param locale current language
* @returns
*/
export type WithI18n = (props: {
/**
* Language independent of the main program
*/

locale: string
}) => { t: Translate }
export type WithI18n = (locale: string) => { t: Translate }
4 changes: 1 addition & 3 deletions test/lib/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1111,9 +1111,7 @@ it('模拟服务端,验证 withI18n', () => {
})

const serverResponse = vi.fn((locale: string, reqLocale: string) => {
const { t: _i18n } = withI18n({
locale: reqLocale,
})
const { t: _i18n } = withI18n(reqLocale)

// 主程序
expect(t(text)).toBe(langs[locale]?.[text] || text)
Expand Down

0 comments on commit f70404c

Please sign in to comment.