N/translation Module Script Samples

The following script sample demonstrates how to use the features of the N/translation module.

Access Translation Strings

The following sample accesses translation strings one at a time using translation.get(options). This method returns a translator function, which is subsequently called with any specified parameters. The translator function returns the string in the user's session locale by default.

Note:

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) {

        // Create a message with translated strings
        var myMsg = message.create({
            title: translation.get({
                collection: 'custcollection_my_strings',
                key: 'MY_TITLE'
            })(),
            message: translation.get({
                collection: 'custcollection_my_strings',
                key: 'MY_MESSAGE'
            })(),
            type: message.Type.CONFIRMATION
        });

        // Show the message for 5 seconds
        myMsg.show({
            duration: 5000
        });
}); 

          

Access Translation Strings Using a Non-Default Locale

The following sample accesses translation strings using a locale other than the default locale. When you call translation.get(options) and do not specify a locale, the method uses the current user's session locale. You can use the options.locale parameter to specify another locale. The translation.Locale enum lists all locales that are enabled for a company, and you can use these locales in translation.get(options). The translation.Locale enum also includes two special values: CURRENT and COMPANY_DEFAULT. The CURRENT value represents the current user’s locale, and the COMPANY_DEFAULT value represents the default locale for the company.

Note:

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) {

        // Create a message with translated strings
        var myMsg = message.create({
            title: translation.get({
                collection: 'custcollection_my_strings',
                key: 'MY_TITLE',
                locale: translation.Locale.COMPANY_DEFAULT
            })(),
            message: translation.get({
                collection: 'custcollection_my_strings',
                key: 'MY_MESSAGE',
                locale: translation.Locale.COMPANY_DEFAULT
            })(),
            type: message.Type.CONFIRMATION
        });

        // Show the message for 5 seconds
        myMsg.show({
            duration: 5000
        });
}); 

          

Access Parameterized Translation Strings

The following sample accesses parametrized translation strings. When you create a Translation Collection in the NetSuite UI, you can include parameter placeholders in your translation strings. Placeholders use braces and a number (starting from 1). The translator function injects the specified parameter values into the placeholders in the translation string. For example, “Hello, {1}!” is a valid translation string, where {1} is a placeholder for a parameter. In this sample, the parameter “NetSuite” is provided to the translator function returned from translation.get(options), and the translator function returns a translated string of “Hello, NetSuite!”

Note:

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) {

        // Create a message with translated strings
        var myMsg = message.create({
            title: translation.get({
                collection: 'custcollection_my_strings',
                key: 'MY_TITLE'
            })(),
            message: translation.get({
                collection: 'custcollection_my_strings',
                key: 'HELLO_1'
            })({
                params: ['NetSuite']
            }),
            type: message.Type.CONFIRMATION
        });

        // Show the message for 5 seconds
        myMsg.show({
            duration: 5000
        });
}); 

          

Load Specific Translation Strings from a Collection

The following sample loads specific translation strings from a collection. The translation.load(options) method can load a maximum of 1,000 translation strings. If you need only a few of the translation strings in a collection, you can load only the strings you need instead of loading the entire collection.

Note:

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 translation strings by key
        var localizedStrings = translation.load({
            collections: [{
                alias: 'myCollection',
                collection: 'custcollection_my_strings',
                keys: ['MY_TITLE', 'MY_MESSAGE']
            }]
        });

        // Create a message with translated strings
        var myMsg = message.create({
            title: localizedStrings.myCollection.MY_TITLE(),
            message: localizedStrings.myCollection.MY_MESSAGE(),
            type: message.Type.CONFIRMATION
        });

        // Show the message for 5 seconds
        myMsg.show({
            duration: 5000
        });
}); 

          

Load Translation Strings By Key from Multiple Translation Collections

The following sample loads translation strings by key from multiple Translation Collections in a single call of translation.load(options). This method can load a maximum of 1,000 translation strings, regardless of whether the strings are loaded from one collection or multiple collections.

Note:

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 two Translation Collections
        var localizedStrings = translation.load({
            collections: [{
                alias: 'myCollection',
                collection: 'custcollection_my_strings',
                keys: ['MY_TITLE']
            },{
                alias: 'myOtherCollection',
                collection: 'custcollection_other_strings',
                keys: ['MY_OTHER_MESSAGE']
            }]
        });

        // Create a message with translated strings
        var myMsg = message.create({
            title: localizedStrings.myCollection.MY_TITLE(),
            message: localizedStrings.myOtherCollection.MY_OTHER_MESSAGE(),
            type: message.Type.CONFIRMATION
        });

        // Show the message for 5 seconds
        myMsg.show({
            duration: 5000
        });
}); 

          

General Notices