blob: 8be0f5b08240de313117061af89485532f4e1845 [file] [log] [blame]
<!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: Using Localization Backends</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('using_localization_backends.html','');
</script>
<div id="doc-content">
<div class="header">
<div class="headertitle">
<div class="title">Using Localization Backends </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>By default, Boost.Locale uses ICU for all localization and text manipulation tasks. This is the most powerful library available, but sometimes we don't need the full power of this library or we want to reduce dependencies from third-party libraries, and ICU is by no means a small library.</p>
<p>Boost.Locale provides an option to use non-ICU based localization backends. Although usually less powerful, these often provide all you need: message formatting, currency, date, time, number formatting, basic collation and case manipulation. They are implemented using the standard OS API or a C or C++ library.</p>
<h2><a class="anchor" id="when_to_use_non_icu_backends"></a>
When to use non-ICU backends</h2>
<p>There are situations when using non-ICU based localization is appropriate:</p>
<ul>
<li>Embedded systems, where the ICU library is very hefty.</li>
<li>Applications where only basic features like message, date, and time formatting and basic collation are required, and using a third-party library like ICU would be too complicated.</li>
<li>Performance. ICU is a very powerful library, but it is generally slower than the standard library. Sometimes it's better to use a simpler but faster localization backend.</li>
</ul>
<h2><a class="anchor" id="non_icu_backends"></a>
Non-ICU Backends</h2>
<p>All of the alternate backends have these limitations:</p>
<ul>
<li>Only the Gregorian calendar is supported and it is based on capabilites of mktime functionality (including dates range)</li>
<li>No boundary analysis.</li>
<li>Case handling is very simple and based on single codepoint conversions, though they still handle UTF-8 better than the standard library.</li>
<li>Time zone specification is very limited: either local time or a time zone in the format "GMT+HH:MM".</li>
<li>No percent formatting, no spellout or ordinal number formatting.</li>
<li>Collation, with exception of the <code>winapi</code> backend, is limited to a single level, similar to what is done by <code>strcoll</code>.</li>
</ul>
<h3><a class="anchor" id="std_backend"></a>
std - The standard C++ library backend</h3>
<p>This localization backend is based on the standard C++ library.</p>
<p>It is supported on all platforms, but is only actually useful on platforms where the standard library supports locales besides "C" and "POSIX": on Linux with GCC or Intel compilers, and under the MSVC compiler.</p>
<p>It works around some common standard library bugs like invalid UTF-8 generation for numeric formatting, and it gives otherwise-absent POSIX locales names and UTF-8 support under MSVC.</p>
<p>It is very useful when the compiler and the library actually give fine localization support, like GCC under Linux or MSVC under Windows.</p>
<h3><a class="anchor" id="posix_backend"></a>
posix - POSIX 2008 C library</h3>
<p>This backend is based on the latest POSIX 2008 standards, and uses POSIX api functions like <code>newlocale</code>, <code>freelocale</code>, <code>strftime_l</code> etc. It is available on the Linux and Mac OS X platforms.</p>
<p>It gives you simple and ready-made localization support, most notably under Mac OS X where GCC's <code>libstdc++</code> does not support locales.</p>
<dl class="note"><dt><b>Note:</b></dt><dd>The POSIX backend only supports UTF-8, single-byte, and double-byte encodings.</dd></dl>
<h3><a class="anchor" id="winapi_backend"></a>
winapi - Win32 API.</h3>
<p>The Win32API-based localization backend provides decent UTF-8/UTF-16 locale support. It is based on Windows API functions like <code>GetLocaleInfoW</code>, <code>LCMapStringW</code>, <code>GetDateFormatW</code> etc and provides good localization support even on the MinGW and Cygwin platforms, which normally have problems with this.</p>
<dl class="note"><dt><b>Note:</b></dt><dd></dd></dl>
<ul>
<li>If you using GCC compiler under Windows you need GCC-4.x series to use it, GCC-3.4 is not supported</li>
<li>Only UTF-8 as narrow locale encoding and UTF-16 as wide encoding are supported.</li>
</ul>
<h2><a class="anchor" id="supported_features_by_backends"></a>
Supported Features</h2>
<table border="1" sellpadding="5" sellspacing="3">
<tr>
<th>Backend </th><th>icu</th><th>posix</th><th>winapi</th><th>std </th></tr>
<tr>
<th>Message Formatting </th><td>Yes</td><td>Yes</td><td>Yes</td><td>Yes </td></tr>
<tr>
<th>Non UTF-8 encodings </th><td>Yes</td><td>Yes</td><td>No</td><td>Yes </td></tr>
<tr>
<th>Date/Time Formatting/Parsing </th><td>Yes</td><td>Formatting Only</td><td>Formatting Only</td><td>Formatting Only </td></tr>
<tr>
<th>Monetary Formatting/Parsing </th><td>Yes</td><td>Formatting Only</td><td>Formatting Only</td><td>Yes </td></tr>
<tr>
<th>Number Formatting/Parsing </th><td>Yes</td><td>Yes</td><td>Yes</td><td>Yes </td></tr>
<tr>
<th>Numbers as Percent, Spelled Out </th><td>Yes</td><td>No</td><td>No</td><td>No </td></tr>
<tr>
<th>Case Manipulation </th><td>Yes</td><td>Basic</td><td>Basic</td><td>Basic </td></tr>
<tr>
<th>Collation </th><td>Full</td><td>Linux - 1 level<br/>
Mac OS X - broken</td><td>3 levels</td><td>1 level </td></tr>
<tr>
<th>Calendar </th><td>Yes</td><td>Gregorian Only</td><td>Gregorian Only</td><td>Gregorian Only </td></tr>
<tr>
<th>Boundary Analysis </th><td>Yes</td><td>No</td><td>No</td><td>No </td></tr>
<tr>
<th>Unicode Normalization </th><td>Yes</td><td>No</td><td>Vista and above</td><td>No </td></tr>
<tr>
<th>C++0x characters </th><td>Yes</td><td>No</td><td>No</td><td>Yes </td></tr>
<tr>
<th>OS Support </th><td>Any</td><td>Linux, Mac OS X</td><td>Windows, Cygwin</td><td>Any </td></tr>
<tr>
<th>Useful on </th><td>Any Platform</td><td>Linux and Mac OS X</td><td>Windows/MinGW/Cygwin</td><td>Linux with GCC or Intel<br/>
Windows with MSVC </td></tr>
</table>
<h2><a class="anchor" id="using_localization_backends"></a>
Using Localization Backends</h2>
<p>Accessing a localization backend is done via the <a class="el" href="classboost_1_1locale_1_1localization__backend__manager.html" title="Localization backend manager is a class that holds various backend and allows creation of their combi...">boost::locale::localization_backend_manager</a> class.</p>
<p>You can create your own <a class="el" href="classboost_1_1locale_1_1localization__backend__manager.html" title="Localization backend manager is a class that holds various backend and allows creation of their combi...">boost::locale::localization_backend_manager</a> by starting with a global backend via the <a class="el" href="classboost_1_1locale_1_1localization__backend__manager.html#a65649bc161a0cc160da9b40a9ad14e20">boost::locale::localization_backend_manager::global</a> static member function and modifying it.</p>
<p>For example:</p>
<div class="fragment"><pre class="fragment"> localization_backend_manager my = localization_backend_manager::global();
<span class="comment">// Get global backend</span>
my.select(<span class="stringliteral">&quot;std&quot;</span>);
<span class="comment">// select std backend as default</span>
generator gen(my);
<span class="comment">// create a generator that uses this backend.</span>
localization_backend_manager::global(my);
<span class="comment">// set this backend globally</span>
generator gen2();
<span class="comment">// now this one would use the new global backend.</span>
</pre></div><p>You can also create a mixture of several backends, using for example <code>icu</code> for one kind of operation and <code>std</code> for all others:</p>
<div class="fragment"><pre class="fragment"> localization_backend_manager my = localization_backend_manager::global();
<span class="comment">// Get global backend</span>
my.select(<span class="stringliteral">&quot;std&quot;</span>);
<span class="comment">// select std backend as default for all categories</span>
my.select(<span class="stringliteral">&quot;icu&quot;</span>,boundary_facet);
<span class="comment">// select icu backend for boundary analysis (since it is not supported by \c std)</span>
</pre></div> </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>