Retrieving locale contents

A TLocale instance set to a particular locale gives you access to all the objects within that locale. Objects within a locale are stored and accessed via locale items, or TLocaleItem instances. The TLocaleItem class is a template, so you can use it to manipulate any type of object you want stored in a particular locale.

Each locale item encapsulates the information about a particular object in a locale. Each object within a locale is identified by an item identifier, an instance of TText, that uniquely identifies the purpose of that object regardless of both the data within that object and its locale. For example, the identifier kTypingConfigurationID identifies the preferred typing configuration for a locale, whether it is the U.S. English configuration, the French configuration, or any other configuration. The TLocale class defines a set of constant identifiers for common items.

Predefined locale items

The following table lists the set of predefined identifiers for items commonly found in any locale. These identifiers are static constant TText instances scoped to the TLocale class.

Identifier Type of object Example: U.S.A. locale Example: French locale
kAbbreviatedDateFormatID TDateTimeFormatter* Apr 1, 1999 Jeud 1 avr 99
kBodyTextFontID TFontIdentifierStyle* Times Times
kCalendarID TCalendar* Gregorian calendar Gregorian
kCountryID TLocalizableName* "U.S.A." "France"
kCurrencyFormatID TNumberFormatter* $1,234,567.99 $0.00 ($1,234,567.99) 1 234 567,99 F 0,00 F -1 234 567,99 F
kDefaultTimeFormatID TDateTimeFormatter* 11:12 PM 23:12
kFullDateFormatID TDateTimeFormatter* Thursday, April 1, 1999, 11:12 PM Jeudi 1 avril 1999, 23:12
kMenuTitleFontID TFontIdentifierStyle* Chicago Chicago
kNumberFormatID TNumberFormatter* 1,234,567.9999 1 234 567,9999
kNumericDateFormatID TDateTimeFormatter* 4/1/99 1/4/99
kTextOrderItemID TTextOrder* English collation table French collation table
kTimeZoneID TTimeZone* Greenwich Mean Time Greenwich Mean Time
kTypingConfigurationID TTypingConfigHandle* American keyboard with accent and smart quote transliteration French keyboard

Getting a preferred object

To use an object from a particular locale, create a TLocaleItem to represent that object and then copy the object into that TLocaleItem instance.

To retrieve an object from a particular locale:

  1. Get the locale.
    Use the TLocale::GetCurrentLocale function to get the current locale.
  2. Instantiate the locale item referring to the object.
    Instantiate TLocaleItem with the type of object you want retrieved as the parameter.
  3. Call TLocaleItem::CopyItem to retrieve a copy of the object. Specify the identifier for the item you want and the locale from which to retrieve it.
    CopyItem makes a copy of the object and returns a pointer to it. You are responsible for the storage for the returned object. The examples in this chapter use the TDeleterFor<AType> template class to manage the storage for the returned object.
NOTE TLocaleItem throws an exception if the object cannot be retrieved. Generally TLocaleItem cannot retrieve an item because the archive containing the contents of the specified locale is not accessible. You should enclose the TLocaleItem constructor and the CopyItem call in a try block.

This example demonstrates how to retrieve the default number formatter from the current locale.

      // Get the current locale.
      TLocale currentLocale = TLocale::GetCurrentLocale();
      
      try
      {   
          TLocaleItem<TNumberFormatter> numberFormatterItem;
          TDeleterFor<TNumberFormatter> numberFormatter =
              numberFormatterItem.CopyItem( TLocale::kNumberFormatID, currentLocale );
      }
      
    catch (TArchiveException&)
      {
          // You can either rethrow the exception or directly
          // instantiate an acceptable object, in this case, a
          // concrete subclass of TNumberFormatter.
      }

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker