1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
4 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
5 <meta name="AUTHOR" content="pme@gcc.gnu.org (Phil Edwards)">
6 <meta name="KEYWORDS" content="HOWTO, libstdc++, GCC, g++, libg++, STL">
7 <meta name="DESCRIPTION" content="HOWTO for the libstdc++ chapter 22.">
8 <meta name="GENERATOR" content="vi and eight fingers">
9 <title>libstdc++-v3 HOWTO: Chapter 22</title>
10 <link rel=StyleSheet href="../lib3styles.css">
14 <h1 class="centered"><a name="top">Chapter 22: Localization</a></h1>
16 <p>Chapter 22 deals with the C++ localization facilities.
18 <!-- I wanted to write that sentence in something requiring an exotic font,
19 like Cryllic or Kanji. Probably more work than such cuteness is worth,
20 but I still think it'd be funny.
24 <!-- ####################################################### -->
28 <li><a href="#1">class locale</a>
29 <li><a href="#2">class codecvt</a>
30 <li><a href="#3">class ctype</a>
31 <li><a href="#4">class messages</a>
32 <li><a href="#5">Bjarne Stroustrup on Locales</a>
33 <li><a href="#6">Nathan Myers on Locales</a>
34 <li><a href="#7">Correct Transformations</a>
37 <!-- ####################################################### -->
40 <h2><a name="1">class locale</a></h2>
41 <p>Notes made during the implementation of locales can be found
42 <a href="locale.html">here</a>.
46 <h2><a name="2">class codecvt</a></h2>
47 <p>Notes made during the implementation of codecvt can be found
48 <a href="codecvt.html">here</a>.
51 <p>The following is the abstract from the implementation notes:
53 The standard class codecvt attempts to address conversions between
54 different character encoding schemes. In particular, the standard
55 attempts to detail conversions between the implementation-defined
56 wide characters (hereafter referred to as wchar_t) and the standard
57 type char that is so beloved in classic "C" (which can
58 now be referred to as narrow characters.) This document attempts
59 to describe how the GNU libstdc++-v3 implementation deals with the
60 conversion between wide and narrow characters, and also presents a
61 framework for dealing with the huge number of other encodings that
62 iconv can convert, including Unicode and UTF8. Design issues and
63 requirements are addressed, and examples of correct usage for both
64 the required specializations for wide and narrow characters and the
65 implementation-provided extended functionality are given.
70 <h2><a name="3">class ctype</a></h2>
71 <p>Notes made during the implementation of ctype can be found
72 <a href="ctype.html">here</a>.
76 <h2><a name="4">class messages</a></h2>
77 <p>Notes made during the implementation of messages can be found
78 <a href="messages.html">here</a>.
82 <h2><a name="5">Stroustrup on Locales</a></h2>
83 <p>Dr. Bjarne Stroustrup has released a
84 <a href="http://www.research.att.com/~bs/3rd_loc0.html">pointer</a>
85 to Appendix D of his book,
86 <a href="http://www.research.att.com/~bs/3rd.html">The C++
87 Programming Language (3rd Edition)</a>. It is a detailed
88 description of locales and how to use them.
92 Please note that I still consider this detailed description of
93 locales beyond the needs of most C++ programmers. It is written
94 with experienced programmers in mind and novices will do best to
100 <h2><a name="6">Nathan Myers on Locales</a></h2>
101 <p>An article entitled "The Standard C++ Locale" was
102 published in Dr. Dobb's Journal and can be found
103 <a href="http://www.cantrip.org/locale.html">here</a>.
107 <h2><a name="7">Correct Transformations</a></h2>
108 <!-- Jumping directly to here from chapter 21. -->
109 <p>A very common question on newsgroups and mailing lists is, "How
110 do I do <foo> to a character string?" where <foo> is
111 a task such as changing all the letters to uppercase, to lowercase,
112 testing for digits, etc. A skilled and conscientious programmer
113 will follow the question with another, "And how do I make the
116 <p>(Poor innocent programmer, you have no idea the depths of trouble
117 you are getting yourself into. 'Twould be best for your sanity if
118 you dropped the whole idea and took up basket weaving instead. No?
119 Fine, you asked for it...)
121 <p>The task of changing the case of a letter or classifying a character
122 as numeric, graphical, etc, all depends on the cultural context of the
123 program at runtime. So, first you must take the portability question
124 into account. Once you have localized the program to a particular
125 natural language, only then can you perform the specific task.
126 Unfortunately, specializing a function for a human language is not
127 as simple as declaring
128 <code> extern "Danish" int tolower (int); </code>.
130 <p>The C++ code to do all this proceeds in the same way. First, a locale
131 is created. Then member functions of that locale are called to
132 perform minor tasks. Continuing the example from Chapter 21, we wish
133 to use the following convenience functions:
136 template <class charT>
138 toupper (charT c, const locale& loc) const;
139 template <class charT>
141 tolower (charT c, const locale& loc) const;
143 This function extracts the appropriate "facet" from the
144 locale <em>loc</em> and calls the appropriate member function of that
145 facet, passing <em>c</em> as its argument. The resulting character
148 <p>For the C/POSIX locale, the results are the same as calling the
149 classic C <code>toupper/tolower</code> function that was used in previous
150 examples. For other locales, the code should Do The Right Thing.
152 <p>Of course, these functions take a second argument, and the
153 transformation algorithm's operator argument can only take a single
154 parameter. So we write simple wrapper structs to handle that.
156 <p>The next-to-final version of the code started in Chapter 21 looks like:
158 #include <iterator> // for back_inserter
159 #include <locale>
160 #include <string>
161 #include <algorithm>
162 #include <cctype> // old <ctype.h>
166 Toupper (std::locale const& l) : loc(l) {;}
167 char operator() (char c) { return std::toupper(c,loc); }
169 std::locale const& loc;
174 Tolower (std::locale const& l) : loc(l) {;}
175 char operator() (char c) { return std::tolower(c,loc); }
177 std::locale const& loc;
182 std::string s ("Some Kind Of Initial Input Goes Here");
183 Toupper up ( std::locale("C") );
184 Tolower down ( std::locale("C") );
186 // Change everything into upper case
187 std::transform (s.begin(), s.end(), s.begin(),
191 // Change everything into lower case
192 std::transform (s.begin(), s.end(), s.begin(),
196 // Change everything back into upper case, but store the
197 // result in a different string
198 std::string capital_s;
199 std::transform (s.begin(), s.end(), std::back_inserter(capital_s),
204 <p>The final version of the code uses <code>bind2nd</code> to eliminate
205 the wrapper structs, but the resulting code is tricky. I have not
206 shown it here because no compilers currently available to me will
211 <!-- ####################################################### -->
214 <p class="fineprint"><em>
215 See <a href="../17_intro/license.html">license.html</a> for copying conditions.
216 Comments and suggestions are welcome, and may be sent to
217 <a href="mailto:libstdc++@gcc.gnu.org">the libstdc++ mailing list</a>.