locale/doc/html/using_localization_backends.html
2015-10-18 17:31:48 +03:00

181 lines
11 KiB
HTML

<!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: Using Localization Backends</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('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>
<h1><a class="anchor" id="when_to_use_non_icu_backends"></a>
When to use non-ICU backends</h1>
<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>
<h1><a class="anchor" id="non_icu_backends"></a>
Non-ICU Backends</h1>
<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>
<h2><a class="anchor" id="std_backend"></a>
std - The standard C++ library backend</h2>
<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>
<h2><a class="anchor" id="posix_backend"></a>
posix - POSIX 2008 C library</h2>
<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="section note"><dt>Note</dt><dd>The POSIX backend only supports UTF-8, single-byte, and double-byte encodings.</dd></dl>
<h2><a class="anchor" id="winapi_backend"></a>
winapi - Win32 API.</h2>
<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="section note"><dt>Note</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>
<h1><a class="anchor" id="supported_features_by_backends"></a>
Supported Features</h1>
<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>
<h1><a class="anchor" id="using_localization_backends"></a>
Using Localization Backends</h1>
<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"><div class="line">localization_backend_manager my = localization_backend_manager::global(); </div>
<div class="line"><span class="comment">// Get global backend</span></div>
<div class="line"></div>
<div class="line">my.select(<span class="stringliteral">&quot;std&quot;</span>); </div>
<div class="line"><span class="comment">// select std backend as default</span></div>
<div class="line"></div>
<div class="line">generator gen(my); </div>
<div class="line"><span class="comment">// create a generator that uses this backend.</span></div>
<div class="line"></div>
<div class="line">localization_backend_manager::global(my);</div>
<div class="line"><span class="comment">// set this backend globally</span></div>
<div class="line"></div>
<div class="line">generator gen2();</div>
<div class="line"><span class="comment">// now this one would use the new global backend.</span></div>
</div><!-- fragment --><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"><div class="line">localization_backend_manager my = localization_backend_manager::global(); </div>
<div class="line"><span class="comment">// Get global backend</span></div>
<div class="line"></div>
<div class="line">my.select(<span class="stringliteral">&quot;std&quot;</span>); </div>
<div class="line"><span class="comment">// select std backend as default for all categories</span></div>
<div class="line">my.select(<span class="stringliteral">&quot;icu&quot;</span>,boundary_facet); </div>
<div class="line"><span class="comment">// select icu backend for boundary analysis (since it is not supported by \c std)</span></div>
</div><!-- fragment --> </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>