A Python tool for translating JSON files from English to Chinese, supporting both Baidu Translate API and Google Cloud Translation API.
一个支持百度翻译API和谷歌云翻译API的JSON文件英译中工具。
- Translate JSON files while preserving structure
- Support for both Baidu and Google translation services
- Automatic handling of nested JSON structures
- Customizable fields for translation
- Type hints for better IDE support
- Comprehensive test coverage
- 在保持结构的同时翻译JSON文件
- 同时支持百度和谷歌翻译服务
- 自动处理嵌套的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 BaseTranslator, JsonTranslator
class MyCustomTranslator(BaseTranslator):
def translate_to_chinese(self, english_text: str) -> str:
# Implement your translation logic here | 在这里实现您的翻译逻辑
return translated_text
# Use your custom translator | 使用您的自定义翻译器
translator = MyCustomTranslator()
json_translator = JsonTranslator(
translator,
fields_to_translate=["title", "content", "description"] # Required | 必需
)
json_translator.translate_json_file("input.json", "output.json")
Input | 输入:
{
"title": "Product Manual",
"description": "User guide for the product",
"content": "Detailed content here",
"sections": [
{
"title": "Getting Started",
"description": "How to begin",
"text": "Step by step guide"
}
]
}
Output (with fields_to_translate=["description", "text"]
) | 输出:
{
"title": "Product Manual",
"description": "产品使用指南",
"content": "Detailed content here",
"sections": [
{
"title": "Getting Started",
"description": "如何开始",
"text": "分步指南"
}
]
}
Main class for handling JSON translation.
用于处理JSON翻译的主类。
translator = JsonTranslator(translator_instance)
translator.translate_json_file(input_filename, output_filename)
Implementation for Baidu Translation API.
百度翻译API的实现。
translator = BaiduTranslator(app_id, secret_key)
result = translator.translate_to_chinese(text)
Implementation for Google Cloud Translation API.
谷歌云翻译API的实现。
translator = GoogleTranslator(credentials_path=None)
result = translator.translate_to_chinese(text)
translate_json_baidu(input_file, output_file, app_id, secret_key)
translate_json_google(input_file, output_file, credentials_path=None)
- Register at Baidu Translate
- Get your APP ID and Secret Key
- Use these credentials in your code
- 在百度翻译注册
- 获取您的APP ID和密钥
- 在代码中使用这些凭证
- Create a project in Google Cloud Console
- Enable the Cloud Translation API
- Create a service account and download credentials
- Either:
- Set GOOGLE_APPLICATION_CREDENTIALS environment variable
- Or provide credentials_path in code
- 在谷歌云控制台创建项目
- 启用云翻译API
- 创建服务账号并下载凭证
- 选择以下方式之一:
- 设置GOOGLE_APPLICATION_CREDENTIALS环境变量
- 或在代码中提供credentials_path
# Clone the repository | 克隆仓库
git clone https://github.com/yourusername/json-trans.git
cd json-trans
# Install dependencies | 安装依赖
poetry install
# 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 | 创建特性分支 (
git checkout -b feature/amazing-feature
) - Commit your changes | 提交更改 (
git commit -m 'Add some amazing feature'
) - Push to the branch | 推送到分支 (
git push origin feature/amazing-feature
) - Open a Pull Request | 开启拉取请求
This project is licensed under the MIT License - see the LICENSE file for details.
本项目采用MIT许可证 - 查看LICENSE文件了解详情。
- CuiZhengPeng & Liuyaowen
- Thanks to Baidu Translate API and Google Cloud Translation API for providing translation services
- Built with Poetry
- 感谢百度翻译API和谷歌云翻译API提供的翻译服务
- 使用Poetry构建