# json-trans
[![PyPI version](https://badge.fury.io/py/json-trans.svg)](https://badge.fury.io/py/json-trans)
[![Python Support](https://img.shields.io/pypi/pyversions/json-trans.svg)](https://pypi.org/project/json-trans/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
A Python tool for translating JSON files from English to Chinese, supporting multiple translation APIs.
一个支持多种翻译API的JSON文件英译中工具。
## Features | 特性
- 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支持
- 全面的测试覆盖
## Installation | 安装
```bash
pip install json-trans
```
## Quick Start | 快速开始
### Using Baidu Translate API | 使用百度翻译API
```python
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 | 必需
)
```
### Using Google Cloud Translation API | 使用谷歌云翻译API
```python
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 | 可选
)
```
### Using Google Gemini API | 使用谷歌 Gemini API
```python
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 | 可选
)
```
## Configuration | 配置
### Environment Variables | 环境变量
You can set up environment variables for different environments:
可以为不同环境设置环境变量:
```bash
# 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
```
### API Configuration | API配置
1. Baidu Translate API:
- Register at [Baidu Translate](http://api.fanyi.baidu.com/api/trans/product/desktop)
- Get your APP ID and Secret Key
2. Google Cloud Translation API:
- Create a project in [Google Cloud Console](https://console.cloud.google.com/)
- Enable the Cloud Translation API
- Create a service account and download credentials
3. Google Gemini API:
- Get API key from [Google AI Studio](https://makersuite.google.com/app/apikey)
- Choose appropriate model (default: gemini-2.0-flash-exp)
## Development | 开发
### Setup | 设置
```bash
# 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
```
### Testing | 测试
```bash
# 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
```
## Contributing | 贡献
1. Fork the repository | 复刻仓库
2. Create your feature branch | 创建特性分支
3. Commit your changes | 提交更改
4. Push to the branch | 推送到分支
5. Open a Pull Request | 开启拉取请求
## License | 许可证
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
本项目采用MIT许可证 - 查看[LICENSE](LICENSE)文件了解详情。
## Authors | 作者
- CuiZhengPeng & Liuyaowen
## Changelog | 更新日志
### 0.2.0
- Added Google Gemini API support
- Added environment variables support
- Improved test coverage
- Added custom model selection for Gemini API
### 0.1.1
- Initial release
- Support for Baidu and Google Cloud Translation APIs
- Basic JSON translation functionality
## Acknowledgments | 致谢
- Thanks to Baidu Translate API, Google Cloud Translation API, and Google Gemini API for providing translation services
- Built with [Poetry](https://python-poetry.org/)
Raw data
{
"_id": null,
"home_page": "https://github.com/liyown/json-trans",
"name": "json-trans",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": "json, translation, chinese, baidu, google, gemini",
"author": "CuiZhengPeng&Liuyaowen",
"author_email": "liuyaowen_smile@126.com",
"download_url": "https://files.pythonhosted.org/packages/69/bb/f57cfadbcd121865f3b6dd8762949c3c0083d41b8fabedbbbf4269340169/json_trans-0.2.0.tar.gz",
"platform": null,
"description": "# json-trans\n\n[![PyPI version](https://badge.fury.io/py/json-trans.svg)](https://badge.fury.io/py/json-trans)\n[![Python Support](https://img.shields.io/pypi/pyversions/json-trans.svg)](https://pypi.org/project/json-trans/)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nA Python tool for translating JSON files from English to Chinese, supporting multiple translation APIs.\n\n\u4e00\u4e2a\u652f\u6301\u591a\u79cd\u7ffb\u8bd1API\u7684JSON\u6587\u4ef6\u82f1\u8bd1\u4e2d\u5de5\u5177\u3002\n\n## Features | \u7279\u6027\n\n- Translate JSON files while preserving structure\n- Support for multiple translation services:\n - Baidu Translate API\n - Google Cloud Translation API\n - Google Gemini API\n- Automatic handling of nested JSON structures\n- Customizable fields for translation\n- Type hints for better IDE support\n- Comprehensive test coverage\n\n---\n\n- \u5728\u4fdd\u6301\u7ed3\u6784\u7684\u540c\u65f6\u7ffb\u8bd1JSON\u6587\u4ef6\n- \u652f\u6301\u591a\u79cd\u7ffb\u8bd1\u670d\u52a1\uff1a\n - \u767e\u5ea6\u7ffb\u8bd1API\n - \u8c37\u6b4c\u4e91\u7ffb\u8bd1API\n - \u8c37\u6b4c Gemini API\n- \u81ea\u52a8\u5904\u7406\u5d4c\u5957\u7684JSON\u7ed3\u6784\n- \u652f\u6301\u81ea\u5b9a\u4e49\u7ffb\u8bd1\u5b57\u6bb5\n- \u63d0\u4f9b\u7c7b\u578b\u63d0\u793a\u4ee5\u83b7\u5f97\u66f4\u597d\u7684IDE\u652f\u6301\n- \u5168\u9762\u7684\u6d4b\u8bd5\u8986\u76d6\n\n## Installation | \u5b89\u88c5\n\n```bash\npip install json-trans\n```\n\n## Quick Start | \u5feb\u901f\u5f00\u59cb\n\n### Using Baidu Translate API | \u4f7f\u7528\u767e\u5ea6\u7ffb\u8bd1API\n\n```python\nfrom json_trans import translate_json_baidu\n\ntranslate_json_baidu(\n input_file=\"input.json\",\n output_file=\"output.json\",\n app_id=\"your_baidu_app_id\",\n secret_key=\"your_baidu_secret_key\",\n fields_to_translate=[\"title\", \"content\", \"description\"] # Required | \u5fc5\u9700\n)\n```\n\n### Using Google Cloud Translation API | \u4f7f\u7528\u8c37\u6b4c\u4e91\u7ffb\u8bd1API\n\n```python\nfrom json_trans import translate_json_google\n\ntranslate_json_google(\n input_file=\"input.json\",\n output_file=\"output.json\",\n fields_to_translate=[\"summary\", \"details\", \"text\"], # Required | \u5fc5\u9700\n credentials_path=\"path/to/google_credentials.json\" # Optional | \u53ef\u9009\n)\n```\n\n### Using Google Gemini API | \u4f7f\u7528\u8c37\u6b4c Gemini API\n\n```python\nfrom json_trans import translate_json_gemini\n\n# Basic usage | \u57fa\u672c\u7528\u6cd5\ntranslate_json_gemini(\n input_file=\"input.json\",\n output_file=\"output.json\",\n api_key=\"your_gemini_api_key\",\n fields_to_translate=[\"title\", \"content\", \"description\"]\n)\n\n# With custom model and role | \u4f7f\u7528\u81ea\u5b9a\u4e49\u6a21\u578b\u548c\u89d2\u8272\ntranslate_json_gemini(\n input_file=\"input.json\",\n output_file=\"output.json\",\n api_key=\"your_gemini_api_key\",\n fields_to_translate=[\"title\", \"content\", \"description\"],\n model=\"gemini-pro\", # Optional | \u53ef\u9009\n role=\"translator\" # Optional | \u53ef\u9009\n)\n```\n\n## Configuration | \u914d\u7f6e\n\n### Environment Variables | \u73af\u5883\u53d8\u91cf\n\nYou can set up environment variables for different environments:\n\u53ef\u4ee5\u4e3a\u4e0d\u540c\u73af\u5883\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff1a\n\n```bash\n# Baidu Translate API credentials\nBAIDU_APP_ID=your_baidu_app_id\nBAIDU_SECRET_KEY=your_baidu_secret_key\n\n# Google Gemini API configuration\nGEMINI_API_KEY=your_gemini_api_key\nGEMINI_MODEL=gemini-2.0-flash-exp\n\n# Google Cloud Translation credentials path\nGOOGLE_CREDENTIALS_PATH=path/to/your/credentials.json\n```\n\n### API Configuration | API\u914d\u7f6e\n\n1. Baidu Translate API:\n - Register at [Baidu Translate](http://api.fanyi.baidu.com/api/trans/product/desktop)\n - Get your APP ID and Secret Key\n\n2. Google Cloud Translation API:\n - Create a project in [Google Cloud Console](https://console.cloud.google.com/)\n - Enable the Cloud Translation API\n - Create a service account and download credentials\n\n3. Google Gemini API:\n - Get API key from [Google AI Studio](https://makersuite.google.com/app/apikey)\n - Choose appropriate model (default: gemini-2.0-flash-exp)\n\n## Development | \u5f00\u53d1\n\n### Setup | \u8bbe\u7f6e\n\n```bash\n# Clone the repository | \u514b\u9686\u4ed3\u5e93\ngit clone https://github.com/liyown/json-trans.git\ncd json-trans\n\n# Install dependencies | \u5b89\u88c5\u4f9d\u8d56\npoetry install\n\n# Set up environment variables | \u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\ncp .env.example .env\n# Edit .env with your credentials | \u7f16\u8f91 .env \u586b\u5165\u60a8\u7684\u51ed\u8bc1\n\n# Run tests | \u8fd0\u884c\u6d4b\u8bd5\npoetry run pytest\n```\n\n### Testing | \u6d4b\u8bd5\n\n```bash\n# Run all tests | \u8fd0\u884c\u6240\u6709\u6d4b\u8bd5\npoetry run pytest\n\n# Run with coverage report | \u8fd0\u884c\u5e76\u751f\u6210\u8986\u76d6\u7387\u62a5\u544a\npoetry run pytest --cov\n\n# Run specific test | \u8fd0\u884c\u7279\u5b9a\u6d4b\u8bd5\npoetry run pytest tests/test_translator.py::test_json_translator_init\n```\n\n## Contributing | \u8d21\u732e\n\n1. Fork the repository | \u590d\u523b\u4ed3\u5e93\n2. Create your feature branch | \u521b\u5efa\u7279\u6027\u5206\u652f\n3. Commit your changes | \u63d0\u4ea4\u66f4\u6539\n4. Push to the branch | \u63a8\u9001\u5230\u5206\u652f\n5. Open a Pull Request | \u5f00\u542f\u62c9\u53d6\u8bf7\u6c42\n\n## License | \u8bb8\u53ef\u8bc1\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n\u672c\u9879\u76ee\u91c7\u7528MIT\u8bb8\u53ef\u8bc1 - \u67e5\u770b[LICENSE](LICENSE)\u6587\u4ef6\u4e86\u89e3\u8be6\u60c5\u3002\n\n## Authors | \u4f5c\u8005\n\n- CuiZhengPeng & Liuyaowen\n\n## Changelog | \u66f4\u65b0\u65e5\u5fd7\n\n### 0.2.0\n- Added Google Gemini API support\n- Added environment variables support\n- Improved test coverage\n- Added custom model selection for Gemini API\n\n### 0.1.1\n- Initial release\n- Support for Baidu and Google Cloud Translation APIs\n- Basic JSON translation functionality\n\n## Acknowledgments | \u81f4\u8c22\n\n- Thanks to Baidu Translate API, Google Cloud Translation API, and Google Gemini API for providing translation services\n- Built with [Poetry](https://python-poetry.org/)\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A tool for translating JSON files from English to Chinese using multiple translation APIs",
"version": "0.2.0",
"project_urls": {
"Documentation": "https://github.com/liyown/json-trans#readme",
"Homepage": "https://github.com/liyown/json-trans",
"Repository": "https://github.com/liyown/json-trans"
},
"split_keywords": [
"json",
" translation",
" chinese",
" baidu",
" google",
" gemini"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "64c1e18bc67f22694c7e6adabf792891ca86146cec56ea6cfbcb58a7d1edbc89",
"md5": "5aff1d84f616bc538310898002b9f637",
"sha256": "a423d3a30f890aed6d1e5cb2871b5fa3f8ee612ff118ef9efc224cf18f9ad346"
},
"downloads": -1,
"filename": "json_trans-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5aff1d84f616bc538310898002b9f637",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 6333,
"upload_time": "2024-12-17T06:05:23",
"upload_time_iso_8601": "2024-12-17T06:05:23.058346Z",
"url": "https://files.pythonhosted.org/packages/64/c1/e18bc67f22694c7e6adabf792891ca86146cec56ea6cfbcb58a7d1edbc89/json_trans-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "69bbf57cfadbcd121865f3b6dd8762949c3c0083d41b8fabedbbbf4269340169",
"md5": "1db3062f352c0d0f9dd786a45b4f2bcb",
"sha256": "9f16b7638a1c00cfd5569929f7609e3bd95ff88a47e527bf34eb7f9953c175c3"
},
"downloads": -1,
"filename": "json_trans-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "1db3062f352c0d0f9dd786a45b4f2bcb",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 6398,
"upload_time": "2024-12-17T06:05:26",
"upload_time_iso_8601": "2024-12-17T06:05:26.158945Z",
"url": "https://files.pythonhosted.org/packages/69/bb/f57cfadbcd121865f3b6dd8762949c3c0083d41b8fabedbbbf4269340169/json_trans-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-17 06:05:26",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "liyown",
"github_project": "json-trans",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "json-trans"
}