Returning Locale-Sensitive Custom Strings

When you throw custom validation error messages, if your end users are multi-lingual, you may need to worry about providing a locale-specific error message string.

To accomplish this, you can reference the current locale (inferred from each end user’s browser settings) as part of global function that encapsulates all of your error strings. Consider a getMessage function like the one below. Once it is defined, your validation rule or trigger can throw a locale-sensitive error message by passing in the appropriate message key:

// context is trigger or object-level validation rule
throw new oracle.jbo.ValidationException(adf.util.getMessage('BIG_ERROR'))

The global function is defined as follows.

  • Function Name: getMessage

  • Return Type: String

  • Parameters: stringKey String

Function Definition

// Let "en" be the default lang
// Get the language part of the locale
// e.g. for locale "en_US" lang part is "en"
def defaultLang = 'en';
def userLocale = adf.context.getLocale() as String
def userLang = left(userLocale,2)
def supportedLangs=['en','it']
def lookupLang = supportedLangs.contains(userLang)
                 ? userLang : defaultLang
def messages =
   [BIG_ERROR:  [en:'A big error occurred',
                 it:'È successo un grande errore'],
    SMALL_ERROR:[en:'A small error occurred',
                 it:'È successo un piccolo errore']
]
return messages[stringKey][lookupLang]