UNPKG

boost-react-native-bundle

Version:

Boost library as in https://sourceforge.net/projects/boost/files/boost/1.57.0/

152 lines (134 loc) 9.2 kB
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <meta http-equiv="X-UA-Compatible" content="IE=9"/> <title>Boost.Locale: Introduction to C++ Standard Library localization support</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css" /> <link href="navtree.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="resize.js"></script> <script type="text/javascript" src="navtree.js"></script> <script type="text/javascript"> $(document).ready(initResizable); </script> </head> <body> <div id="top"><!-- do not remove this div! --> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> <td id="projectlogo"><img alt="Logo" src="boost-small.png"/></td> <td style="padding-left: 0.5em;"> <div id="projectname">Boost.Locale </div> </td> </tr> </tbody> </table> </div> <!-- Generated by Doxygen 1.7.6.1 --> <div id="navrow1" class="tabs"> <ul class="tablist"> <li><a href="index.html"><span>Main&#160;Page</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li><a href="files.html"><span>Files</span></a></li> <li><a href="examples.html"><span>Examples</span></a></li> </ul> </div> </div> <div id="side-nav" class="ui-resizable side-nav-resizable"> <div id="nav-tree"> <div id="nav-tree-contents"> </div> </div> <div id="splitbar" style="-moz-user-select:none;" class="ui-resizable-handle"> </div> </div> <script type="text/javascript"> initNavTree('std_locales.html',''); </script> <div id="doc-content"> <div class="header"> <div class="headertitle"> <div class="title">Introduction to C++ Standard Library localization support </div> </div> </div><!--header--> <div class="contents"> <div class="textblock"><h2><a class="anchor" id="std_locales_basics"></a> Getting familiar with standard C++ Locales</h2> <p>The C++ standard library offers a simple and powerful way to provide locale-specific information. It is done via the <code>std::locale</code> class, the container that holds all the required information about a specific culture, such as number formatting patterns, date and time formatting, currency, case conversion etc.</p> <p>All this information is provided by facets, special classes derived from the <code>std::locale::facet</code> base class. Such facets are packed into the <code>std::locale</code> class and allow you to provide arbitrary information about the locale. The <code>std::locale</code> class keeps reference counters on installed facets and can be efficiently copied.</p> <p>Each facet that was installed into the <code>std::locale</code> object can be fetched using the <code>std::use_facet</code> function. For example, the <code>std::ctype&lt;Char&gt;</code> facet provides rules for case conversion, so you can convert a character to upper-case like this:</p> <div class="fragment"><pre class="fragment">std::ctype&lt;char&gt; <span class="keyword">const</span> &amp;ctype_facet = std::use_facet&lt;std::ctype&lt;char&gt; &gt;(some_locale); <span class="keywordtype">char</span> upper_a = ctype_facet.toupper(<span class="charliteral">&#39;a&#39;</span>); </pre></div><p>A locale object can be imbued into an <code>iostream</code> so it would format information according to the locale:</p> <div class="fragment"><pre class="fragment">cout.imbue(std::locale(<span class="stringliteral">&quot;en_US.UTF-8&quot;</span>)); cout &lt;&lt; 1345.45 &lt;&lt; endl; cout.imbue(std::locale(<span class="stringliteral">&quot;ru_RU.UTF-8&quot;</span>)); cout &lt;&lt; 1345.45 &lt;&lt; endl; </pre></div><p>Would display:</p> <div class="fragment"><pre class="fragment"> 1,345.45 1.345,45 </pre></div><p>You can also create your own facets and install them into existing locale objects. For example:</p> <div class="fragment"><pre class="fragment"> <span class="keyword">class </span>measure : <span class="keyword">public</span> std::locale::facet { <span class="keyword">public</span>: <span class="keyword">typedef</span> <span class="keyword">enum</span> { inches, ... } measure_type; measure(measure_type m,<span class="keywordtype">size_t</span> refs=0) double from_metric(<span class="keywordtype">double</span> value) const; std::<span class="keywordtype">string</span> name() const; ... }; </pre></div><p> And now you can simply provide this information to a locale:</p> <div class="fragment"><pre class="fragment"> std::locale::global(std::locale(std::locale(<span class="stringliteral">&quot;en_US.UTF-8&quot;</span>),<span class="keyword">new</span> measure(measure::inches))); <span class="comment">/// Create default locale built from en_US locale and add paper size facet.</span> </pre></div><p>Now you can print a distance according to the correct locale:</p> <div class="fragment"><pre class="fragment"> <span class="keywordtype">void</span> print_distance(std::ostream &amp;out,<span class="keywordtype">double</span> value) { measure <span class="keyword">const</span> &amp;m = std::use_facet&lt;measure&gt;(out.getloc()); <span class="comment">// Fetch locale information from stream</span> out &lt;&lt; m.from_metric(value) &lt;&lt; <span class="stringliteral">&quot; &quot;</span> &lt;&lt; m.name(); } </pre></div><p>This technique was adopted by the Boost.Locale library in order to provide powerful and correct localization. Instead of using the very limited C++ standard library facets, it uses ICU under the hood to create its own much more powerful ones.</p> <h2><a class="anchor" id="std_locales_common"></a> Common Critical Problems with the Standard Library</h2> <p>There are numerous issues in the standard library that prevent the use of its full power, and there are several additional issues:</p> <ul> <li>Setting the global locale has bad side effects. <br/> Consider following code: <br/> <div class="fragment"><pre class="fragment"> <span class="keywordtype">int</span> main() { std::locale::global(std::locale(<span class="stringliteral">&quot;&quot;</span>)); <span class="comment">// Set system&#39;s default locale as global</span> std::ofstream csv(<span class="stringliteral">&quot;test.csv&quot;</span>); csv &lt;&lt; 1.1 &lt;&lt; <span class="stringliteral">&quot;,&quot;</span> &lt;&lt; 1.3 &lt;&lt; std::endl; } </pre></div> <br/> What would be the content of <code>test.csv</code> ? It may be "1.1,1.3" or it may be "1,1,1,3" rather than what you had expected. <br/> More than that it affects even <code>printf</code> and libraries like <code>boost::lexical_cast</code> giving incorrect or unexpected formatting. In fact many third-party libraries are broken in such a situation. <br/> Unlike the standard localization library, Boost.Locale never changes the basic number formatting, even when it uses <code>std</code> based localization backends, so by default, numbers are always formatted using C-style locale. Localized number formatting requires specific flags. <br/> </li> <li>Number formatting is broken on some locales. <br/> Some locales use the non-breakable space u00A0 character for thousands separator, thus in <code>ru_RU.UTF-8</code> locale number 1024 should be displayed as "1 024" where the space is a Unicode character with codepoint u00A0. Unfortunately many libraries don't handle this correctly, for example GCC and SunStudio display a "\xC2" character instead of the first character in the UTF-8 sequence "\xC2\xA0" that represents this code point, and actually generate invalid UTF-8. <br/> </li> <li>Locale names are not standardized. For example, under MSVC you need to provide the name <code>en-US</code> or <code>English_USA.1252</code> , when on POSIX platforms it would be <code>en_US.UTF-8</code> or <code>en_US.ISO-8859-1</code> <br/> More than that, MSVC does not support UTF-8 locales at all. <br/> </li> <li>Many standard libraries provide only the C and POSIX locales, thus GCC supports localization only under Linux. On all other platforms, attempting to create locales other than "C" or "POSIX" would fail. </li> </ul> </div></div><!-- contents --> </div> <div id="nav-path" class="navpath"> <ul> <li class="navelem"><a class="el" href="index.html">Boost.Locale</a> </li> <li class="footer"> &copy; Copyright 2009-2012 Artyom Beilis, Distributed under the <a href="http://www.boost.org/LICENSE_1_0.txt">Boost Software License</a>, Version 1.0. </li> </ul> </div> </body> </html>