From b97117c9e25f9e0808e26e256d82cd5a0a6409e2 Mon Sep 17 00:00:00 2001 From: Linzp Date: Wed, 31 Dec 2025 11:01:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9context?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 501 ++++-------------- doc/api.md | 354 ++++--------- doc/example.json | 4 +- doc/summary.md | 144 +---- package.json | 8 +- ...72\344\276\213\346\217\217\350\277\260.md" | 50 ++ ...73\345\236\213\345\243\260\346\230\216.md" | 103 ++++ ...17\350\277\260\346\226\207\344\273\266.md" | 179 +++++++ ...37\346\210\220\346\226\207\346\241\243.md" | 79 +++ ...20\350\257\255\350\250\200\345\214\205.md" | 125 +++++ src/index.d.ts | 56 ++ src/index.js | 14 +- src/package-manifest.json | 115 ++++ 13 files changed, 912 insertions(+), 820 deletions(-) create mode 100644 "prompts/\345\221\275\345\220\215\347\244\272\344\276\213\347\274\226\345\206\231\347\244\272\344\276\213\346\217\217\350\277\260.md" create mode 100644 "prompts/\346\267\273\345\212\240ts\347\261\273\345\236\213\345\243\260\346\230\216.md" create mode 100644 "prompts/\347\224\237\346\210\220\345\214\205\345\212\237\350\203\275\346\217\217\350\277\260\346\226\207\344\273\266.md" create mode 100644 "prompts/\347\224\237\346\210\220\346\226\207\346\241\243.md" create mode 100644 "prompts/\347\224\237\346\210\220\350\257\255\350\250\200\345\214\205.md" create mode 100644 src/index.d.ts create mode 100644 src/package-manifest.json diff --git a/README.md b/README.md index 10611fa..3ccd3ee 100644 --- a/README.md +++ b/README.md @@ -16,149 +16,13 @@ npm i --save @kne/react-intl ### 概述 -@kne/react-intl是一个基于[react-intl](https://formatjs.io/docs/react-intl/)的React国际化封装组件库,提供了更简便的API和更灵活的使用方式。它与@kne/global-context深度集成,支持从上下文获取语言设置,并提供了命名空间支持,使得国际化资源的管理更加方便。 +这是一个基于 react-intl 的 React 国际化组件库,提供便捷的国际化解决方案。通过封装 Provider 和工具函数,简化了多语言应用的开发流程,支持灵活的命名空间管理和动态语言切换。 -### 主要特性 +核心特性包括:开箱即用的国际化 Provider 工厂函数、支持命名空间的消息隔离、自动加载和管理翻译消息、与全局上下文集成实现自动语言检测、完整的 TypeScript 类型定义支持。组件库兼容 react-intl 的所有功能,同时提供了更简洁的 API 设计。 -- **简化的API**:封装react-intl,提供更简洁直观的API -- **命名空间支持**:支持通过命名空间隔离和组织国际化消息 -- **上下文集成**:与@kne/global-context无缝集成,支持从上下文获取locale -- **灵活的Provider**:支持多种方式创建和使用国际化Provider -- **动态消息加载**:支持运行时动态加载和更新国际化消息 -- **函数式组件支持**:支持将intl实例作为render props传递 +适用于需要快速实现国际化功能的 React 应用,特别适合中大型项目的多语言支持,包括电商网站、SaaS 平台、企业后台管理系统等场景。支持组件级和全局级的语言配置,可以根据不同模块独立管理翻译内容。 -### 安装 - -```bash -npm install @kne/react-intl -``` - -### 基本使用 - -#### 1. 使用createIntlProvider创建Provider - -```jsx -import {createIntlProvider} from '@kne/react-intl'; - -// 方式1:使用对象配置 -const IntlProvider = createIntlProvider({ - defaultLocale: 'zh-CN', - messages: { - 'zh-CN': { - hello: '你好,世界!' - }, - 'en-US': { - hello: 'Hello, world!' - } - }, - namespace: 'app' -}); - -// 方式2:使用参数配置 -const IntlProvider = createIntlProvider('zh-CN', { - hello: '你好,世界!' -}, 'app'); - -// 在应用中使用 -const App = () => ( - - - -); - -// 使用render props获取intl实例 -const App = () => ( - - {(intl) => ( -
{intl.formatMessage({id: 'hello'})}
- )} -
-); -``` - -#### 2. 使用createWithIntlProvider创建高阶组件 - -```jsx -import {createWithIntlProvider} from '@kne/react-intl'; - -const withIntl = createWithIntlProvider({ - defaultLocale: 'zh-CN', - defaultMessage: { - hello: '你好,世界!' - }, - namespace: 'app' -}); - -// 包装组件 -const WrappedComponent = withIntl(YourComponent); -``` - -#### 3. 动态加载国际化消息 - -```jsx -import {localeLoader, messagesLoader} from '@kne/react-intl'; - -// 加载单个语言的消息 -localeLoader('zh-CN', { - welcome: '欢迎使用' -}, 'app'); - -// 批量加载多个语言的消息 -messagesLoader({ - 'zh-CN': { - welcome: '欢迎使用' - }, - 'en-US': { - welcome: 'Welcome' - } -}, 'app'); -``` - -#### 4. 与@kne/global-context集成 - -```jsx -import {createIntlProvider} from '@kne/react-intl'; -import {createContext} from '@kne/global-context'; - -const {useContext, ContextProvider} = createContext(); - -const IntlProvider = createIntlProvider({ - defaultLocale: 'zh-CN', - messages: { - 'zh-CN': { - hello: '你好' - } - } -}); - -// locale将从context中获取 -const App = () => ( - - - - - -); -``` - -### 最佳实践 - -1. **使用命名空间**: - - 为不同模块使用不同的命名空间,避免消息键冲突 - - 保持命名空间结构清晰,便于管理 - -2. **动态加载**: - - 按需加载国际化消息,减少初始加载大小 - - 使用messagesLoader批量加载相关消息 - -3. **上下文集成**: - - 优先使用上下文管理locale - - 在需要时才通过props覆盖locale - -4. **消息组织**: - - 使用有意义的消息键 - - 保持消息结构扁平化 - - 适当使用消息格式化功能 +技术亮点在于提供了多种创建方式,createIntlProvider 用于创建完整的国际化上下文,createWithIntlProvider 可以为单个组件注入国际化能力,createIntl 则适用于非组件环境的国际化场景。消息加载器支持单个和批量加载,命名空间机制确保不同模块的翻译内容互不干扰。 ### 示例 @@ -176,8 +40,8 @@ const App = () => ( #### 示例代码 -- 这里填写示例标题 -- 这里填写示例说明 +- 基础国际化 +- 使用createIntlProvider创建国际化Provider,支持语言切换 - _ReactIntl(@kne/current-lib_react-intl)[import * as _ReactIntl from "@kne/react-intl"],antd(antd) ```jsx @@ -224,277 +88,104 @@ render(); ### API -本文档详细介绍了@kne/react-intl提供的API,包括函数、组件和钩子。 - -### 核心API - -#### createIntl - -创建国际化相关的组件和hooks,可以自定义获取locale的方法。 - -| 参数 | 类型 | 必填 | 默认值 | 描述 | -|------|------|------|--------|------| -| options | Object | 否 | {} | 配置选项 | -| options.getLocale | Function | 否 | 从context获取 | 自定义获取locale的函数 | - -**返回值**: - -| 属性 | 类型 | 描述 | -|------|------|------| -| IntlProvider | Component | 国际化Provider组件 | -| useIntl | Hook | 获取intl对象的hook | -| FormattedDate | Component | 格式化日期组件 | -| FormattedTime | Component | 格式化时间组件 | -| FormattedNumber | Component | 格式化数字组件 | -| FormattedPlural | Component | 格式化复数组件 | -| FormattedMessage | Component | 格式化消息组件 | -| FormattedHTMLMessage | Component | 格式化HTML消息组件 | -| FormattedRelativeTime | Component | 格式化相对时间组件 | - -**示例**: - -```jsx -import {createIntl} from '@kne/react-intl'; -import {createContext} from '@kne/global-context'; - -const {useContext} = createContext(); - -const {IntlProvider, useIntl, FormattedMessage} = createIntl({ - getLocale: () => { - const [context] = useContext(); - return context.locale; - } -}); -``` - -#### createIntlProvider - -创建国际化Provider组件,支持多种调用方式。 - -**方式1:使用对象配置** - -| 参数 | 类型 | 必填 | 默认值 | 描述 | -|------|------|------|--------|------| -| options | Object | 是 | - | Provider的配置选项 | -| options.locale | String | 否 | 从context获取 | 当前语言 | -| options.defaultLocale | String | 否 | 'zh-CN' | 默认语言 | -| options.messages | Object | 否 | {} | 国际化消息对象 | -| options.namespace | String | 否 | 'global' | 命名空间 | - -**方式2:使用参数配置** - -| 参数 | 类型 | 必填 | 默认值 | 描述 | -|------|------|------|--------|------| -| locale | String | 是 | - | 当前语言 | -| messages | Object | 是 | - | 国际化消息对象 | -| namespace | String | 否 | 'global' | 命名空间 | - -**返回值**: - -| 类型 | 描述 | +### createIntlProvider +创建国际化 Provider 组件,为子组件提供 i18n 上下文。支持配置默认语言、翻译消息和命名空间,返回的 Provider 组件可以动态切换语言。 + +#### 配置参数 +| 参数名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| args | string \| object \| array | 是 | - | 支持三种格式:(1) 字符串:默认语言标识 (2) 对象:包含 defaultLocale、defaultMessage、namespace、messages (3) 数组:[defaultLocale, defaultMessage, namespace] | + +#### Provider 属性 +| 属性名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| locale | string | 否 | zh-CN | 当前语言标识,优先级高于默认值 | +| children | ReactNode \| function | 是 | - | 子组件或渲染函数,函数形式接收 intl 对象 | + +### createWithIntlProvider +创建高阶组件,为被包装组件添加国际化能力。返回的 HOC 函数接受组件并返回带有国际化上下文的组件。 + +#### 配置参数 +| 参数名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| args | string \| object \| array | 是 | - | 支持三种格式:(1) 字符串:默认语言标识 (2) 对象:包含 defaultLocale、defaultMessage、namespace、messages (3) 数组:[defaultLocale, defaultMessage, namespace] | + +#### HOC 返回组件属性 +| 属性名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| locale | string | 否 | zh-CN | 当前语言标识,优先级高于默认值 | + +### createIntl +创建国际化实例,用于在非组件环境中使用 i18n 功能,例如在工具函数、API 调用等场景。 + +#### 配置参数 +| 参数名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| locale | string | 否 | zh-CN | 语言标识 | +| message | object | 否 | - | 翻译消息对象 | +| namespace | string | 否 | global | 命名空间 | + +#### 返回值 +| 类型 | 说明 | |------|------| -| Component | 国际化Provider组件,支持render props获取intl实例 | +| IntlShape | react-intl 的国际化实例对象 | -**示例**: +### localeLoader +加载指定语言的翻译消息到指定命名空间。 -```jsx -// 方式1:使用对象配置 -import {createIntlProvider} from '@kne/react-intl'; - -const IntlProvider = createIntlProvider({ - defaultLocale: 'zh-CN', - messages: { - 'zh-CN': { - hello: '你好,世界!' - }, - 'en-US': { - hello: 'Hello, world!' - } - }, - namespace: 'app' -}); +#### 方法参数 +| 参数名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| locale | string | 是 | - | 语言标识,如 zh-CN、en-US | +| defaultMessage | object | 是 | - | 翻译消息对象,key 为消息 ID | +| namespace | string | 否 | global | 命名空间,用于隔离不同模块的翻译 | -// 方式2:使用参数配置 -const IntlProvider = createIntlProvider('zh-CN', { - hello: '你好,世界!' -}, 'app'); - -// 在应用中使用 -const App = () => ( - - - -); - -// 使用render props获取intl实例 -const App = () => ( - - {(intl) => ( -
{intl.formatMessage({id: 'hello'})}
- )} -
-); -``` - -#### createWithIntlProvider - -创建一个高阶组件,用于包装组件并提供国际化功能。 - -| 参数 | 类型 | 必填 | 默认值 | 描述 | -|------|------|------|--------|------| -| options | Object | 是 | - | Provider的配置选项 | -| options.locale | String | 否 | 从context获取 | 当前语言 | -| options.defaultLocale | String | 否 | 'zh-CN' | 默认语言 | -| options.defaultMessage | Object | 否 | {} | 默认消息对象 | -| options.namespace | String | 否 | 'global' | 命名空间 | - -**返回值**: - -| 类型 | 描述 | +#### 返回值 +| 类型 | 说明 | |------|------| -| Function | 高阶组件函数,接收一个组件并返回包装后的组件 | +| object | 更新后的消息对象 | -**示例**: +### messagesLoader +批量加载多语言消息对象,支持同时加载多种语言的翻译内容。 -```jsx -import {createWithIntlProvider} from '@kne/react-intl'; - -const withIntl = createWithIntlProvider({ - defaultLocale: 'zh-CN', - defaultMessage: { - hello: '你好,世界!' - }, - namespace: 'app' -}); +#### 方法参数 +| 参数名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| messages | object | 是 | - | 多语言消息对象,key 为语言标识,value 为该语言的翻译消息 | +| namespace | string | 否 | global | 命名空间,用于隔离不同模块的翻译 | -// 包装组件 -const WrappedComponent = withIntl(YourComponent); +### message +存储所有已加载的翻译消息的容器对象,结构为多层嵌套对象,按语言和命名空间组织。 -// 使用包装后的组件 -const App = () => ; -``` - -#### localeLoader - -加载单个语言的国际化消息。 - -| 参数 | 类型 | 必填 | 默认值 | 描述 | -|------|------|------|--------|------| -| locale | String | 是 | - | 语言代码 | -| localeMessage | Object | 是 | - | 消息对象 | -| namespace | String | 否 | 'global' | 命名空间 | - -**返回值**: - -| 类型 | 描述 | +#### 数据结构 +| 层级 | 类型 | 说明 | +|------|------|------| +| 第一层 key | string | 语言标识,如 zh-CN、en-US | +| 第二层 key | string | 命名空间,如 global、user | +| 第三层 | object | 翻译消息键值对 | + +### IntlProvider +react-intl 的国际化 Provider 组件,提供完整的国际化上下文支持。 + +#### 属性说明 +| 属性名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| locale | string | 是 | - | 当前语言 | +| messages | object | 是 | - | 翻译消息对象 | +| children | ReactNode | 是 | - | 子组件 | + +### 默认导出 +默认导出为 createIntl 函数,用于创建国际化实例。 + +#### 配置参数 +| 参数名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| locale | string | 否 | zh-CN | 语言标识 | +| message | object | 否 | - | 翻译消息对象 | +| namespace | string | 否 | global | 命名空间 | + +#### 返回值 +| 类型 | 说明 | |------|------| -| Object | 更新后的完整消息对象 | - -**示例**: - -```jsx -import {localeLoader} from '@kne/react-intl'; - -// 加载中文消息 -localeLoader('zh-CN', { - welcome: '欢迎使用我们的应用' -}, 'app'); - -// 加载英文消息 -localeLoader('en-US', { - welcome: 'Welcome to our application' -}, 'app'); -``` - -#### messagesLoader - -批量加载多语言的国际化消息。 - -| 参数 | 类型 | 必填 | 默认值 | 描述 | -|------|------|------|--------|------| -| messages | Object | 是 | - | 多语言消息对象,格式为 {locale: messages} | -| namespace | String | 否 | 'global' | 命名空间 | - -**返回值**:无 - -**示例**: - -```jsx -import {messagesLoader} from '@kne/react-intl'; - -// 批量加载多语言消息 -messagesLoader({ - 'zh-CN': { - welcome: '欢迎使用', - goodbye: '再见' - }, - 'en-US': { - welcome: 'Welcome', - goodbye: 'Goodbye' - } -}, 'app'); -``` - -### 从react-intl导出的API - -以下API直接从react-intl导出,详细用法请参考[react-intl官方文档](https://formatjs.io/docs/react-intl/)。 - -| API | 类型 | 描述 | -|-----|------|------| -| IntlProvider | Component | 国际化Provider组件 | -| FormattedDate | Component | 格式化日期组件 | -| FormattedTime | Component | 格式化时间组件 | -| FormattedNumber | Component | 格式化数字组件 | -| FormattedPlural | Component | 格式化复数组件 | -| FormattedMessage | Component | 格式化消息组件 | -| FormattedHTMLMessage | Component | 格式化HTML消息组件 | -| FormattedRelativeTime | Component | 格式化相对时间组件 | -| useIntl | Hook | 获取intl对象的hook | -| injectIntl | HOC | 注入intl对象的高阶组件 | -| defineMessages | Function | 定义消息的辅助函数 | -| createIntl | Function | 创建intl对象的函数 | - -### 内部API - -#### message - -存储所有已加载的国际化消息的对象。 - -**结构**: - -```javascript -{ - [locale: string]: { - [namespace: string]: { - [messageId: string]: string - } - } -} -``` - -**示例**: - -```javascript -// 内部结构示例 -{ - 'zh-CN': { - 'global': { - 'hello': '你好' - }, - 'app': { - 'welcome': '欢迎使用' - } - }, - 'en-US': { - 'global': { - 'hello': 'Hello' - }, - 'app': { - 'welcome': 'Welcome' - } - } -} -``` +| IntlShape | react-intl 的国际化实例对象 | -**注意**:此对象通常不需要直接访问,应通过提供的API函数进行操作。 -### diff --git a/doc/api.md b/doc/api.md index ba8d4f7..20981b8 100644 --- a/doc/api.md +++ b/doc/api.md @@ -1,274 +1,100 @@ -本文档详细介绍了@kne/react-intl提供的API,包括函数、组件和钩子。 - -### 核心API - -#### createIntl - -创建国际化相关的组件和hooks,可以自定义获取locale的方法。 - -| 参数 | 类型 | 必填 | 默认值 | 描述 | -|------|------|------|--------|------| -| options | Object | 否 | {} | 配置选项 | -| options.getLocale | Function | 否 | 从context获取 | 自定义获取locale的函数 | - -**返回值**: - -| 属性 | 类型 | 描述 | -|------|------|------| -| IntlProvider | Component | 国际化Provider组件 | -| useIntl | Hook | 获取intl对象的hook | -| FormattedDate | Component | 格式化日期组件 | -| FormattedTime | Component | 格式化时间组件 | -| FormattedNumber | Component | 格式化数字组件 | -| FormattedPlural | Component | 格式化复数组件 | -| FormattedMessage | Component | 格式化消息组件 | -| FormattedHTMLMessage | Component | 格式化HTML消息组件 | -| FormattedRelativeTime | Component | 格式化相对时间组件 | - -**示例**: - -```jsx -import {createIntl} from '@kne/react-intl'; -import {createContext} from '@kne/global-context'; - -const {useContext} = createContext(); - -const {IntlProvider, useIntl, FormattedMessage} = createIntl({ - getLocale: () => { - const [context] = useContext(); - return context.locale; - } -}); -``` - -#### createIntlProvider - -创建国际化Provider组件,支持多种调用方式。 - -**方式1:使用对象配置** - -| 参数 | 类型 | 必填 | 默认值 | 描述 | -|------|------|------|--------|------| -| options | Object | 是 | - | Provider的配置选项 | -| options.locale | String | 否 | 从context获取 | 当前语言 | -| options.defaultLocale | String | 否 | 'zh-CN' | 默认语言 | -| options.messages | Object | 否 | {} | 国际化消息对象 | -| options.namespace | String | 否 | 'global' | 命名空间 | - -**方式2:使用参数配置** - -| 参数 | 类型 | 必填 | 默认值 | 描述 | -|------|------|------|--------|------| -| locale | String | 是 | - | 当前语言 | -| messages | Object | 是 | - | 国际化消息对象 | -| namespace | String | 否 | 'global' | 命名空间 | - -**返回值**: - -| 类型 | 描述 | +### createIntlProvider +创建国际化 Provider 组件,为子组件提供 i18n 上下文。支持配置默认语言、翻译消息和命名空间,返回的 Provider 组件可以动态切换语言。 + +#### 配置参数 +| 参数名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| args | string \| object \| array | 是 | - | 支持三种格式:(1) 字符串:默认语言标识 (2) 对象:包含 defaultLocale、defaultMessage、namespace、messages (3) 数组:[defaultLocale, defaultMessage, namespace] | + +#### Provider 属性 +| 属性名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| locale | string | 否 | zh-CN | 当前语言标识,优先级高于默认值 | +| children | ReactNode \| function | 是 | - | 子组件或渲染函数,函数形式接收 intl 对象 | + +### createWithIntlProvider +创建高阶组件,为被包装组件添加国际化能力。返回的 HOC 函数接受组件并返回带有国际化上下文的组件。 + +#### 配置参数 +| 参数名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| args | string \| object \| array | 是 | - | 支持三种格式:(1) 字符串:默认语言标识 (2) 对象:包含 defaultLocale、defaultMessage、namespace、messages (3) 数组:[defaultLocale, defaultMessage, namespace] | + +#### HOC 返回组件属性 +| 属性名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| locale | string | 否 | zh-CN | 当前语言标识,优先级高于默认值 | + +### createIntl +创建国际化实例,用于在非组件环境中使用 i18n 功能,例如在工具函数、API 调用等场景。 + +#### 配置参数 +| 参数名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| locale | string | 否 | zh-CN | 语言标识 | +| message | object | 否 | - | 翻译消息对象 | +| namespace | string | 否 | global | 命名空间 | + +#### 返回值 +| 类型 | 说明 | |------|------| -| Component | 国际化Provider组件,支持render props获取intl实例 | - -**示例**: - -```jsx -// 方式1:使用对象配置 -import {createIntlProvider} from '@kne/react-intl'; +| IntlShape | react-intl 的国际化实例对象 | -const IntlProvider = createIntlProvider({ - defaultLocale: 'zh-CN', - messages: { - 'zh-CN': { - hello: '你好,世界!' - }, - 'en-US': { - hello: 'Hello, world!' - } - }, - namespace: 'app' -}); +### localeLoader +加载指定语言的翻译消息到指定命名空间。 -// 方式2:使用参数配置 -const IntlProvider = createIntlProvider('zh-CN', { - hello: '你好,世界!' -}, 'app'); +#### 方法参数 +| 参数名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| locale | string | 是 | - | 语言标识,如 zh-CN、en-US | +| defaultMessage | object | 是 | - | 翻译消息对象,key 为消息 ID | +| namespace | string | 否 | global | 命名空间,用于隔离不同模块的翻译 | -// 在应用中使用 -const App = () => ( - - - -); - -// 使用render props获取intl实例 -const App = () => ( - - {(intl) => ( -
{intl.formatMessage({id: 'hello'})}
- )} -
-); -``` - -#### createWithIntlProvider - -创建一个高阶组件,用于包装组件并提供国际化功能。 - -| 参数 | 类型 | 必填 | 默认值 | 描述 | -|------|------|------|--------|------| -| options | Object | 是 | - | Provider的配置选项 | -| options.locale | String | 否 | 从context获取 | 当前语言 | -| options.defaultLocale | String | 否 | 'zh-CN' | 默认语言 | -| options.defaultMessage | Object | 否 | {} | 默认消息对象 | -| options.namespace | String | 否 | 'global' | 命名空间 | - -**返回值**: - -| 类型 | 描述 | +#### 返回值 +| 类型 | 说明 | |------|------| -| Function | 高阶组件函数,接收一个组件并返回包装后的组件 | - -**示例**: +| object | 更新后的消息对象 | -```jsx -import {createWithIntlProvider} from '@kne/react-intl'; +### messagesLoader +批量加载多语言消息对象,支持同时加载多种语言的翻译内容。 -const withIntl = createWithIntlProvider({ - defaultLocale: 'zh-CN', - defaultMessage: { - hello: '你好,世界!' - }, - namespace: 'app' -}); +#### 方法参数 +| 参数名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| messages | object | 是 | - | 多语言消息对象,key 为语言标识,value 为该语言的翻译消息 | +| namespace | string | 否 | global | 命名空间,用于隔离不同模块的翻译 | -// 包装组件 -const WrappedComponent = withIntl(YourComponent); +### message +存储所有已加载的翻译消息的容器对象,结构为多层嵌套对象,按语言和命名空间组织。 -// 使用包装后的组件 -const App = () => ; -``` - -#### localeLoader - -加载单个语言的国际化消息。 - -| 参数 | 类型 | 必填 | 默认值 | 描述 | -|------|------|------|--------|------| -| locale | String | 是 | - | 语言代码 | -| localeMessage | Object | 是 | - | 消息对象 | -| namespace | String | 否 | 'global' | 命名空间 | - -**返回值**: - -| 类型 | 描述 | +#### 数据结构 +| 层级 | 类型 | 说明 | +|------|------|------| +| 第一层 key | string | 语言标识,如 zh-CN、en-US | +| 第二层 key | string | 命名空间,如 global、user | +| 第三层 | object | 翻译消息键值对 | + +### IntlProvider +react-intl 的国际化 Provider 组件,提供完整的国际化上下文支持。 + +#### 属性说明 +| 属性名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| locale | string | 是 | - | 当前语言 | +| messages | object | 是 | - | 翻译消息对象 | +| children | ReactNode | 是 | - | 子组件 | + +### 默认导出 +默认导出为 createIntl 函数,用于创建国际化实例。 + +#### 配置参数 +| 参数名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| locale | string | 否 | zh-CN | 语言标识 | +| message | object | 否 | - | 翻译消息对象 | +| namespace | string | 否 | global | 命名空间 | + +#### 返回值 +| 类型 | 说明 | |------|------| -| Object | 更新后的完整消息对象 | - -**示例**: - -```jsx -import {localeLoader} from '@kne/react-intl'; - -// 加载中文消息 -localeLoader('zh-CN', { - welcome: '欢迎使用我们的应用' -}, 'app'); - -// 加载英文消息 -localeLoader('en-US', { - welcome: 'Welcome to our application' -}, 'app'); -``` - -#### messagesLoader - -批量加载多语言的国际化消息。 - -| 参数 | 类型 | 必填 | 默认值 | 描述 | -|------|------|------|--------|------| -| messages | Object | 是 | - | 多语言消息对象,格式为 {locale: messages} | -| namespace | String | 否 | 'global' | 命名空间 | - -**返回值**:无 - -**示例**: - -```jsx -import {messagesLoader} from '@kne/react-intl'; - -// 批量加载多语言消息 -messagesLoader({ - 'zh-CN': { - welcome: '欢迎使用', - goodbye: '再见' - }, - 'en-US': { - welcome: 'Welcome', - goodbye: 'Goodbye' - } -}, 'app'); -``` - -### 从react-intl导出的API - -以下API直接从react-intl导出,详细用法请参考[react-intl官方文档](https://formatjs.io/docs/react-intl/)。 - -| API | 类型 | 描述 | -|-----|------|------| -| IntlProvider | Component | 国际化Provider组件 | -| FormattedDate | Component | 格式化日期组件 | -| FormattedTime | Component | 格式化时间组件 | -| FormattedNumber | Component | 格式化数字组件 | -| FormattedPlural | Component | 格式化复数组件 | -| FormattedMessage | Component | 格式化消息组件 | -| FormattedHTMLMessage | Component | 格式化HTML消息组件 | -| FormattedRelativeTime | Component | 格式化相对时间组件 | -| useIntl | Hook | 获取intl对象的hook | -| injectIntl | HOC | 注入intl对象的高阶组件 | -| defineMessages | Function | 定义消息的辅助函数 | -| createIntl | Function | 创建intl对象的函数 | - -### 内部API - -#### message - -存储所有已加载的国际化消息的对象。 - -**结构**: - -```javascript -{ - [locale: string]: { - [namespace: string]: { - [messageId: string]: string - } - } -} -``` - -**示例**: - -```javascript -// 内部结构示例 -{ - 'zh-CN': { - 'global': { - 'hello': '你好' - }, - 'app': { - 'welcome': '欢迎使用' - } - }, - 'en-US': { - 'global': { - 'hello': 'Hello' - }, - 'app': { - 'welcome': 'Welcome' - } - } -} -``` - -**注意**:此对象通常不需要直接访问,应通过提供的API函数进行操作。 -### \ No newline at end of file +| IntlShape | react-intl 的国际化实例对象 | diff --git a/doc/example.json b/doc/example.json index 09c534e..3b28cd2 100644 --- a/doc/example.json +++ b/doc/example.json @@ -2,8 +2,8 @@ "isFull": false, "list": [ { - "title": "这里填写示例标题", - "description": "这里填写示例说明", + "title": "基础国际化", + "description": "使用createIntlProvider创建国际化Provider,支持语言切换", "code": "./base.js", "scope": [ { diff --git a/doc/summary.md b/doc/summary.md index 820e908..d550856 100644 --- a/doc/summary.md +++ b/doc/summary.md @@ -1,143 +1,7 @@ -@kne/react-intl是一个基于[react-intl](https://formatjs.io/docs/react-intl/)的React国际化封装组件库,提供了更简便的API和更灵活的使用方式。它与@kne/global-context深度集成,支持从上下文获取语言设置,并提供了命名空间支持,使得国际化资源的管理更加方便。 +这是一个基于 react-intl 的 React 国际化组件库,提供便捷的国际化解决方案。通过封装 Provider 和工具函数,简化了多语言应用的开发流程,支持灵活的命名空间管理和动态语言切换。 -### 主要特性 +核心特性包括:开箱即用的国际化 Provider 工厂函数、支持命名空间的消息隔离、自动加载和管理翻译消息、与全局上下文集成实现自动语言检测、完整的 TypeScript 类型定义支持。组件库兼容 react-intl 的所有功能,同时提供了更简洁的 API 设计。 -- **简化的API**:封装react-intl,提供更简洁直观的API -- **命名空间支持**:支持通过命名空间隔离和组织国际化消息 -- **上下文集成**:与@kne/global-context无缝集成,支持从上下文获取locale -- **灵活的Provider**:支持多种方式创建和使用国际化Provider -- **动态消息加载**:支持运行时动态加载和更新国际化消息 -- **函数式组件支持**:支持将intl实例作为render props传递 +适用于需要快速实现国际化功能的 React 应用,特别适合中大型项目的多语言支持,包括电商网站、SaaS 平台、企业后台管理系统等场景。支持组件级和全局级的语言配置,可以根据不同模块独立管理翻译内容。 -### 安装 - -```bash -npm install @kne/react-intl -``` - -### 基本使用 - -#### 1. 使用createIntlProvider创建Provider - -```jsx -import {createIntlProvider} from '@kne/react-intl'; - -// 方式1:使用对象配置 -const IntlProvider = createIntlProvider({ - defaultLocale: 'zh-CN', - messages: { - 'zh-CN': { - hello: '你好,世界!' - }, - 'en-US': { - hello: 'Hello, world!' - } - }, - namespace: 'app' -}); - -// 方式2:使用参数配置 -const IntlProvider = createIntlProvider('zh-CN', { - hello: '你好,世界!' -}, 'app'); - -// 在应用中使用 -const App = () => ( - - - -); - -// 使用render props获取intl实例 -const App = () => ( - - {(intl) => ( -
{intl.formatMessage({id: 'hello'})}
- )} -
-); -``` - -#### 2. 使用createWithIntlProvider创建高阶组件 - -```jsx -import {createWithIntlProvider} from '@kne/react-intl'; - -const withIntl = createWithIntlProvider({ - defaultLocale: 'zh-CN', - defaultMessage: { - hello: '你好,世界!' - }, - namespace: 'app' -}); - -// 包装组件 -const WrappedComponent = withIntl(YourComponent); -``` - -#### 3. 动态加载国际化消息 - -```jsx -import {localeLoader, messagesLoader} from '@kne/react-intl'; - -// 加载单个语言的消息 -localeLoader('zh-CN', { - welcome: '欢迎使用' -}, 'app'); - -// 批量加载多个语言的消息 -messagesLoader({ - 'zh-CN': { - welcome: '欢迎使用' - }, - 'en-US': { - welcome: 'Welcome' - } -}, 'app'); -``` - -#### 4. 与@kne/global-context集成 - -```jsx -import {createIntlProvider} from '@kne/react-intl'; -import {createContext} from '@kne/global-context'; - -const {useContext, ContextProvider} = createContext(); - -const IntlProvider = createIntlProvider({ - defaultLocale: 'zh-CN', - messages: { - 'zh-CN': { - hello: '你好' - } - } -}); - -// locale将从context中获取 -const App = () => ( - - - - - -); -``` - -### 最佳实践 - -1. **使用命名空间**: - - 为不同模块使用不同的命名空间,避免消息键冲突 - - 保持命名空间结构清晰,便于管理 - -2. **动态加载**: - - 按需加载国际化消息,减少初始加载大小 - - 使用messagesLoader批量加载相关消息 - -3. **上下文集成**: - - 优先使用上下文管理locale - - 在需要时才通过props覆盖locale - -4. **消息组织**: - - 使用有意义的消息键 - - 保持消息结构扁平化 - - 适当使用消息格式化功能 +技术亮点在于提供了多种创建方式,createIntlProvider 用于创建完整的国际化上下文,createWithIntlProvider 可以为单个组件注入国际化能力,createIntl 则适用于非组件环境的国际化场景。消息加载器支持单个和批量加载,命名空间机制确保不同模块的翻译内容互不干扰。 diff --git a/package.json b/package.json index 5b678e7..92c47a4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kne/react-intl", - "version": "0.1.9", + "version": "0.1.10", "description": "快捷地创建国际化中需要使用到的组件", "syntax": { "esmodules": true @@ -8,6 +8,7 @@ "main": "dist/index.js", "module": "dist/index.modern.js", "source": "src/index.js", + "types": "dist/index.d.ts", "scripts": { "init": "husky && npm run init-example", "start": "run-p start:lib start:md start:example", @@ -15,10 +16,13 @@ "init-example": "modules-dev-libs-init", "build:md": "npx @kne/md-doc", "start:md": "npx @kne/md-doc --watch", - "build:lib": "microbundle --no-compress --format modern,cjs --jsx React.createElement --jsxFragment React.Fragment", + "build:lib-main": "microbundle --no-compress --format modern,cjs --jsx React.createElement --jsxFragment React.Fragment", + "build:lib": "run-s build:lib-main build:types build:package-manifest", "start:lib": "microbundle watch --no-compress --format modern,cjs --jsx React.createElement --jsxFragment React.Fragment", "build:example": "cd example && npm run build", "start:example": "cd example && npm run start", + "build:types": "cp src/index.d.ts dist/", + "build:package-manifest": "cp src/package-manifest.json dist/", "test:build": "run-s build", "test:lint": "eslint .", "test:unit": "cross-env CI=1 react-scripts test --env=jsdom", diff --git "a/prompts/\345\221\275\345\220\215\347\244\272\344\276\213\347\274\226\345\206\231\347\244\272\344\276\213\346\217\217\350\277\260.md" "b/prompts/\345\221\275\345\220\215\347\244\272\344\276\213\347\274\226\345\206\231\347\244\272\344\276\213\346\217\217\350\277\260.md" new file mode 100644 index 0000000..c7952d3 --- /dev/null +++ "b/prompts/\345\221\275\345\220\215\347\244\272\344\276\213\347\274\226\345\206\231\347\244\272\344\276\213\346\217\217\350\277\260.md" @@ -0,0 +1,50 @@ +# 任务:完善示例文档的标题和描述 + +## 目标 +根据 `doc/example.json` 中 `code` 字段引用的代码实现内容,完善该文件中的 `title` 和 `description` 字段。 + +## 具体要求 + +### 1. 分析代码实现 +- 仔细阅读 `doc/example.json` 中每个示例项的 `code` 字段引用的代码文件 +- 理解每个代码示例的核心功能和实现方式 +- 识别组件的主要用途和特点 + +### 2. 编写标题 (title) +- **简洁明了**:用4-8个字概括示例的核心功能 +- **突出重点**:体现示例的最主要特性 +- **用户视角**:从使用者的角度命名 +- **避免技术术语**:使用通俗易懂的表达 + +### 3. 编写描述 (description) +- **功能说明**:清晰描述示例展示的功能 +- **使用场景**:说明适用的业务场景 +- **特点优势**:突出实现的特点或优势 +- **长度控制**:建议在20-50字之间 + +## 示例格式 + +### 原始结构 +```json +{ + "code": "./form-modal.js", + "title": "", + "description": "" +} +``` + +### 优化后示例 +```json +{ + "code": "./form-modal.js", + "title": "模态框表单", + "description": "在模态框中展示表单,支持弹窗内数据提交和验证" +} +``` + +## 注意事项 +- 不要修改 `code` 字段的路径引用 +- 确保每个示例都有完整的 `title` 和 `description` +- 描述要准确反映代码实现的功能 +- 保持所有示例的命名风格一致 +- 重点突出用户体验和实用价值 \ No newline at end of file diff --git "a/prompts/\346\267\273\345\212\240ts\347\261\273\345\236\213\345\243\260\346\230\216.md" "b/prompts/\346\267\273\345\212\240ts\347\261\273\345\236\213\345\243\260\346\230\216.md" new file mode 100644 index 0000000..f6117f6 --- /dev/null +++ "b/prompts/\346\267\273\345\212\240ts\347\261\273\345\236\213\345\243\260\346\230\216.md" @@ -0,0 +1,103 @@ +# 为React组件库添加TypeScript类型定义的通用提示词 + +## 任务描述 + +为一个已有的React组件库添加完整的TypeScript类型定义,包括所有组件的Props接口、类型声明和导出。 + +## 实施步骤 + +### 1. 创建类型定义文件 + +在源代码目录创建`index.d.ts`文件,位置与主入口文件`index.js`同级。 + +### 2. 分析组件结构 + +- 查看所有组件文件的Props参数 +- 确定每个组件的输入属性类型 +- 识别回调函数的参数和返回值类型 + +### 3. 定义基础类型 + +```typescript +import { ReactNode, ComponentType, FC } from 'react'; +``` + +### 4. 为每个组件创建Props接口 + +- 组件名 + Props 作为接口名(如:FormInfoProps) +- 所有可选属性使用`?`标记 +- ReactNode类型用于支持字符串和JSX元素 +- 函数类型明确定义参数和返回值 +- 使用`[key: string]: any`支持动态属性 + +### 5. 处理复杂场景 + +- **嵌套对象属性**:定义内联对象类型 +- **函数属性**:明确参数类型和返回值类型 +- **联合类型**:使用`|`支持多种类型 +- **泛型组件**:使用ComponentType + +### 6. 导出组件类型声明 + +```typescript +export declare const ComponentName: FC; +``` + +## 类型定义模板 + +### 基础组件Props模板 + +```typescript +export interface ComponentNameProps { + className?: string; + children?: ReactNode; + + // 其他属性... + [key: string]: any; // 支持动态属性 +} +``` + +### 函数回调模板 + +```typescript +onEvent ? : (data: any, context: any, ...args: any[]) => Promise | any; +``` + +### 组件声明模板 + +```typescript +export declare const ComponentName: FC; +``` + +## 最佳实践 + +1. **类型完整性**:覆盖所有组件的公开API +2. **向后兼容**:保持现有JavaScript代码的正常运行 +3. **可选属性**:合理使用可选属性,提供良好的开发体验 +4. **文档注释**:为复杂类型添加JSDoc注释 +5. **版本控制**:在package.json中指定types字段指向类型文件 + +## 使用场景 + +- 为现有组件库添加TypeScript支持 +- 提升代码可维护性和开发体验 +- 支持IDE智能提示和类型检查 +- 便于团队协作和代码审查 + +## 示例项目结构 + +``` +src/ +├── index.js # 主入口文件 +├── index.d.ts # 类型定义文件(新增) +├── ComponentA.js # 组件A +├── ComponentB.js # 组件B +└── ... +``` + +## 注意事项 + +- 类型定义文件应与实际组件实现保持同步 +- 考虑使用更具体的类型替代`any`类型 +- 定期检查和更新类型定义以匹配组件API的变化 +- 对于复杂的组件,考虑拆分类型定义到多个文件中 \ No newline at end of file diff --git "a/prompts/\347\224\237\346\210\220\345\214\205\345\212\237\350\203\275\346\217\217\350\277\260\346\226\207\344\273\266.md" "b/prompts/\347\224\237\346\210\220\345\214\205\345\212\237\350\203\275\346\217\217\350\277\260\346\226\207\344\273\266.md" new file mode 100644 index 0000000..39dfcd3 --- /dev/null +++ "b/prompts/\347\224\237\346\210\220\345\214\205\345\212\237\350\203\275\346\217\217\350\277\260\346\226\207\344\273\266.md" @@ -0,0 +1,179 @@ +# 生成包功能描述文件(package-manifest.json)的通用提示词 + +## 任务描述 + +为一个JavaScript/TypeScript包项目生成`package-manifest.json`文件,该文件用于描述包的主要功能、导出模块、组件API等详细信息。 + +## 实施步骤 + +### 1. 分析项目结构 + +- 查看`package.json`了解包的基本信息 +- 分析主入口文件(`index.js`/`index.ts`)的导出内容 +- 检查源代码目录结构,识别所有可导出的模块/组件 + +### 2. 识别导出模块 + +- 默认导出:通常标记为`default` +- 命名导出:所有具名导出的组件、函数、类等 + +### 3. 分析每个模块的功能 + +对于每个导出的模块,需要收集以下信息: + +#### React组件 +- **description**: 组件的主要功能描述 +- **type**: 通常为`ReactNode` +- **props**: 组件的所有属性,包括: + - 属性名 + - 属性描述 + - 属性类型(支持联合类型,如`string|number`,不能使用下面`类型定义规范`定义之外的类型) + +#### 函数/类 +- **description**: 功能描述 +- **type**: 返回值类型 +- **parameters**: 参数列表(如果有) +- **properties**: 类属性(如果是类) + +#### 常量/对象 +- **description**: 用途描述 +- **type**: 数据类型 +- **properties**: 对象属性结构 + +### 4. 构建JSON结构 + +```json +{ + "description": "包的主要导出模块入口", + "modules": { + "模块名": { + "description": "模块功能描述", + "type": "类型声明", + // 根据模块类型可能包含以下字段 + "props": {}, // React组件属性 + "parameters": [], // 函数参数 + "properties": {}, // 对象/类属性 + "returns": "返回值类型" // 函数返回值 + } + } +} +``` + +## 类型定义规范 + +### 基础类型 +- `string`: 字符串 +- `number`: 数字 +- `boolean`: 布尔值 +- `object`: 对象 +- `array`: 数组 +- `function`: 函数 +- `Date`: 日期对象 +- `Promise`: Promise对象 +- `RegExp`: 正则表达式 +- `Error`: 错误对象 +- `undefined`: 未定义 +- `null`: 空值 +- `any`: 任意类型 +- `void`: 无返回值 +- `unknown`: 未知类型 +- `ReactNode`: React节点(组件专用) + +### 联合类型 +使用`|`连接多种类型: +- `string|ReactNode`: 字符串或React节点 +- `number|object`: 数字或对象 +- `function|null`: 函数或null + +### 复杂类型 +- `object[]`: 对象数组 +- `ReactNode[]`: React节点数组 +- `string[]`: 字符串数组 + +## 优先级排序 + +1. **主要组件**: 包中最核心、最常用的组件 +2. **辅助组件**: 支持性组件 +3. **工具函数**: 纯函数、工具方法 +4. **类型定义**: TypeScript类型、接口 +5. **常量配置**: 配置对象、常量 + +## 属性命名约定 + +### 通用属性 +- `className`: CSS类名 +- `children`: 子元素 +- `style`: 样式对象 +- `title`: 标题 +- `description`: 描述 + +## 描述编写原则 + +1. **简洁明确**: 一句话说明核心功能 +2. **用户视角**: 从使用者的角度描述 +3. **突出价值**: 说明解决了什么问题 +4. **避免实现细节**: 不描述内部实现逻辑 + +## 示例格式 + +### React组件示例 +```json +{ + "FormModal": { + "description": "模态框表单组件,在弹窗中展示表单", + "type": "ReactNode", + "props": { + "open": { + "description": "是否打开模态框", + "type": "boolean" + }, + "title": { + "description": "模态框标题", + "type": "ReactNode" + }, + "onSubmit": { + "description": "提交回调函数", + "type": "function" + } + } + } +} +``` + +### 工具函数示例 +```json +{ + "formatDate": { + "description": "格式化日期字符串", + "type": "string", + "parameters": [ + { + "name": "format", + "description": "格式化模板", + "type": "string" + } + ] + } +} +``` + +## 文件位置 + +将生成的`package-manifest.json`文件放置在: +- `src/package-manifest.json` + +## 使用场景 + +- 自动化API文档生成 +- IDE智能提示和自动补全 +- 包管理和分发平台的组件展示 +- 团队协作时的API参考 +- 第三方工具的集成和分析 + +## 注意事项 + +- 避免包含内部实现细节 +- 不要包含私有方法和属性 +- 确保向后兼容性 +- 定期更新以匹配代码变更 +- 考虑国际化需求(如需要) \ No newline at end of file diff --git "a/prompts/\347\224\237\346\210\220\346\226\207\346\241\243.md" "b/prompts/\347\224\237\346\210\220\346\226\207\346\241\243.md" new file mode 100644 index 0000000..b02891b --- /dev/null +++ "b/prompts/\347\224\237\346\210\220\346\226\207\346\241\243.md" @@ -0,0 +1,79 @@ +# 任务:生成项目文档 + +## 目标 +根据代码实现生成完整的项目文档,包括项目概述文件 (`doc/summary.md`) 和 API 文档 (`doc/api.md`)。 + +## 文档生成要求 + +### doc/summary.md - 项目概述 +#### 格式要求 +- **禁止使用 h1、h2 标题**:直接从内容开始编写 +- **无需标题**:不需要"项目概述"等标题文字 +- **不包含依赖项说明**:专注于项目特点和功能介绍 +- **吸引用户**:突出项目优势和使用价值 + +#### 内容结构 +1. **项目简介**:简明扼要介绍项目的主要功能和定位 +2. **核心特性**:列出3-5个最突出的特点或优势 +3. **适用场景**:描述适用的业务场景和使用场景 +4. **技术亮点**:重点说明技术创新或设计优势 + +#### 写作风格 +- **简洁明了**:用通俗语言表达,避免过度技术化 +- **用户导向**:从使用者角度描述价值 +- **重点突出**:强调解决什么问题,带来什么便利 + +### doc/api.md - API 文档 +#### 格式要求 +- **无需标题**:不添加"API文档"等标题 +- **使用 h3 及以下级别**:组件标题用 h3,子部分用 h4、h5 +- **优先使用表格格式**:API 参数和属性用表格展示 +- **不包含示例代码**:专注于 API 说明,不提供代码示例 + +#### 内容结构 +```markdown +### 组件名称 +组件功能描述文字 + +#### 属性说明 +| 属性名 | 类型 | 必填 | 默认值 | 说明 | +|--------|------|------|--------|------| +| prop1 | string | 是 | - | 属性说明 | +| prop2 | number | 否 | 0 | 属性说明 | + +#### 方法说明 +| 方法名 | 参数 | 返回值 | 说明 | +|--------|------|--------|------| +| method1 | data: object | void | 方法说明 | +``` + +#### API 文档要点 +- **完整性**:涵盖所有公开的 API +- **准确性**:类型、必填项、默认值要准确 +- **清晰性**:说明文字简洁明确 +- **一致性**:命名和格式保持一致 + +## 生成流程 + +### 1. 分析代码结构 +- 阅读 `src/index.js` 了解导出的组件 +- 分析组件的 TypeScript 类型定义 (`src/index.d.ts`) +- 理解组件的属性和功能 + +### 2. 提取 API 信息 +- 从组件代码中提取 props 定义 +- 识别方法的参数和返回值 +- 确定属性的类型和默认值 + +### 3. 编写文档内容 +- 按照格式要求编写两个文档 +- 确保内容准确性和完整性 +- 检查格式规范符合要求 + +## 严格注意事项 +- **禁止使用 h1、h2 标签**:严格遵守 markdown 标题级别限制 +- **summary.md 无标题**:直接从内容开始 +- **api.md 无文档标题**:直接从第一个组件 h3 标题开始 +- **使用表格格式**:API 说明必须使用表格 +- **不包含示例代码**:专注于文档说明 +- **保持格式一致性**:确保所有文档格式符合要求 \ No newline at end of file diff --git "a/prompts/\347\224\237\346\210\220\350\257\255\350\250\200\345\214\205.md" "b/prompts/\347\224\237\346\210\220\350\257\255\350\250\200\345\214\205.md" new file mode 100644 index 0000000..e583148 --- /dev/null +++ "b/prompts/\347\224\237\346\210\220\350\257\255\350\250\200\345\214\205.md" @@ -0,0 +1,125 @@ +# 任务:生成和更新国际化语言包 + +## 目标 +从代码中抽取中文文案,生成完整的国际化语言包,并同步更新到英文语言包。 + +## 任务要求 + +### 1. 抽取中文文案 +#### 扫描范围 +- **组件文件**:扫描 `src/` 目录下的所有 `.js` 组件文件 +- **模板字符串**:提取模板字符串中的中文文本 +- **字符串字面量**:提取字符串常量中的中文内容 +- **JSX 文本**:提取 JSX 中的中文显示文本 + +#### 抽取规则 +- **纯中文文本**:只提取包含中文字符的字符串 +- **过滤无关内容**:忽略 CSS 类名、技术术语、调试信息 +- **保持原意**:确保提取的文案保持原始含义 +- **去重处理**:相同文案只保留一个 key + +### 2. 生成语言包结构 +#### zh-CN.js 格式 +```javascript +const locale = { + // 已有的 key 保持不变 + submit: '提交', + cancel: '取消', + + // 新增的 key + addText: '添加', + deleteText: '删除', + // ... 其他新增文案 +}; + +export default locale; +``` + +#### Key 命名规范 +- **驼峰命名**:使用 camelCase 命名规则 +- **语义化**:key 名称要能表达文案含义 +- **简洁明了**:避免过长的 key 名称 +- **一致性**:相似功能使用相同命名模式 + +### 3. 翻译到英文版本 +#### 翻译原则 +- **准确翻译**:确保英文翻译准确传达中文含义 +- **自然表达**:使用符合英文习惯的表达方式 +- **保持一致**:相同概念使用一致的英文词汇 +- **简洁明了**:避免冗长复杂的表达 + +#### en-US.js 格式 +```javascript +const locale = { + // 与 zh-CN.js 对应的英文翻译 + submit: 'Submit', + cancel: 'Cancel', + complete: 'Complete', + next: 'Next', + addText: 'Add', + deleteText: 'Delete', + // ... 其他对应的英文文案 +}; + +export default locale; +``` + +## 实施步骤 + +### 第一步:代码扫描 +1. 遍历 `src/` 目录下的所有组件文件 +2. 使用正则表达式匹配中文文本 +3. 收集所有中文文案并去重 +4. 过滤掉不需要国际化的内容 + +### 第二步:更新中文语言包 +1. 打开 `src/locale/zh-CN.js` +2. 保留现有的 key-value 对 +3. 添加新发现的文案和对应的 key +4. 确保格式正确,导出语句完整 + +### 第三步:更新英文语言包 +1. 打开 `src/locale/en-US.js` +2. 为所有中文 key 添加对应的英文翻译 +3. 确保翻译准确、自然、一致 +4. 保持与中文版本相同的 key 结构 + +### 第四步:质量检查 +1. **完整性检查**:确保中文和英文版本 key 一致 +2. **准确性检查**:验证翻译的准确性 +3. **格式检查**:确保文件格式符合要求 +4. **语法检查**:确保 JavaScript 语法正确 + +## 严格注意事项 +- **不修改原始文件**:绝对不要修改任何组件源代码 +- **保留现有内容**:不要删除或修改已有的语言包内容 +- **保持文件结构**:维持两个语言包文件的完整结构 +- **确保导出正确**:保持 `export default locale` 语句 +- **格式一致性**:确保代码格式和缩进一致 + +## 常见中文文案类型 +- **按钮文本**:确认、取消、添加、删除、保存等 +- **提示信息**:成功、失败、警告、信息提示等 +- **表单标签**:用户名、密码、邮箱、手机号等 +- **操作描述**:编辑、查看、删除、复制等 +- **状态说明**:加载中、已完成、进行中等 +- **错误信息**:必填项验证、格式错误、网络错误等 + +## 输出格式示例 +```javascript +// zh-CN.js +const locale = { + // 原有内容 + submit: '提交', + cancel: '取消', + + // 新增内容 + confirmDelete: '确认删除', + deleteSuccess: '删除成功', + addNewItem: '添加新项目', + loading: '加载中...', + required: '此字段为必填项' +}; + +export default locale; +``` \ No newline at end of file diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 0000000..e589e9e --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,56 @@ +import { ReactNode, ComponentType, FC, ForwardRefExoticComponent, RefAttributes, PropsWithoutRef } from 'react'; +import { IntlProvider as IntlProviderBase, IntlShape, createIntl as createIntlBase, useIntl as useIntlBase } from 'react-intl'; + +declare module '@kne/global-context' { + export const useGlobalValue: (key: string) => any; +} + +// loader 类型定义 +export declare const localeLoader: (locale: string, defaultMessage: Record, namespace?: string) => void; +export declare const message: Record>>; +export declare const messagesLoader: (messages: Record>, namespace?: string) => void; + +// withIntlProvider Props +export interface WithIntlProviderProps { + locale?: string; + namespace?: string; + children?: ReactNode; + [key: string]: any; +} + +// withIntlProvider 类型 +export declare const withIntlProvider:

