https://github.com/2ue/i18n-x
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/2ue/i18n-x
- Owner: 2ue
- Created: 2025-06-20T09:48:47.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-08-21T07:01:51.000Z (10 months ago)
- Last Synced: 2025-08-29T11:29:21.299Z (10 months ago)
- Language: TypeScript
- Size: 613 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.en.md
Awesome Lists containing this project
README
# i18n-xy
A CLI tool for automatically extracting Chinese strings from React projects and internationalizing them. Supports intelligent string extraction, AST code transformation, key generation, automatic translation, and more.
## β¨ Features
- π **Automatic Extraction**: Intelligently identifies and extracts Chinese strings from JavaScript/TypeScript/JSX/TSX files
- π **Code Transformation**: Uses AST technology to precisely replace Chinese strings with internationalization function calls
- π― **Smart Key Generation**: Supports pinyin conversion, hash generation, duplicate key detection, and other strategies
- π **Automatic Translation**: Integrated with Baidu Translate API for batch translation of internationalization files
- βοΈ **Flexible Configuration**: Rich configuration options to meet different project requirements
- π **File Management**: Supports temporary directory processing to avoid directly modifying source files
- π§ **Auto Import**: Configurable automatic addition of internationalization function import statements
- π **Detailed Logging**: Multi-level log output for easy debugging and monitoring
## π¦ Installation
```bash
# Global installation
npm install -g i18n-xy
# Or using pnpm
pnpm add -g i18n-xy
# Or using yarn
yarn global add i18n-xy
```
## π Quick Start
### 1. Initialize Configuration
```bash
# Initialize configuration file
i18n-xy init
# Or use short command
i18nx init
```
### 2. Extract Chinese Strings
```bash
# Use default configuration to extract
i18n-xy extract
# Specify configuration file
i18n-xy extract -c ./my-config.json
```
### 3. Translate Internationalization Files
```bash
# Batch translation
i18n-xy translate --batch
# Translate single text
i18n-xy translate --test -f zh -t en -i "δ½ ε₯½δΈη"
```
## βοΈ Basic Configuration
Configuration file example (`i18n.config.json`):
```json
{
"locale": "zh-CN",
"displayLanguage": "en-US",
"outputDir": "locales",
"include": [
"src/**/*.{js,jsx,ts,tsx}",
"pages/**/*.{js,jsx,ts,tsx}",
"components/**/*.{js,jsx,ts,tsx}"
],
"exclude": [
"node_modules/**",
"dist/**",
"build/**",
"**/*.test.{js,jsx,ts,tsx}"
],
"replacement": {
"functionName": "$t",
"autoImport": {
"enabled": true,
"insertPosition": "afterImports"
}
}
}
```
### Core Configuration Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `locale` | string | `"zh-CN"` | Source language |
| `outputDir` | string | `"locales"` | Internationalization file output directory |
| `include` | string[] | `["src/**/*.{js,jsx,ts,tsx}"]` | File patterns to process |
| `exclude` | string[] | `["node_modules/**"]` | File patterns to exclude |
| `replacement.functionName` | string | `"$t"` | Replacement function name |
## π οΈ Development
### Environment Requirements
- Node.js >= 16.0.0
- npm/pnpm/yarn
### Local Development
```bash
# Clone project
git clone
cd i18n-xy
# Install dependencies
pnpm install
# Development mode
pnpm dev
# Build
pnpm build
# Code check
pnpm lint
# Type check
pnpm type-check
```
### Project Structure
```
src/
βββ ast/ # AST processing core logic
βββ cli.ts # CLI command line entry
βββ config/ # Configuration management
βββ translation/ # Translation functionality
βββ utils/ # Utility functions
βββ vars/ # Variable management
```
## π Usage Examples
### Before Processing
```javascript
function Welcome({ name }) {
return (
ζ¬’θΏδ½Ώη¨ζ们ηη³»η»
η¨ζ·οΌ{name}οΌζ¨ε₯½οΌ
ηΉε»εΌε§
);
}
```
### After Processing
```javascript
import { useTranslation } from 'react-i18next';
function Welcome({ name }) {
const { t: $t } = useTranslation();
return (
{$t("huan_ying_shi_yong_wo_men_de_xi_tong")}
{$t("yong_hu")}οΌ{name}οΌ{$t("nin_hao")}οΌ
{$t("dian_ji_kai_shi")}
);
}
```
### Generated Internationalization File
`locales/zh-CN.json`:
```json
{
"huan_ying_shi_yong_wo_men_de_xi_tong": "ζ¬’θΏδ½Ώη¨ζ们ηη³»η»",
"yong_hu": "η¨ζ·",
"nin_hao": "ζ¨ε₯½",
"dian_ji_kai_shi": "ηΉε»εΌε§"
}
```
## π License
ISC
## π€ Contributing
Issues and Pull Requests are welcome!
---
For more detailed configuration and advanced features, please check the [documentation](./docs/).