Apache C++ Standard Library User's Guide

## 24.2 The C++ Locales

In C++, a locale is a class called locale provided by the C++ Standard Library. The C++ class locale differs from the C locale because it is more than a language table, or data representation of the various culture and language dependencies. It also includes the internationalization services, which in C are global functions.

In C++, internationalization semantics are broken out into separate classes called facets. Each facet handles a set of internationalization services; for example, the formatting of monetary values. Facets may also represent a set of culture and language dependencies, such as the rules and symbols for monetary information.

Each locale object maintains a set of facet objects. In fact, you can think of a C++ locale as a container of facets, as illustrated in Figure 5 below:

#### Figure 5: A C++ locale is a container of facets

### 24.2.1 Facets

Facet classes encapsulate data that represents a set of culture and language dependencies, and offer a set of related internationalization services. Facet classes are very flexible. They can contain just about any internationalization service you can invent. The C++ Standard Library offers a number of predefined standard facets, which provide services similar to those contained in the C library. However, you could bundle additional internationalization services into a new facet class, or purchase a facet library.

#### 24.2.1.1 The Standard Facets

The C locale is composed of six categories of locale-dependent information: `LC_NUMERIC` (rules and symbols for numbers), `LC_TIME` (values for date and time information), `LC_MONETARY` (rules and symbols for monetary information), `LC_CTYPE` (character classification and conversion), `LC_COLLATE` (collation sequence), and `LC_MESSAGES` (formats and values of messages).

Similarly, there are six categories of standard facet classes. A detailed description of these facets is contained in the Apache C++ Standard Library Reference Guide, but a brief overview is given below. Note that an abbreviation like num_get<> means that num_get is a class template taking two template arguments, a character type, and an input iterator type. The categories of the standard facets are:

- Numeric. The facet classes num_get<> and num_put<> handle numeric formatting and parsing. The facet classes provide `get()` and `put()` member functions for values of type `long`, `double`, etc.

The facet class numpunct<> specifies numeric punctuation. It provides functions like `decimal_point()`, `thousands_sep()`, etc.

- Time. The facet classes time_get<> and time_put<> handle date and time formatting and parsing. They provide functions like `get_time()`, `get_date()`, `get_weekday()`, etc.

- Monetary. The facet classes money_get<> and money_put<> handle formatting and parsing of monetary values. They provide `get()` and `put()` member functions that parse or produce a sequence of digits, representing a count of the smallest unit of the currency. For example, the sequence $1,056.23 in a common US locale would yield 105623 units, or the character sequence "105623".

The facet class moneypunct<> handles monetary punctuation like the facet numpunct<> handles numeric punctuation. It comes with functions like `curr_symbol()`, etc.

- Ctype. The facet class ctype<> encapsulates the C++ Standard Library ctype features for character classification, like `tolower()`, `toupper()`, `isspace()`, `isprint()`, etc.

The facet class codecvt<> is used when converting from one encoding scheme to another, such as from the multibyte encoding JIS to the wide-character encoding Unicode. The main member functions are `in()` and `out()`. There is a template specialization codecvt<> for multibyte to wide character conversions.

- Collate. The facet class collate<> provides features for string collation, including a `compare()` function used for string comparison.

- Messages. The facet class messages<> implements the X/Open message retrieval facility. It provides facilities to access message catalogues via `open()` and `close(catalog)`, and to retrieve messages via `get(..., int msgid,...)`.

The names of the standard facets obey certain naming rules. The `get` facet classes, like num_get and time_get, handle parsing. The `put` facet classes handle formatting. The `punct` facet classes, like numpunct and moneypunct, represent rules and symbols.