Can I use getActiveUserLocale() in onOpen(e) of a published Add-on?

163 Views Asked by At

Google shows at https://developers.google.com/apps-script/reference/base/session#getactiveuserlocale how to get the Locale for the active user.

I have an editor (not G-Suite) add-on that I want to make multilingual so that when the user opens the Sheet, which triggers the onOpen(e) function, the Add-on checks the user's locale and then translates the Add-on into the user's language.

Questions:

  1. Can I do this in order to translate the menu into other languages?
function onInstall(e) {
  onOpen();
}
function onOpen(e) {
  var lang = Session.getActiveUserLocale().split("_")[0];
  if (lang === 'en'){
  SpreadsheetApp.getUi().createMenu('test')
  .addItem('Publish', 'upload')
  .addToUi();
  }
  else{
  SpreadsheetApp.getUi().createMenu('test')
  .addItem(LanguageApp.translate('Publish posts','en',lang), 'uploadPosts') 
  .addToUi();
  } 
}
  1. Do I need to add the Locale scope: https://www.googleapis.com/auth/script.locale to my manifest file as well? When I published the Add-on without the scope, users were seeing a blank menu dropdown.
  2. If I do need the scope, why isn't documented at https://developers.google.com/apps-script/reference/base/session#getactiveuserlocale.
1

There are 1 best solutions below

3
Wicket On

The documentation doesn't mention that an authorization scope is required, so Session.getActiveUserLocale() can be called from onOpen.


As this is about the onOpen simple trigger on Google Apps Script add-on you should be aware about the add-on authorization live-cycle.

Basically you should consider the following cases

  1. Users who have installed the add-on on spreadsheets where the addon hasn't being enabled (authMode = NONE)
  2. Users who have installed the add-on on spreadsheets where the addon has being enabled (authmode = LIMITED)
  3. Users that are editors of an spreadsheet where the addon has being enable but who haven't installed it (authmode = LIMITED but the user have not authorized the script to be executed)
  4. onOpen being called by the active users (authMode = FULL)