Skip to content

Instantly share code, notes, and snippets.

@pfelipm
Last active February 7, 2021 21:54
Show Gist options
  • Save pfelipm/2c42ac475fd9ff6a631b13e7e4e579a2 to your computer and use it in GitHub Desktop.
Save pfelipm/2c42ac475fd9ff6a631b13e7e4e579a2 to your computer and use it in GitHub Desktop.
A simple and straightforward way to manage message translations in Google Apps Script projects
// This object stores text strings in several languages inside sub-objects {ES}, {EN_US}...
const TRANSLATIONS = {
ES: {
MSG_HELLO: 'Hola',
MSG_GOODBYE: 'Adiós'
},
EN_US: {
MSG_HELLO: 'Hello',
MSG_GOODBYE: 'Goodbye'
}
// More translations...
};
// Session.getActiveUserLocale() is not working properly at this moment in the new IDE
// https://issuetracker.google.com/issues/179563675
function showMessages(local = Session.getActiveUserLocale()) {
// translation will reference the proper sub-object according to the selected or auto-detected locale
let translation;
switch(local) {
case 'es':
translation = TRANSLATIONS.ES;
break;
case 'en_us':
translation = TRANSLATIONS.EN_US;
break;
/* Other locales here */
default:
translation = TRANSLATIONS.EN_US;
}
// Output messages
console.info(translation.MSG_HELLO, translation.MSG_GOODBYE);
}
function test() {
showMessages('es');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment