You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Google Translate API translate_v3的translate_document时glossary未生效

Google Translate API v3 translate_document 术语表不生效问题排查方案
  • 严格匹配术语表语言对:translate_document对术语表的源/目标语言匹配要求比translate_text更严格,必须与请求中的source_language_code、target_language_code完全一致,包括区域变体(例如zh-CN不能和zh-TW混用)。

  • 使用完整的术语表资源名称:确保glossary_config中的glossary字段传入的是完整的资源路径,而非仅术语表ID。示例路径格式:

    projects/{your-project-id}/locations/{your-location}/glossaries/{your-glossary-id}
    
  • 核对文档MIME类型:传入的mime_type必须与实际文档格式完全匹配,常见格式对应值:

    • Word文档:application/vnd.openxmlformats-officedocument.wordprocessingml.document
    • PDF:application/pdf
    • 纯文本:text/plain
  • 调整术语匹配规则:translate_document的术语匹配对大小写、标点敏感度更高。创建术语表时可设置case_insensitive=True启用大小写不敏感匹配;同时确保术语表条目与文档中的文本格式一致(避免标点、空格差异)。

  • 检查请求参数结构:translate_document的请求结构与translate_text存在差异,需确保glossary_config挂载层级正确。完整请求示例:

    from google.cloud import translate_v3 as translate
    
    client = translate.TranslationServiceClient()
    parent = f"projects/{project_id}/locations/{location}"
    glossary_name = client.glossary_path(project_id, location, glossary_id)
    
    # 读取待翻译文档
    with open("source.docx", "rb") as doc_file:
        doc_content = doc_file.read()
    
    request = {
        "parent": parent,
        "document": {
            "content": doc_content,
            "mime_type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        },
        "source_language_code": "en-US",
        "target_language_code": "zh-CN",
        "glossary_config": {"glossary": glossary_name}
    }
    
    response = client.translate_document(request=request)
    
    # 保存翻译结果
    with open("translated.docx", "wb") as output_file:
        output_file.write(response.document_translation.byte_stream_outputs[0])
    
  • 验证术语表状态:确保术语表状态为ACTIVE,可通过API查询术语表详情确认;若术语表创建时间较久,尝试重新创建并同步条目。

内容的提问来源于stack exchange,提问作者Roland Bühler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 07:17:30