Skip to main content

json-trans

PyPI version Python Support License: 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 | 安装

pip install json-trans

Quick Start | 快速开始

Using Baidu Translate API | 使用百度翻译API

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

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

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: 可以为不同环境设置环境变量:

# 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:

  2. Google Cloud Translation API:

    • Create a project in Google Cloud Console
    • Enable the Cloud Translation API
    • Create a service account and download credentials
  3. Google Gemini API:

    • Get API key from Google AI Studio
    • Choose appropriate model (default: gemini-2.0-flash-exp)

Development | 开发

Setup | 设置

# 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 | 测试

# 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 file for details.

本项目采用MIT许可证 - 查看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

Release files for json-trans 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-trans 0.2.1
File Size Uploaded
json_trans-0.2.1.tar.gz 43.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for json-trans 0.2.1
File Interpreter ABI Platform
json_trans-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size:50.0 kB

Release files / json_trans-0.2.1.tar.gz

Download URL json_trans-0.2.1.tar.gz
Size 43.1 kB
Tags Source
SHA-256 checksum
How to use checksums
49a1dbd22969ebf5d20a3e42cda91c8c3c91d3260c5d35e5524bc9472f1d5d18
BLAKE2b-256 checksum
How to use checksums
1a9a88442656742f155cefe89b4661b52965cc01fbf2622e07911d1204e1efe5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.14

Release files / json_trans-0.2.1-py3-none-any.whl

Download URL json_trans-0.2.1-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cc399044d726a2038592bfd1704d73c5ae81a4fb5fe56c0ddd7ac35e02a12fea
BLAKE2b-256 checksum
How to use checksums
73228e9bba57a1ddf51e6d47bd780ded64976ef0af054704481a7e8c170421f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.14

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page