Please enable Javascript to view the contents

开发者必备:微软翻译200万免费额度薅羊毛攻略

 ·  ☕ 3 分钟

字幕翻译 - 字幕工具箱

我最近升级了字幕翻译 - 字幕工具箱,新增加了微软翻译的功能,使用了微软翻译的API。

200M

翻译文本 API-微软商务翻译

如果你在中国, 你可以报名参加一个 每月免费200万字符订阅 在 azure. cn。 刚好,我就在中国。

申请流程

一、注册 Microsoft Azure 账户

  1. 访问官网并注册
    前往 Microsoft Azure 官网,选择“免费账户”或“开始使用”。若已有微软账户可直接登录,否则需填写个人信息并验证邮箱或手机号。

    • 学生用户:若使用教育邮箱(如 xxx@edu.cn),可通过 Azure for Students 申请,无需绑定信用卡即可获得 100 美元额度及 200 万字符/月的免费翻译额度。
    • 普通用户:需绑定国际信用卡(Visa/MasterCard)完成身份验证,但前 12 个月可免费使用每月 200 万字符的额度。
  2. 设置订阅计划
    注册完成后,进入 Azure 门户创建订阅:

    • 普通用户:选择“即用即付”订阅,避免 30 天试用期限制。
    • 学生用户:直接通过 Azure for Students 订阅,无需额外配置。

二、创建翻译资源

创建翻译资源

  1. 搜索并选择服务
    在 Azure 门户中点击“创建资源”,搜索“Translator Text”或“翻译”,选择对应的翻译服务。
  2. 配置资源参数
    • 订阅和资源组:选择已创建的订阅和资源组(若无,需新建资源组)。
    • 区域:推荐选择离你较近的节点(如东亚 East Asia)以优化延迟。
    • 定价层:选择 Free F0(免费层),该层提供每月 200 万字符的免费额度。
    • 名称:自定义资源名称(需全局唯一)。
  3. 完成创建
    确认配置后点击“审阅并创建”,等待资源部署完成。

三、获取 API 密钥与终结点

  1. 进入资源管理页面
    在 Azure 门户的“所有资源”中找到已创建的翻译资源,点击进入详情页。
  2. 复制密钥和终结点
    在“密钥和终结点”选项卡中,复制任一密钥及终结点 URL,后续调用 API 时需使用。

四、调用 API 示例(以 nodejs 为例)

Azure 服务本身也提供一个简单的在线试用工具,直接在浏览器中输入文本即可翻译。

在线测试

也提供了多种语言调用API 的示例代码,以下是 Node.js 的示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    const axios = require('axios').default;
    const { v4: uuidv4 } = require('uuid');

    let key = "<your-translator-key>";
    let endpoint = "https://api.cognitive.microsofttranslator.com";

    // location, also known as region.
    // required if you're using a multi-service or regional (not global) resource. It can be found in the Azure portal on the Keys and Endpoint page.
    let location = "<YOUR-RESOURCE-LOCATION>";

    axios({
        baseURL: endpoint
        url: '/translate'
        method: 'post'
        headers: {
            'Ocp-Apim-Subscription-Key': key
             // location required if you're using a multi-service or regional (not global) resource.
            'Ocp-Apim-Subscription-Region': location
            'Content-type': 'application/json'
            'X-ClientTraceId': uuidv4().toString()
        }
        params: {
            'api-version': '3.0'
            'from': 'en'
            'to': ['fr', 'zu']
        }
        data: [{
            'text': 'I would really like to drive your car around the block a few times!'
        }]
        responseType: 'json'
    }).then(function(response){
        console.log(JSON.stringify(response.data, null, 4));
    })

五、注意事项

  1. 免费额度限制
    • 免费额度仅限注册后的前 12 个月,超出后按 10 美元/100 万字符收费。
    • Free F0 层不支持文档翻译,需升级至付费层(如 S1)。
  2. 密钥安全
    密钥泄露可能导致超额费用,建议定期更换或启用 Azure 的监控工具。
  3. 归属权声明
    若将翻译结果展示给用户,需按微软要求标注“Translations by Microsoft® Translator”。

参考资料

分享

码农真经
作者
码农真经
Web Developer