<!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"/>
<meta name="generator" content="Doxygen 1.8.6"/>
<title>Boost.Locale: Introduction to C++ Standard Library localization support</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<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>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.6 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&#160;Pages</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><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){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"><h1><a class="anchor" id="std_locales_basics"></a>
Getting familiar with standard C++ Locales</h1>
<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"><div class="line">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);</div>
<div class="line"><span class="keywordtype">char</span> upper_a = ctype_facet.toupper(<span class="charliteral">&#39;a&#39;</span>);</div>
</div><!-- fragment --><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"><div class="line">cout.imbue(std::locale(<span class="stringliteral">&quot;en_US.UTF-8&quot;</span>));</div>
<div class="line">cout &lt;&lt; 1345.45 &lt;&lt; endl;</div>
<div class="line">cout.imbue(std::locale(<span class="stringliteral">&quot;ru_RU.UTF-8&quot;</span>));</div>
<div class="line">cout &lt;&lt; 1345.45 &lt;&lt; endl;</div>
</div><!-- fragment --><p>Would display:</p>
<pre class="fragment">    1,345.45 1.345,45
</pre><p>You can also create your own facets and install them into existing locale objects. For example:</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>measure : <span class="keyword">public</span> std::locale::facet {</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line">    <span class="keyword">typedef</span> <span class="keyword">enum</span> { inches, ... } measure_type;</div>
<div class="line">    measure(measure_type m,<span class="keywordtype">size_t</span> refs=0) </div>
<div class="line">    double from_metric(<span class="keywordtype">double</span> value) const;</div>
<div class="line">    std::<span class="keywordtype">string</span> name() const;</div>
<div class="line">    ...</div>
<div class="line">};</div>
</div><!-- fragment --><p> And now you can simply provide this information to a locale:</p>
<div class="fragment"><div class="line">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)));</div>
<div class="line"><span class="comment">/// Create default locale built from en_US locale and add paper size facet.</span></div>
</div><!-- fragment --><p>Now you can print a distance according to the correct locale:</p>
<div class="fragment"><div class="line"><span class="keywordtype">void</span> print_distance(std::ostream &amp;out,<span class="keywordtype">double</span> value)</div>
<div class="line">{</div>
<div class="line">    measure <span class="keyword">const</span> &amp;m = std::use_facet&lt;measure&gt;(out.getloc());</div>
<div class="line">    <span class="comment">// Fetch locale information from stream</span></div>
<div class="line">    out &lt;&lt; m.from_metric(value) &lt;&lt; <span class="stringliteral">&quot; &quot;</span> &lt;&lt; m.name();</div>
<div class="line">}</div>
</div><!-- fragment --><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>
<h1><a class="anchor" id="std_locales_common"></a>
Common Critical Problems with the Standard Library</h1>
<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"><div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">    std::locale::global(std::locale(<span class="stringliteral">&quot;&quot;</span>)); </div>
<div class="line">    <span class="comment">// Set system&#39;s default locale as global</span></div>
<div class="line">    std::ofstream csv(<span class="stringliteral">&quot;test.csv&quot;</span>);</div>
<div class="line">    csv &lt;&lt; 1.1 &lt;&lt; <span class="stringliteral">&quot;,&quot;</span>  &lt;&lt; 1.3 &lt;&lt; std::endl;</div>
<div class="line">}</div>
</div><!-- fragment --> <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><!-- doc-content -->

    <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>
