Online Book Reader

Home Category

Professional C__ - Marc Gregoire [253]

By Root 1200 0
with Japanese Industrial Standard encoding is ja_JP.jis.

Locale names on Windows follow a different standard, which has the following general format:

lang[_country_region[.code_page]]

Everything between the square brackets is optional. The following table lists some examples:

LINUX GCC WINDOWS

U.S. English en_US English_United States

Great Britain English en_GB English_Great Britain

Most operating systems have a mechanism to determine the locale as defined by the user. In C++, you can pass an empty string to the locale object constructor to create a locale from the user’s environment. Once this object is created, you can use it to query the locale, possibly making programmatic decisions based on it. The following code demonstrates how to use the user’s locale by calling the imbue() method on a stream. The result is that everything that is send to wcout will be formatted according to the formatting rules for your system:

wcout.imbue(locale(""));

wcout << 32767 << endl;

This means that if your system locale is English United States and you output the number 32767, the number will be displayed as 32,767, but if your system locale is Dutch Belgium, the same number will be displayed as 32.767.

The user’s locale is usually not the default locale. The default locale is generally the classic locale, which uses ANSI C conventions. The classic C locale is similar to U.S. English, but there are slight differences. For example, if you do not set a locale at all, or set the default locale, and you output a number, it will be presented without any punctuation:

wcout.imbue(locale("C"));

wcout << 32767 << endl;

The output of this code will be as follows:

32767

The following code manually sets the U.S. English locale, so the number 32767 will be formatted with U.S. English punctuation, independent of your system locale:

wcout.imbue(locale("en_US"));

wcout << 32767 << endl;

The output of this code will be as follows:

32,767

A locale object allows you to query information about the locale. For example, the following program creates a locale matching the user’s environment. The name() method is used to get a C++ string that describes the locale. Then, the find() method is used on the string object to find a given sub-string, which returns string::npos when the given sub-string was not found. The code checks for the Windows name and the Linux GCC name. One of two messages is output, depending on whether the locale appears to be U.S. English or not:

locale loc("");

if (loc.name().find("en_US") == string::npos &&

loc.name().find("United States") == string::npos) {

wcout << L"Welcome non-U.S. English speaker!" << endl;

} else {

wcout << L"Welcome U.S. English speaker!" << endl;

}

Code snippet from Locales\Locales.cpp

Using Facets

You can use the std::use_facet() function to obtain a particular facet in a particular locale. The argument to use_facet() is a locale. For example, the following expression retrieves the standard monetary punctuation facet of the British English locale using the Linux GCC locale name:

use_facet>(locale("en_GB"));

Note that the innermost template type determines the character type to use. This is usually wchar_t or char. The use of nested template classes is unfortunate, but once you get past the syntax, the result is an object that contains all the information you want to know about British money punctuation. The data available in the standard facets are defined in the header and its associated files.

The following program brings together locales and facets by printing out the currency symbol in both U.S. English and British English. Note that, depending on your environment, the British currency symbol may appear as a question mark, a box, or not at all. If your environment is equipped to handle it, you may actually get the British pound symbol:

locale locUSEng("en_US");

locale locBritEng("en_GB");

wstring dollars = use_facet>(locUSEng).curr_symbol();

wstring pounds = use_facet>(locBritEng).curr_symbol();

wcout

Return Main Page Previous Page Next Page

®Online Book Reader