Load Translation Strings by Key from a Translation Collection with Multiple Locales
The following sample loads translation strings by key from a Translation Collection with multiple locales. When you load translation strings using translation.load(options), you can specify a list of valid locales for the strings. You can use these locales when you select a locale using translation.selectLocale(options). If you specify more than one locale when you call translation.load(options)
, the first specified locale in the list is used for the created translation.Handle object. If you want to use a different locale from the list, use translation.selectLocale(options)
, which returns a translation.Handle
object in the specified locale. You must load a locale using translation.load(options)
before you can select it using translation.selectLocale(options)
.
This sample script uses the require
function so that you can copy it into the SuiteScript Debugger and test it. You must use the define
function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.
/**
* @NApiVersion 2.x
*/
require(['N/ui/message', 'N/translation'],
function(message, translation) {
// Load a Translation Collection and a set of locales
var germanStrings = translation.load({
collections: [{
alias: 'myCollection',
collection: 'custcollection_my_strings',
keys: ['MY_TITLE', 'MY_MESSAGE']
}],
locales: [translation.Locale.de_DE, translation.Locale.es_ES]
});
// Select a locale from the list of loaded locales
var spanishStrings = translation.selectLocale({
handle: germanStrings,
locale: translation.Locale.es_ES
});
// Create a message with translated strings
var myMsg = message.create({
title: germanStrings.myCollection.MY_TITLE(),
message: spanishStrings.myCollection.MY_MESSAGE(),
type: message.Type.CONFIRMATION
});
// Show the message for 5 seconds
myMsg.show({
duration: 5000
});
});