Skip to main content

A tool for translating JSON files from English to Chinese using Baidu Translate API

Project description

json-trans

PyPI version Python Support License: MIT

A Python tool for translating JSON files from English to Chinese, supporting both Baidu Translate API and Google Cloud Translation API.

一个支持百度翻译API和谷歌云翻译API的JSON文件英译中工具。

Features | 特性

  • 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支持
  • 全面的测试覆盖

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 | 可选
)

Advanced Usage | 高级用法

Custom Translation Implementation | 自定义翻译实现

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")

Example JSON | 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": "分步指南"
        }
    ]
}

API Reference | API参考

Classes | 类

JsonTranslator

Main class for handling JSON translation.

用于处理JSON翻译的主类。

translator = JsonTranslator(translator_instance)
translator.translate_json_file(input_filename, output_filename)

BaiduTranslator

Implementation for Baidu Translation API.

百度翻译API的实现。

translator = BaiduTranslator(app_id, secret_key)
result = translator.translate_to_chinese(text)

GoogleTranslator

Implementation for Google Cloud Translation API.

谷歌云翻译API的实现。

translator = GoogleTranslator(credentials_path=None)
result = translator.translate_to_chinese(text)

Convenience Functions | 便捷函数

  • translate_json_baidu(input_file, output_file, app_id, secret_key)
  • translate_json_google(input_file, output_file, credentials_path=None)

Configuration | 配置

Baidu Translation API | 百度翻译API

  1. Register at Baidu Translate
  2. Get your APP ID and Secret Key
  3. Use these credentials in your code

  1. 百度翻译注册
  2. 获取您的APP ID和密钥
  3. 在代码中使用这些凭证

Google Cloud Translation API | 谷歌云翻译API

  1. Create a project in Google Cloud Console
  2. Enable the Cloud Translation API
  3. Create a service account and download credentials
  4. Either:
    • Set GOOGLE_APPLICATION_CREDENTIALS environment variable
    • Or provide credentials_path in code

  1. 谷歌云控制台创建项目
  2. 启用云翻译API
  3. 创建服务账号并下载凭证
  4. 选择以下方式之一:
    • 设置GOOGLE_APPLICATION_CREDENTIALS环境变量
    • 或在代码中提供credentials_path

Development | 开发

Setup | 设置

# Clone the repository | 克隆仓库
git clone https://github.com/yourusername/json-trans.git
cd json-trans

# Install dependencies | 安装依赖
poetry install

# Run tests | 运行测试
poetry run pytest

Running Tests | 运行测试

# 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 | 创建特性分支 (git checkout -b feature/amazing-feature)
  3. Commit your changes | 提交更改 (git commit -m 'Add some amazing feature')
  4. Push to the branch | 推送到分支 (git push origin feature/amazing-feature)
  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

Acknowledgments | 致谢

  • Thanks to Baidu Translate API and Google Cloud Translation API for providing translation services
  • Built with Poetry

  • 感谢百度翻译API和谷歌云翻译API提供的翻译服务
  • 使用Poetry构建

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json_trans-0.1.1.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json_trans-0.1.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file json_trans-0.1.1.tar.gz.

File metadata

  • Download URL: json_trans-0.1.1.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.7 Windows/11

File hashes

Hashes for json_trans-0.1.1.tar.gz
Algorithm Hash digest
SHA256 df85cffda1a918e54050e84d844ef2ccaf29ea855a4bc9cdc87b1c7b19d6ab05
MD5 374d69ba5fb7c5ab9b31f577fc4118eb
BLAKE2b-256 3a500bfde22f1ce2cbad7d42c1523a731e33f386e211729b0530bb74fca019f3

See more details on using hashes here.

File details

Details for the file json_trans-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: json_trans-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.7 Windows/11

File hashes

Hashes for json_trans-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e7397e6d6fd395bbb445647f076318a127abc48ee95d8596c5e7e93676e148b7
MD5 83f68b4dc273d07e834bb9698243c5d6
BLAKE2b-256 a5ac5ccf7b16fb965127e958a2263d013e62572fb51d1e7601d9d10c18526eec

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page