feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
239
frontend/packages/arch/resources/studio-i18n-resource/README.md
Normal file
239
frontend/packages/arch/resources/studio-i18n-resource/README.md
Normal file
@@ -0,0 +1,239 @@
|
||||
# @coze-studio/studio-i18n-resource-adapter
|
||||
|
||||
> Comprehensive internationalization (i18n) resource adapter for Coze Studio applications
|
||||
|
||||
This package provides a centralized internationalization resource adapter containing localized strings and type definitions for Coze Studio applications. It supports multiple languages and provides type-safe access to translation keys with parameter interpolation.
|
||||
|
||||
## Features
|
||||
|
||||
- 🌍 **Multi-language Support**: Currently supports English (`en`) and Simplified Chinese (`zh-CN`)
|
||||
- 🔒 **Type Safety**: Comprehensive TypeScript definitions for all translation keys and parameters
|
||||
- 📦 **Auto-generated**: Resources are automatically generated using the `dl-i18n` command
|
||||
- ⚡ **React Integration**: Built-in support for React components with `ReactNode` parameter types
|
||||
- 🎯 **Parameter Interpolation**: Support for dynamic content insertion with type-safe parameters
|
||||
- 📝 **Extensive Coverage**: Over 13,000+ translation strings covering the entire Coze Studio ecosystem
|
||||
|
||||
## Get Started
|
||||
|
||||
### Installation
|
||||
|
||||
This package is part of the Coze Studio monorepo and should be installed via Rush:
|
||||
|
||||
```bash
|
||||
# Add to your package.json dependencies
|
||||
{
|
||||
"dependencies": {
|
||||
"@coze-studio/studio-i18n-resource-adapter": "workspace:*"
|
||||
}
|
||||
}
|
||||
|
||||
# Install dependencies
|
||||
rush update
|
||||
```
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```typescript
|
||||
import {
|
||||
localeEn,
|
||||
localeZhCN,
|
||||
defaultConfig,
|
||||
type I18nKeysHasOptionsType,
|
||||
type I18nKeysNoOptionsType
|
||||
} from '@coze-studio/studio-i18n-resource-adapter';
|
||||
|
||||
// Use individual locale data
|
||||
console.log(localeEn.AddSuccessToast); // Access English translation
|
||||
console.log(localeZhCN.AddSuccessToast); // Access Chinese translation
|
||||
|
||||
// Use default configuration object
|
||||
const currentLocale = 'en';
|
||||
const translations = defaultConfig[currentLocale].i18n;
|
||||
```
|
||||
|
||||
### With i18n Libraries
|
||||
|
||||
```typescript
|
||||
import { defaultConfig } from '@coze-studio/studio-i18n-resource-adapter';
|
||||
import i18n from 'i18next';
|
||||
|
||||
// Initialize with react-i18next
|
||||
i18n.init({
|
||||
resources: defaultConfig,
|
||||
lng: 'en',
|
||||
fallbackLng: 'en',
|
||||
defaultNS: 'i18n',
|
||||
interpolation: {
|
||||
escapeValue: false
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
## API Reference
|
||||
|
||||
### Exports
|
||||
|
||||
#### `localeEn`
|
||||
English locale data object containing all translation strings.
|
||||
|
||||
```typescript
|
||||
import { localeEn } from '@coze-studio/studio-i18n-resource-adapter';
|
||||
|
||||
// Simple string access
|
||||
const message = localeEn.account_login_success;
|
||||
```
|
||||
|
||||
#### `localeZhCN`
|
||||
Simplified Chinese locale data object containing all translation strings.
|
||||
|
||||
```typescript
|
||||
import { localeZhCN } from '@coze-studio/studio-i18n-resource-adapter';
|
||||
|
||||
// Simple string access
|
||||
const message = localeZhCN.account_login_success;
|
||||
```
|
||||
|
||||
#### `defaultConfig`
|
||||
Pre-configured object structure ready for use with i18n libraries.
|
||||
|
||||
```typescript
|
||||
import { defaultConfig } from '@coze-studio/studio-i18n-resource-adapter';
|
||||
|
||||
// Structure: { [locale]: { i18n: translations } }
|
||||
const config = defaultConfig;
|
||||
// config.en.i18n contains English translations
|
||||
// config['zh-CN'].i18n contains Chinese translations
|
||||
```
|
||||
|
||||
### Type Definitions
|
||||
|
||||
#### `I18nKeysHasOptionsType`
|
||||
Union type of all translation keys that require parameters for interpolation.
|
||||
|
||||
```typescript
|
||||
import { type I18nKeysHasOptionsType } from '@coze-studio/studio-i18n-resource-adapter';
|
||||
|
||||
// Example keys that require parameters:
|
||||
// - 'AddSuccessToast' (requires: { name: ReactNode })
|
||||
// - 'Coze_token_body' (requires: { num: number })
|
||||
// - 'account_merge_oauth_success2' (requires: { phone_number: string })
|
||||
```
|
||||
|
||||
#### `I18nKeysNoOptionsType`
|
||||
Union type of all translation keys that don't require parameters.
|
||||
|
||||
```typescript
|
||||
import { type I18nKeysNoOptionsType } from '@coze-studio/studio-i18n-resource-adapter';
|
||||
|
||||
// Example keys without parameters:
|
||||
// - 'account_login_success'
|
||||
// - 'bot_create_success'
|
||||
// - 'workflow_save_success'
|
||||
```
|
||||
|
||||
#### `I18nOptionsMap`
|
||||
Interface mapping each parameterized translation key to its required parameters.
|
||||
|
||||
```typescript
|
||||
import { type I18nOptionsMap } from '@coze-studio/studio-i18n-resource-adapter';
|
||||
|
||||
// Type-safe parameter access
|
||||
type AddSuccessParams = I18nOptionsMap['AddSuccessToast']; // { name: ReactNode }
|
||||
type TokenBodyParams = I18nOptionsMap['Coze_token_body']; // { num: ReactNode }
|
||||
```
|
||||
|
||||
#### `LocaleData`
|
||||
Type definition for the structure of locale data objects.
|
||||
|
||||
```typescript
|
||||
import { type LocaleData } from '@coze-studio/studio-i18n-resource-adapter';
|
||||
|
||||
// Use for typing custom locale objects
|
||||
const customLocale: LocaleData = {
|
||||
// ... your translation keys
|
||||
};
|
||||
```
|
||||
|
||||
### Parameter Interpolation Examples
|
||||
|
||||
```typescript
|
||||
import { localeEn, type I18nOptionsMap } from '@coze-studio/studio-i18n-resource-adapter';
|
||||
|
||||
// With react-i18next
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
function MyComponent() {
|
||||
const { t } = useTranslation();
|
||||
|
||||
// Type-safe parameter usage
|
||||
const successMessage = t('AddSuccessToast', {
|
||||
name: 'My Bot'
|
||||
} satisfies I18nOptionsMap['AddSuccessToast']);
|
||||
|
||||
const tokenMessage = t('Coze_token_body', {
|
||||
num: 100
|
||||
} satisfies I18nOptionsMap['Coze_token_body']);
|
||||
|
||||
return <div>{successMessage}</div>;
|
||||
}
|
||||
```
|
||||
|
||||
## Development
|
||||
|
||||
### Regenerating Resources
|
||||
|
||||
The locale files and type definitions are auto-generated. To update them:
|
||||
|
||||
```bash
|
||||
# Run the i18n download command
|
||||
pnpm dl-i18n
|
||||
```
|
||||
|
||||
This will:
|
||||
- Download the latest translations from the source
|
||||
- Generate updated `en.json` and `zh-CN.json` files
|
||||
- Regenerate the `locale-data.d.ts` type definitions
|
||||
- Update the main `index.ts` export file
|
||||
|
||||
### Adding New Languages
|
||||
|
||||
To add support for additional languages:
|
||||
|
||||
1. Add the new locale JSON file to `src/locales/`
|
||||
2. Import and export it in `src/index.ts`
|
||||
3. Update the `defaultConfig` object to include the new locale
|
||||
4. Regenerate types using `pnpm dl-i18n`
|
||||
|
||||
### Project Structure
|
||||
|
||||
```
|
||||
src/
|
||||
├── index.ts # Main exports and default configuration
|
||||
├── locale-data.d.ts # Auto-generated TypeScript definitions
|
||||
└── locales/
|
||||
├── en.json # English translations (~13K+ strings)
|
||||
└── zh-CN.json # Chinese translations (~14K+ strings)
|
||||
```
|
||||
|
||||
## Dependencies
|
||||
|
||||
### Runtime Dependencies
|
||||
- No runtime dependencies (pure data package)
|
||||
|
||||
### Peer Dependencies
|
||||
- `react` ~18.2.0 - Required for ReactNode type support
|
||||
- `react-dom` ~18.2.0 - React DOM integration
|
||||
|
||||
### Development Dependencies
|
||||
- `@coze-arch/eslint-config` - Linting configuration
|
||||
- `@coze-arch/ts-config` - TypeScript configuration
|
||||
- `@coze-arch/vitest-config` - Testing configuration
|
||||
- `@types/node` ^18 - Node.js type definitions
|
||||
|
||||
## License
|
||||
|
||||
This package is part of the Coze Studio monorepo and follows the same licensing terms as the main project.
|
||||
|
||||
---
|
||||
|
||||
**Note**: This package is automatically generated and maintained. Direct modifications to locale files or type definitions will be overwritten during the next `dl-i18n` command execution.
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"operationSettings": [
|
||||
{
|
||||
"operationName": "ts-check",
|
||||
"outputFolderNames": ["./lib-ts"]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
const { defineConfig } = require('@coze-arch/eslint-config');
|
||||
|
||||
module.exports = defineConfig({
|
||||
packageRoot: __dirname,
|
||||
preset: 'web',
|
||||
rules: {},
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "@coze-studio/studio-i18n-resource-adapter",
|
||||
"version": "0.0.1",
|
||||
"author": "chenjiawei.inizio@bytedance.com",
|
||||
"main": "./src/index.ts",
|
||||
"scripts": {
|
||||
"build": "exit 0",
|
||||
"lint": "eslint ./ --cache",
|
||||
"test": "exit 0",
|
||||
"test:cov": "exit 0"
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"@coze-arch/eslint-config": "workspace:*",
|
||||
"@coze-arch/ts-config": "workspace:*",
|
||||
"@coze-arch/vitest-config": "workspace:*",
|
||||
"@types/node": "^18"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": "~18.2.0",
|
||||
"react-dom": "~18.2.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/* eslint-disable */
|
||||
// 由 dl-i18n 命令自动生成
|
||||
import localeEn from './locales/en.json';
|
||||
import localeZhCN from './locales/zh-CN.json';
|
||||
|
||||
const defaultConfig = {
|
||||
en: { 'i18n': localeEn },
|
||||
'zh-CN': { 'i18n': localeZhCN },
|
||||
} as { en: { 'i18n': typeof localeEn }; 'zh-CN': { 'i18n': typeof localeZhCN }};
|
||||
|
||||
export { localeEn, localeZhCN, defaultConfig };
|
||||
export type { I18nOptionsMap, I18nKeysHasOptionsType, I18nKeysNoOptionsType, LocaleData } from './locale-data';
|
||||
16177
frontend/packages/arch/resources/studio-i18n-resource/src/locale-data.d.ts
vendored
Normal file
16177
frontend/packages/arch/resources/studio-i18n-resource/src/locale-data.d.ts
vendored
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/tsconfig",
|
||||
"extends": "@coze-arch/ts-config/tsconfig.node.json",
|
||||
"compilerOptions": {
|
||||
"jsx": "preserve",
|
||||
"useUnknownInCatchVariables": false,
|
||||
"rootDir": "./src",
|
||||
"outDir": "./dist",
|
||||
"tsBuildInfoFile": "dist/tsconfig.build.tsbuildinfo"
|
||||
},
|
||||
"include": ["src", "src/**/*.json"],
|
||||
"references": [
|
||||
{
|
||||
"path": "../../../../config/eslint-config/tsconfig.build.json"
|
||||
},
|
||||
{
|
||||
"path": "../../../../config/ts-config/tsconfig.build.json"
|
||||
},
|
||||
{
|
||||
"path": "../../../../config/vitest-config/tsconfig.build.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/tsconfig",
|
||||
"compilerOptions": {
|
||||
"composite": true
|
||||
},
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.build.json"
|
||||
},
|
||||
{
|
||||
"path": "./tsconfig.misc.json"
|
||||
}
|
||||
],
|
||||
"exclude": ["**/*"]
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"extends": "@coze-arch/ts-config/tsconfig.node.json",
|
||||
"$schema": "https://json.schemastore.org/tsconfig",
|
||||
"include": ["__tests__", "vitest.config.ts"],
|
||||
"exclude": ["./dist"],
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.build.json"
|
||||
}
|
||||
],
|
||||
"compilerOptions": {
|
||||
"jsx": "preserve",
|
||||
"rootDir": "./",
|
||||
"outDir": "./dist",
|
||||
"useUnknownInCatchVariables": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { defineConfig } from '@coze-arch/vitest-config';
|
||||
|
||||
export default defineConfig({
|
||||
dirname: __dirname,
|
||||
preset: 'web',
|
||||
test: {
|
||||
coverage: {
|
||||
all: true,
|
||||
exclude: ['starling.config.js', 'src/resource', 'script/dl-i18n.js'],
|
||||
},
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user