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:
- 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();
}
}
- 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.
- If I do need the scope, why isn't documented at https://developers.google.com/apps-script/reference/base/session#getactiveuserlocale.
The documentation doesn't mention that an authorization scope is required, so
Session.getActiveUserLocale()can be called fromonOpen.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
onOpenbeing called by the active users (authMode = FULL)