Name

locale class — Represents a locale as a set of facets

Synopsis

class locale{
public:
  class facet;
  class id;
  typedef int category;
  static const category
    none, collate, ctype, monetary, numeric, time, messages,
    all = collate|ctype|monetary|numeric|time|messages;
  // Construct/copy/destroy
  locale(  ) throw(  );
  locale(const locale& other) throw(  );
  explicit locale(const char* std_name);
  locale(const locale& other, const char* std_name,category);
  template <typename Facet>
  locale(const locale& other, Facet* f);
  locale(const locale& other, const locale& one, category);
  ~locale(  ) throw(  );
   
  const locale& operator=(const locale& other) throw(  );
  template <typename Facet>
  locale combine(const locale& other) const;
   
  basic_string<char> name(  ) const;
  bool operator==(const locale& other) const;
  bool operator!=(const locale& other) const;
  template <typename charT, typename Traits, typename Alloc>
  bool operator(  )(const basic_string<charT,Traits,Alloc>& s1,
                    const basic_string<charT,Traits,Alloc>& s2) const;
   
  static locale global(const locale&);
  static const locale& classic(  );
};

The locale class template represents the information for a locale. This information is stored as a set of facets. Several facets are defined by the C++ standard, and user-defined facets can be added to any locale. The has_facet function template tests whether a locale supports a particular facet. The use_facet function template retrieves a facet of a locale.

References to a facet are safe until all locale objects that use that facet have been destroyed. New locales can be created from existing locales, with modifications to a particular facet.

Some locales have names. A locale can be constructed for a standard name, or a named locale can be copied or combined with other named locales to produce a new named locale.

Example 13-25 shows locales that control input and output formats.

Example

The following are the member functions of locale:

locale ( ) throw( )

Initializes the locale with a copy of the current global locale. The initial global locale is locale::classic( ).

locale (const locale& other) throw( )

Copies the other locale.

Using locales for input and output explicit locale (const char* std_name)

Initializes the locale using a standard name. The names "C" and "" (empty string) are always defined, in which "C" is the locale returned by the classic( ) function, and "" identifies the implementation-defined native locale.

An implementation can define additional names. Many C++ implementations use ISO language codes and country codes to identify a locale. For example, the ISO 639 language code for English is "en", and the ISO 3166 country code for the United States is "US", so "en_US" could identify the locale for U.S. English.

locale (const locale& other, const char* std_name, category mask)

Copies the locale from other, except for those categories identified by mask, which are copied from the standard locale identified by std_name. The new locale has a name only if other has a name.

template <typename Facet>, locale (const locale& other, Facet* f)

Copies the locale from other except for the facet Facet, which is obtained from f if f is not null.

locale (const locale& other, const locale& one, category mask)

Copies the locale from other except for those categories identified by mask, which are copied from one. The new locale has a name only if other and one have names.

template <typename Facet>, locale combine (const locale& other) const

Returns a new locale that is a copy of *this, except for Facet, which is copied from other. If other does not support the facet—that is, has_facet<Facet>(other) is falseruntime_error is thrown. The new locale has no name.

Using locales for input and output basic_string<char> name ( ) const

Returns the locale's name or "*" if the locale has no name. The exact contents of the name string are implementation-defined, but you can use the string to construct a new locale that is equal to *this—that is, *this == locale(name( ).c_str( )).

const locale& operator= (const locale& other) throw( )

Copies other and returns *this.

bool operator== (const locale& other) const

Returns true if the two locales are the same object, one locale object is a copy of the other, or the two locales are named and have the same name. Otherwise, the function returns false.

bool operator!= (const locale& other) const

Returns !(*this == other).

template <typename charT, typename Tr, typename A>, bool operator( ) (const basic_string<charT, Tr, A>& s1, const basic_string<charT,Tr,A>& s2) const

Compares two strings using the collate<charT> facet and returns true if s1 < s2. You can use the locale object as a comparator predicate to compare strings. See <string> for more information.

Using locales for input and output static locale global (const locale& loc)

Sets the global locale to loc and returns the previous global locale. If the new locale has a name, the C locale is set by calling setlocale(LC_ALL, loc.name( ).c_str( )); if the locale does not have a name, the effect on the C locale is implementation-defined.

static const locale& classic ( )

Returns a locale that implements the "C" locale.