(WrappedComponents: ComponentType

) => ForwardRefExoticComponent & RefAttributes>; + +// argsParse 参数类型 +export interface ArgsParseOptions { + defaultLocale?: string; + defaultMessage?: Record; + namespace?: string; + messages?: Record>; +} + +// createIntlProvider 组件 Props +export interface IntlProviderProps { + locale?: string; + children?: ReactNode | ((intl: IntlShape) => ReactNode); +} + +// createIntlProvider 返回值类型 +export declare const createIntlProvider: (...args: any[]) => FC; + +// createWithIntlProvider 返回值类型 +export declare const createWithIntlProvider: (...args: any[]) =>

(WrappedComponents: ComponentType

) => ForwardRefExoticComponent & RefAttributes>; + +// createIntl 参数类型 +export interface CreateIntlOptions { + locale?: string; + message?: Record; + namespace?: string; +} + +// createIntl 返回值类型 +export declare const createIntl: (options: CreateIntlOptions) => IntlShape; + +// 默认导出 +declare const defaultExport: typeof createIntl; +export default defaultExport; diff --git a/src/index.js b/src/index.js index b316b09..ab7ced9 100644 --- a/src/index.js +++ b/src/index.js @@ -1,12 +1,12 @@ import React, { forwardRef } from 'react'; import localeLoader, { message, messagesLoader } from './loader'; import { IntlProvider, createIntl as createIntlBase, useIntl } from 'react-intl'; -import { useContext } from '@kne/global-context'; +import { useGlobalValue } from '@kne/global-context'; const withIntlProvider = WrappedComponents => forwardRef(({ locale: propsLocale, namespace, ...props }, ref) => { - const context = useContext(); - const locale = propsLocale || context?.locale || 'zh-CN'; + const contextLocal = useGlobalValue('locale'); + const locale = propsLocale || contextLocal || 'zh-CN'; return ( @@ -31,8 +31,8 @@ export const createIntlProvider = (...args) => { return children(intl); }; return ({ locale: propsLocale, children }) => { - const context = useContext(); - const locale = propsLocale || context?.locale || defaultLocale || 'zh-CN'; + const contextLocal = useGlobalValue('locale'); + const locale = propsLocale || contextLocal || defaultLocale || 'zh-CN'; return ( {typeof children === 'function' ? {children} : children} @@ -47,8 +47,8 @@ export const createWithIntlProvider = (...args) => { messages && messagesLoader(messages, namespace); return WrappedComponents => forwardRef(({ locale: propsLocale, ...props }, ref) => { - const context = useContext(); - const locale = propsLocale || context?.locale || defaultLocale || 'zh-CN'; + const contextLocal = useGlobalValue('locale'); + const locale = propsLocale || contextLocal || defaultLocale || 'zh-CN'; return ( diff --git a/src/package-manifest.json b/src/package-manifest.json new file mode 100644 index 0000000..d36856d --- /dev/null +++ b/src/package-manifest.json @@ -0,0 +1,115 @@ +{ + "description": "React国际化组件库,提供便捷的国际化Provider和工具函数", + "modules": { + "createIntlProvider": { + "description": "创建国际化Provider组件,为子组件提供i18n上下文", + "type": "function", + "parameters": [ + { + "name": "args", + "description": "配置参数,支持默认语言、默认消息、命名空间或多语言消息对象", + "type": "string|object|array" + } + ], + "returns": "function" + }, + "createWithIntlProvider": { + "description": "创建高阶组件,为被包装组件添加国际化能力", + "type": "function", + "parameters": [ + { + "name": "args", + "description": "配置参数,支持默认语言、默认消息、命名空间或多语言消息对象", + "type": "string|object|array" + } + ], + "returns": "function" + }, + "createIntl": { + "description": "创建国际化实例,用于在非组件环境中使用i18n功能", + "type": "function", + "parameters": [ + { + "name": "options", + "description": "国际化配置选项", + "type": "object" + } + ], + "returns": "object" + }, + "localeLoader": { + "description": "加载指定语言的翻译消息", + "type": "function", + "parameters": [ + { + "name": "locale", + "description": "语言标识,如zh-CN、en-US", + "type": "string" + }, + { + "name": "defaultMessage", + "description": "翻译消息对象", + "type": "object" + }, + { + "name": "namespace", + "description": "命名空间,默认为global", + "type": "string" + } + ], + "returns": "object" + }, + "messagesLoader": { + "description": "批量加载多语言消息对象", + "type": "function", + "parameters": [ + { + "name": "messages", + "description": "多语言消息对象,key为语言标识", + "type": "object" + }, + { + "name": "namespace", + "description": "命名空间,默认为global", + "type": "string" + } + ], + "returns": "void" + }, + "message": { + "description": "存储所有已加载的翻译消息的容器对象", + "type": "object", + "properties": {} + }, + "IntlProvider": { + "description": "react-intl的国际化Provider组件", + "type": "ReactNode", + "props": { + "locale": { + "description": "当前语言", + "type": "string" + }, + "messages": { + "description": "翻译消息对象", + "type": "object" + }, + "children": { + "description": "子组件", + "type": "ReactNode" + } + } + }, + "default": { + "description": "默认导出,同createIntl函数", + "type": "function", + "parameters": [ + { + "name": "options", + "description": "国际化配置选项", + "type": "object" + } + ], + "returns": "object" + } + } +}