The Locale Data Structure


The Locale data structure is like a little data base the system provides. Once you get a pointer to the Locale structure, it has, among other things, the current language being used, the country code, and the format of dates for the user's cultural environment.

The system creates a Locale structure when an application calls intlOpenLocale(). intlOpenLocale() returns an Item, which must be disposed of using the intlCloseLocale() macro.

To examine the contents of the Locale structure, an application calls intlLookupLocale(), which returns a pointer to the Locale structure for the specified Item. The definition of the Locale structure is shown below.

typedef struct Locale
{
    ItemNode          loc_Item;              /* system linkage */

    /* prefered language to use */
    LanguageCodes     loc_Language;

    /* An array of dialects for the current language, listed in order
     * of preference, and terminated with INTL_DIALECTS_ARRAY_END
     */
    DialectCodes     *loc_Dialects;

    /* ISO-3166 numeric-3 code for the user's country */
    CountryCodes      loc_Country;

    /* general description of the user's environment */
     int32             loc_GMTOffset;     /* minutes from GMT */
    MeasuringSystems  loc_MeasuringSystem; /* measuring system to use */
    CalendarTypes     loc_CalendarType;   /* calendar type to use */
    DrivingTypes      loc_DrivingType;    /* side of the street */

    /* number formatting */
    NumericSpec       loc_Numbers;
    NumericSpec       loc_Currency;
    NumericSpec       loc_SmallCurrency;

    /* date formatting */
    DateSpec          loc_Date;
    DateSpec          loc_ShortDate;
    DateSpec          loc_Time;
    DateSpec          loc_ShortTime;
} Locale;

The fields in the Locale structure are as follows:

NumericSpec Structure

Each culture has its own way of writing numbers and currency. The Locale structure contains three NumericSpec structures that contain number and currency formatting specifications and are used to produce correctly localized output for the target cultures.

The NumericSpec structure defines how numbers should be formatted. It is sufficiently flexible to cover plain numbers and currency values. The structure is shown below.

typedef struct NumericSpec
{
  /* how to generate a positive number */
  GroupingDesc  ns_PosGroups;  /* grouping description */
  unichar      *ns_PosGroupSep;  /* separates the groups */
  unichar      *ns_PosRadix;    /* decimal mark         */
  GroupingDesc  ns_PosFractionalGroups; /* grouping description */
  unichar      *ns_PosFractionalGroupSep; /* separates the groups */
  unichar      *ns_PosFormat;           /* for post-processing  */
  uint32               ns_PosMinFractionalDigits; /* min # of frac digits */
  uint32       ns_PosMaxFractionalDigits; /* max # of frac digits */

  /* how to generate a negative number */
  GroupingDesc  ns_NegGroups;   /* grouping description */
  unichar      *ns_NegGroupSep; /* separates the groups */
  unichar      *ns_NegRadix;   /* decimal mark        */
  GroupingDesc  ns_NegFractionalGroups;   /* grouping description */
  unichar      *ns_NegFractionalGroupSep;  /* separates the groups */
  unichar      *ns_NegFormat;              /* for post-processing  */
  uint32       ns_NegMinFractionalDigits; /* min # of frac digits */
  uint32       ns_NegMaxFractionalDigits; /* max # of frac digits */

  /* when the number is zero, this string is used 'as-is' */
  unichar          *ns_Zero;
} NumericSpec;

Using the fields in the NumericSpec structure, the intlFormatNumber() function can correctly format numbers and currency according to local rules and customs. The fields of the NumericSpec structure are as follows:

For example, if ns_PosFormat is defined as $%s, and the formatted numeric value is 0.02. The result of the post-processing will be $0.02. When this field is NULL, no post-processing occurs. Typically, an application doesn't have to deal with the interpretation of a NumericSpec structure. The Locale structure contains three NumericSpec structures initialized with the correct information for the target culture, which can be passed to intlFormatNumber() to convert numbers into string form.

DateSpec Arrays

Dates also vary in format across different cultures. The Locale structure contains four DateSpec arrays which define the local formats for date and time representation.

The DateSpec arrays contain format commands similar to printf() format strings. The% commands are inserted in the formatting string to produce custom date output. An application can provide the DateSpec structure from the Locale structure to the intlFormatDate() function or it can create its own DateSpec structures for custom formatting.