简体   繁体   English

本地化 Google 插件

[英]Localizing Google Add-ons

We have a published Google Docs Add-on.我们有一个已发布的 Google 文档插件。 Where we need 5 different languages.我们需要 5 种不同的语言。 We are reading the Internationalizing Your App documentation.我们正在阅读Internationalizing Your App文档。 It's mainly describing how it can be done if you are publishing an Chrome Extension (uploading.zip).它主要描述了如果您要发布 Chrome 扩展程序 (uploading.zip),该怎么做。 However an Docs Add-on is written in Google Apps Script.然而,Docs 插件是用 Google Apps 脚本编写的。

As per documentation we need to add:根据文档我们需要添加:

manifest.json清单.json

messages.json消息.json

"and providing a _locales directory in your app's ZIP file" “并在您应用的 ZIP 文件中提供一个 _locales 目录”

Here is where we get trouble.这就是我们遇到麻烦的地方。 How can we add folders in Google Apps Script & how can we add.json files?我们如何在 Google Apps 脚本中添加文件夹以及如何添加 .json 文件? The only options are script-files & html-files.唯一的选择是脚本文件和 html 文件。

在此处输入图像描述

That documentation is NOT for Google Apps Script at all, but for Chrome Apps . 该文档根本不适用于Google Apps脚本 ,但适用于Chrome应用 So it does not directly apply. 所以它不直接适用。

Briefly looking through Google Apps Script docs, i18n is not mentioned. 简要介绍一下Google Apps脚本文档,未提及i18n。 It does not seem like there is any supporting library for it. 它似乎没有任何支持库。

Note that according to an earlier question on the topic you can get the user's locale with Session.getActiveUserLocale()) and implement your own i18n. 请注意,根据之前关于该主题的问题,您可以使用Session.getActiveUserLocale())获取用户的语言环境,并实现您自己的i18n。

However, that probably won't enable item i18n in the Web Store itself. 但是,这可能不会在Web Store本身启用项目i18n。 There's a bug report describing this situation . 有一个错误报告描述了这种情况

The bug report mentions that you could potentially throw in the locale fields/folders in the generated Chrome app by editing it directly using this procedure . 错误报告提到您可以通过使用此过程直接编辑生成的 Chrome应用程序中的区域设置字段/文件夹。 That much is not tested though. 尽管如此,还是没有经过测试。

Here is how we did it at Vocal .以下是我们在Vocal中的做法。 It's fairly simple and easy to implement (Our example supports english and spanish).它相当简单且易于实现(我们的示例支持英语和西班牙语)。

1. Create a dictionary with keys for the different strings in your app: 1. 为您的应用程序中的不同字符串创建一个包含键的字典:

const LANGUAGE_MAP = {
  "RECORD_MESSAGE": {
    "en": "Record message",
    "es": "Grabar mensaje"
  },
  "RECORD_MESSAGE_TEXT": {
    "en": "1. Click here to record your voice",
    "es": "1. Haga clic aquí para grabar su voz"
  }
}

2. Create a function that takes a key as input and returns the string in the proper language based on the user's locale This is done using Session.getActiveUserLocale() 2. 创建一个 function,它将一个键作为输入,并根据用户的区域设置以正确的语言返回字符串这是使用Session.getActiveUserLocale()完成的

function translate(key) {
  locale = Session.getActiveUserLocale();
  if (['en', 'es'].includes(locale)) {
  return LANGUAGE_MAP[key][locale]
  }
  else {
    return LANGUAGE_MAP[key]['en']
  }
}
  1. Replace all your bare strings with the function created above and the string as key.将所有裸字符串替换为上面创建的 function 并将该字符串作为键。
CardService.newTextParagraph().setText(translate("RECORD_MESSAGE_TEXT"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM