A simple and straightforward way to manage message translations in Google Apps Script projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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