简体   繁体   中英

Localizing Google Add-ons

We have a published Google Docs Add-on. Where we need 5 different languages. We are reading the Internationalizing Your App documentation. It's mainly describing how it can be done if you are publishing an Chrome Extension (uploading.zip). However an Docs Add-on is written in Google Apps Script.

As per documentation we need to add:

manifest.json

messages.json

"and providing a _locales directory in your app's ZIP file"

Here is where we get trouble. How can we add folders in Google Apps Script & how can we add.json files? The only options are script-files & html-files.

在此处输入图像描述

That documentation is NOT for Google Apps Script at all, but for Chrome Apps . So it does not directly apply.

Briefly looking through Google Apps Script docs, i18n is not mentioned. 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.

However, that probably won't enable item i18n in the Web Store itself. 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 . That much is not tested though.

Here is how we did it at 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:

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

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.
CardService.newTextParagraph().setText(translate("RECORD_MESSAGE_TEXT"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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