locale class — Represents a locale as a set of facets
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.
When interacting with the user, either through the standard
I/O streams or through a graphical user interface, you should use
the native locale, that is, locale("")
. When performing
I/O—especially to external files where the data must be portable
to other programs, systems, or environments—always use the
"C
" locale (locale::classic( )
).
Example 13-25 shows locales that control input and output formats.
Example 13-25. Using locales for input and output
// Open a file and read floating-point numbers from it, computing the mean.
// Return the mean or 0 if the file contains no data. The data is in the classic
// format, that is, the same format used by C++.
double mean(const char* filename)
{
std::ifstream in(filename);
// Force the datafile to be interpreted in the classic locale, so the same
// datafile can be used everywhere. in.imbue(std::locale::classic( ));
double sum = 0;
unsigned long count = 0;
std::istream_iterator<double> iter(in), end;
for ( ; iter != end; ++iter) {
++count;
sum += *iter;
}
return count == 0 ? 0.0 : sum / count;
}
int main( )
{
// Print results in the user's native locale.
std::cout.imbue(std::locale(""));
std::cout << mean("data.txt") << '\n';
}
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.
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
false
—runtime_error
is thrown. The new
locale has no name.
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.
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.