Setting regional formats for a block of Gosu code

The class gw.api.util.LocaleUtil provides the following methods to enable you to run a block of Gosu code with a specific set of regional formats:

gw.api.util.LocaleUtil.runAsCurrentLocale(alternateLocale, \ -> { GosuCode } ) 

 gw.api.util.LocaleUtil.runAsCurrentLocaleAndLanguage(
    alternateLocale, 
    alternateLanguage,
    \ -> { code } )

The second method sets both region and language at the same time.

Running a block of Gosu code in this way enables PolicyCenter to format dates, times, numbers, and names of people appropriately for a specific region. Otherwise, the block of Gosu code uses the regional formats specified by the current region. The current region is either specified by the current user or, if not, by the DefaultApplicationLocale parameter in config.xml.

The parameters that the methods can take are:

Parameter

Description

alternateLocale

An object of type ILocale that represents a regional format from the LocaleType typelist. Specify a GWLocale object for this parameter.

alternateLanguage

An object of type ILocale that represents a language from the LanguageType typelist. Specify a GWLanguage object for this parameter.

\ -> { code }

A Gosu block as a GWRunnable object—the Gosu code to run with different regional formats or a different language

Obtaining an ILocale object for a locale type

To run a block of Gosu code with a specified set of regional formats, you must specify a locale object of type ILocale. You must specify the subtype GWLocale or GWLanguage as appropriate for the parameter alternateLocale or alternateLanguage.

  • For the parameter alternateLocale, use the gw.api.util.LocaleUtil.toLocale method to provide an ILocale object that corresponds to a Gosu typecode in the LocaleType typelist. The object is actually of type GWLocale, which implements ILocale. You can specify the object directly by using typecode syntax. For example LocaleType.TC_EN_US. The typecode has to be defined in the LocaleType typelist.
  • For the parameter alternateLanguage, use the gw.api.util.LocaleUtil.toLanguage method to provide an ILocale object that corresponds to a Gosu typecode in the LanguageType typelist. The object is actually of type GWLanguage, which implements ILocale. You can specify the object directly by using typecode syntax. For example LanguageType.TC_EN_US. The typecode has to be defined in the LanguageType typelist.
  • You can specify a typecode of the correct type directly, without using the toLocale or toLanguage method. Use the following syntax:
    • GWLocalegw.i18n.ILocale.FR_FR
    • GWLanguagegw.i18n.ILocale.LANGUAGE_FR_FR
  • You can specify the first parameter by using a method on LocaleUtil that can get the current or default locale or language, as appropriate. For example, getDefaultLocale or getDefaultLanguage.

You can add a typecode to the LocaleType or LanguageType typelist. If you add a new typecode to one of these typelists, you must restart Guidewire Studio™ to be able to use it in gw.i18n.ILocale.

Example

The following example Gosu code formats today’s date by using the default application regional formats, overriding the any region that the user might have specified. The code uses runAsCurrentLocale to run a block of code that prints today’s date formatted according the default application region’s date format. The first parameter to this method is a call to getDefaultLocale to obtain a GWLocale object that represents the application’s default regional formats.

uses gw.api.util.LocaleUtil
uses gw.api.util.DateUtil
                                                                
  // Run a block of Gosu code that prints the current date 
  // in the application’s default regional format
  LocaleUtil.runAsCurrentLocale(getDefaultLocale(),
        \-> {print(DateUtil.currentDate().format("long"))}
  )

See also