A Python tool for translating JSON files from English to Chinese, supporting multiple translation APIs.
一个支持多种翻译API的JSON文件英译中工具。
- Translate JSON files while preserving structure
- Support for multiple translation services:
- Baidu Translate API
- Google Cloud Translation API
- Google Gemini API
- Automatic handling of nested JSON structures
- Customizable fields for translation
- Type hints for better IDE support
- Comprehensive test coverage
- 在保持结构的同时翻译JSON文件
- 支持多种翻译服务:
- 百度翻译API
- 谷歌云翻译API
- 谷歌 Gemini API
- 自动处理嵌套的JSON结构
- 支持自定义翻译字段
- 提供类型提示以获得更好的IDE支持
- 全面的测试覆盖
pip install json-trans
from json_trans import translate_json_baidu
translate_json_baidu(
input_file="input.json",
output_file="output.json",
app_id="your_baidu_app_id",
secret_key="your_baidu_secret_key",
fields_to_translate=["title", "content", "description"] # Required | 必需
)
from json_trans import translate_json_google
translate_json_google(
input_file="input.json",
output_file="output.json",
fields_to_translate=["summary", "details", "text"], # Required | 必需
credentials_path="path/to/google_credentials.json" # Optional | 可选
)
from json_trans import translate_json_gemini
# Basic usage | 基本用法
translate_json_gemini(
input_file="input.json",
output_file="output.json",
api_key="your_gemini_api_key",
fields_to_translate=["title", "content", "description"]
)
# With custom model and role | 使用自定义模型和角色
translate_json_gemini(
input_file="input.json",
output_file="output.json",
api_key="your_gemini_api_key",
fields_to_translate=["title", "content", "description"],
model="gemini-pro", # Optional | 可选
role="translator" # Optional | 可选
)
You can set up environment variables for different environments: 可以为不同环境设置环境变量:
# Baidu Translate API credentials
BAIDU_APP_ID=your_baidu_app_id
BAIDU_SECRET_KEY=your_baidu_secret_key
# Google Gemini API configuration
GEMINI_API_KEY=your_gemini_api_key
GEMINI_MODEL=gemini-2.0-flash-exp
# Google Cloud Translation credentials path
GOOGLE_CREDENTIALS_PATH=path/to/your/credentials.json
-
Baidu Translate API:
- Register at Baidu Translate
- Get your APP ID and Secret Key
-
Google Cloud Translation API:
- Create a project in Google Cloud Console
- Enable the Cloud Translation API
- Create a service account and download credentials
-
Google Gemini API:
- Get API key from Google AI Studio
- Choose appropriate model (default: gemini-2.0-flash-exp)
# Clone the repository | 克隆仓库
git clone https://github.com/liyown/json-trans.git
cd json-trans
# Install dependencies | 安装依赖
poetry install
# Set up environment variables | 设置环境变量
cp .env.example .env
# Edit .env with your credentials | 编辑 .env 填入您的凭证
# Run tests | 运行测试
poetry run pytest
# Run all tests | 运行所有测试
poetry run pytest
# Run with coverage report | 运行并生成覆盖率报告
poetry run pytest --cov
# Run specific test | 运行特定测试
poetry run pytest tests/test_translator.py::test_json_translator_init
- Fork the repository | 复刻仓库
- Create your feature branch | 创建特性分支
- Commit your changes | 提交更改
- Push to the branch | 推送到分支
- Open a Pull Request | 开启拉取请求
This project is licensed under the MIT License - see the LICENSE file for details.
本项目采用MIT许可证 - 查看LICENSE文件了解详情。
- CuiZhengPeng & Liuyaowen
- Added Google Gemini API support
- Added environment variables support
- Improved test coverage
- Added custom model selection for Gemini API
- Initial release
- Support for Baidu and Google Cloud Translation APIs
- Basic JSON translation functionality
- Thanks to Baidu Translate API, Google Cloud Translation API, and Google Gemini API for providing translation services
- Built with Poetry