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

如何在manifest文件(appscript.json)中本地化Gmail插件表头名称?

如何实现Gmail插件表头名称的本地化?

我正在开发一款支持多语言的Gmail插件,需要对插件的表头名称进行本地化处理。该表头名称定义在manifest文件(appscript.json)中,但我尚未找到根据用户语言对其进行本地化的方法。请问是否可以实现表头名称的本地化?能否有人协助我解决这个问题?

插件表头截图

以下是我的manifest文件内容:

{
  "oauthScopes": [
    "https://www.googleapis.com/auth/gmail.addons.current.action.compose",
    "https://www.googleapis.com/auth/gmail.addons.current.message.metadata",
    "https://www.googleapis.com/auth/gmail.addons.execute",
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/gmail.addons.current.message.readonly",
    "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/script.locale"
  ],
  "urlFetchWhitelist": [
    "https://abcd.com/"
  ],
  "runtimeVersion": "V8",
  "gmail": {
    "name": "Add-on name",
    "logoUrl": "https://abcd.com/gmail/assets/logo_32x32.png",
    "contextualTriggers": [
      {
        "unconditional": {},
        "onTriggerFunction": "buildAddon"
      }
    ],
    "primaryColor": "#4285F4",
    "secondaryColor": "#4d90fe",
    "openLinkUrlPrefixes": [
      "https://abcd.com/"
    ],
    "useLocaleFromApp": true
  },
  "exceptionLogging": "STACKDRIVER"
}

解决方案

可以实现Gmail插件表头名称的本地化,具体操作步骤如下:

  1. 创建多语言资源文件
    为需要支持的每种语言创建独立的JSON资源文件,命名格式为messages_{locale}.json,比如:

    • messages_en.json(英文)
    • messages_zh.json(中文)
    • 其他语言按对应locale代码命名

    资源文件内容示例(中文):

    {
      "addon_name": "插件名称",
      "header_title": "自定义表头"
    }
    
  2. 修改manifest配置
    将gmail.name的硬编码文本替换为资源文件中的键名,同时保留useLocaleFromApp: true配置,确保插件读取用户的Gmail语言环境:

    "gmail": {
      "name": "addon_name",
      // 其余配置保持不变
      "useLocaleFromApp": true
    }
    
  3. 动态构建本地化UI
    在buildAddon函数中,根据用户的语言环境加载对应资源,动态设置表头文本:

    function buildAddon(e) {
      // 获取用户语言,默认 fallback 到英文
      const userLocale = e.userLocale || "en";
      // 加载对应语言的资源
      const localeMessages = getLocaleMessages(userLocale);
    
      // 构建带本地化表头的卡片
      const card = CardService.newCardBuilder()
        .setHeader(CardService.newCardHeader().setTitle(localeMessages.header_title))
        // 添加其他卡片内容
        .build();
    
      return [card];
    }
    
    function getLocaleMessages(locale) {
      try {
        // 从脚本属性中读取对应语言的资源JSON
        const rawMessages = PropertiesService.getScriptProperties().getProperty(`messages_${locale}`);
        return JSON.parse(rawMessages);
      } catch (err) {
        // 加载失败时回退到英文资源
        const rawEnMessages = PropertiesService.getScriptProperties().getProperty("messages_en");
        return JSON.parse(rawEnMessages);
      }
    }
    
  4. 上传资源到脚本属性
    打开Google Apps Script编辑器的「项目设置」→「脚本属性」,将各语言资源文件的JSON内容添加进去,键名对应messages_en、messages_zh等,值为完整的JSON字符串。

注意:你的manifest已包含https://www.googleapis.com/auth/script.locale权限,这是获取用户语言环境的必要条件,无需额外修改。


内容的提问来源于stack exchange,提问作者Kvu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 04:35:21