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

174 lines
13 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: Localized Text Formatting</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('localized_text_formatting.html','');});
</script>
<div id="doc-content">
<div class="header">
<div class="headertitle">
<div class="title">Localized Text Formatting </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>The <code>iostream</code> manipulators are very useful, but when we create a messages for the user, sometimes we need something like good old <code>printf</code> or <code>boost::format</code>.</p>
<p>Unfortunately <code>boost::format</code> has several limitations in context of localization:</p>
<ol type="1">
<li>It renders all parameters using global locale rather than target <code>ostream</code> locale. For example: <br/>
<div class="fragment"><div class="line">std::locale::global(std::locale(<span class="stringliteral">&quot;en_US.UTF-8&quot;</span>));</div>
<div class="line">output.imbue(std::locale(<span class="stringliteral">&quot;de_DE.UTF-8&quot;</span>))</div>
<div class="line">output &lt;&lt; <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">boost::format</a>(<span class="stringliteral">&quot;%1%&quot;</span>) % 1234.345;</div>
</div><!-- fragment --> <br/>
This would write "1,234.235" to output, instead of the "1.234,234" that is expected for "de_DE" locale</li>
<li>It knows nothing about the new Boost.Locale manipulators.</li>
<li>The <code>printf-like</code> syntax is very limited for formatting complex localized data, not allowing the formatting of dates, times, or currencies</li>
</ol>
<p>Thus a new class, <a class="el" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">boost::locale::format</a>, was introduced. For example:</p>
<div class="fragment"><div class="line">wcout &lt;&lt; <a class="code" href="group__format.html#ga610f3ae827801febc962019cf82a2227">wformat</a>(L<span class="stringliteral">&quot;Today {1,date} I would meet {2} at home&quot;</span>) % <a class="code" href="group__manipulators.html#gae669b101cbeaed6f6d246ebdcaa8f39c">time</a>(0) % name &lt;&lt;endl;</div>
</div><!-- fragment --><p>Each format specifier is enclosed within <code>{}</code> brackets, is separated with a comma "," and may have an additional option after an equals symbol '='. This option may be simple ASCII text or single-quoted localized text. If a single-quote should be inserted within the text, it may be represented with a pair of single-quote characters.</p>
<p>Here is an example of a format string:</p>
<pre class="fragment"> "Ms. {1} had arrived at {2,ftime='%I o''clock'} at home. The exact time is {2,time=full}"
</pre><p>The syntax is described by following grammar:</p>
<pre class="fragment"> format : '{' parameters '}'
parameters: parameter | parameter ',' parameters;
parameter : key ["=" value] ;
key : [0-9a-zA-Z&lt;&gt;]+ ;
value : ascii-string-excluding-"}"-and="," | local-string ;
local-string : quoted-text | quoted-text local-string;
quoted-text : '[^']*' ;
</pre><p>You can include literal '{' and '}' by inserting double "{{" or "}}" to the text.</p>
<div class="fragment"><div class="line">cout &lt;&lt; <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a>(<a class="code" href="group__message.html#ga58e9599005608845d2b022d499dc97f6">translate</a>(<span class="stringliteral">&quot;Unexpected `{{&#39; in line {1} in file {2}&quot;</span>)) % pos % file;</div>
</div><!-- fragment --><p>Would display something like</p>
<pre class="fragment">Unexpected `{' in line 5 in file source.cpp
</pre><p>The following format key-value pairs are supported:</p>
<ul>
<li><code>[0-9]+</code> &ndash; digits, the index of the formatted parameter &ndash; required.</li>
<li><code>num</code> or <code>number</code> &ndash; format a number. Options are: <br/>
<ul>
<li><code>hex</code> &ndash; display in hexadecimal format</li>
<li><code>oct</code> &ndash; display in octal format</li>
<li><code>sci</code> or <code>scientific</code> &ndash; display in scientific format</li>
<li><code>fix</code> or <code>fixed</code> &ndash; display in fixed format <br/>
For example, <code>number=sci</code> </li>
</ul>
</li>
<li><code>cur</code> or <code>currency</code> &ndash; format currency. Options are: <br/>
<ul>
<li><code>iso</code> &ndash; display using ISO currency symbol.</li>
<li><code>nat</code> or <code>national</code> &ndash; display using national currency symbol. <br/>
</li>
</ul>
</li>
<li><code>per</code> or <code>percent</code> &ndash; format a percentage value.</li>
<li><code>date</code>, <code>time</code>, <code>datetime</code> or <code>dt</code> &ndash; format a date, a time, or a date and time. Options are: <br/>
<ul>
<li><code>s</code> or <code>short</code> &ndash; display in short format.</li>
<li><code>m</code> or <code>medium</code> &ndash; display in medium format.</li>
<li><code>l</code> or <code>long</code> &ndash; display in long format.</li>
<li><code>f</code> or <code>full</code> &ndash; display in full format.</li>
</ul>
</li>
<li><code>ftime</code> with string (quoted) parameter &ndash; display as with <code>strftime</code>. See <code>as::ftime</code> manipulator.</li>
<li><code>spell</code> or <code>spellout</code> &ndash; spell the number.</li>
<li><code>ord</code> or <code>ordinal</code> &ndash; format an ordinal number (1st, 2nd... etc)</li>
<li><code>left</code> or <code>&lt;</code> &ndash; align-left.</li>
<li><code>right</code> or <code>&gt;</code> &ndash; align-right.</li>
<li><code>width</code> or <code>w</code> &ndash; set field width (requires parameter).</li>
<li><code>precision</code> or <code>p</code> &ndash; set precision (requires parameter).</li>
<li><code>locale</code> &ndash; with parameter &ndash; switch locales for the current operation. This command generates a locale with formatting facets, giving more fine grained control of formatting. For example: <br/>
<div class="fragment"><div class="line">cout &lt;&lt; <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a>(<span class="stringliteral">&quot;This article was published at {1,date=l} (Gregorian) {1,locale=he_IL@calendar=hebrew,date=l} (Hebrew)&quot;</span>) % <a class="code" href="group__manipulators.html#gae05b82e6658dc573521518fed5f5c77f">date</a>;</div>
</div><!-- fragment --></li>
<li><code>timezone</code> or <code>tz</code> &ndash; the name of the timezone to display the time in. For example:<br/>
<div class="fragment"><div class="line">cout &lt;&lt; <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a>(<span class="stringliteral">&quot;Time is: Local {1,time}, ({1,time,tz=EET} Eastern European Time)&quot;</span>) % <a class="code" href="group__manipulators.html#gae05b82e6658dc573521518fed5f5c77f">date</a>;</div>
</div><!-- fragment --></li>
<li><code>local</code> - display the time in local time</li>
<li><code>gmt</code> - display the time in UTC time scale <div class="fragment"><div class="line">cout &lt;&lt; <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a>(<span class="stringliteral">&quot;Local time is: {1,time,local}, universal time is {1,time,gmt}&quot;</span>) % <a class="code" href="group__manipulators.html#gae669b101cbeaed6f6d246ebdcaa8f39c">time</a>;</div>
</div><!-- fragment --></li>
</ul>
<p>The constructor for the <a class="el" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a> class can take an object of type <a class="el" href="group__message.html#ga556e3e7696302902b2242a7a94516dee">message</a>, simplifying integration with message translation code.</p>
<p>For example:</p>
<div class="fragment"><div class="line">cout&lt;&lt; <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a>(<a class="code" href="group__message.html#ga58e9599005608845d2b022d499dc97f6">translate</a>(<span class="stringliteral">&quot;Adding {1} to {2}, we get {3}&quot;</span>)) % a % b % (a+b) &lt;&lt; endl;</div>
</div><!-- fragment --><p>A formatted string can be fetched directly by using the <a class="el" href="classboost_1_1locale_1_1basic__format.html#a6bc65d7993e3ab6ad51809ef8fb65400">str(std::locale const &amp;loc=std::locale())</a> member function. For example:</p>
<div class="fragment"><div class="line">std::wstring de = (<a class="code" href="group__format.html#ga610f3ae827801febc962019cf82a2227">wformat</a>(<a class="code" href="group__message.html#ga58e9599005608845d2b022d499dc97f6">translate</a>(<span class="stringliteral">&quot;Adding {1} to {2}, we get {3}&quot;</span>)) % a % b % (a+b)).str(de_locale);</div>
<div class="line">std::wstring fr = (<a class="code" href="group__format.html#ga610f3ae827801febc962019cf82a2227">wformat</a>(<a class="code" href="group__message.html#ga58e9599005608845d2b022d499dc97f6">translate</a>(<span class="stringliteral">&quot;Adding {1} to {2}, we get {3}&quot;</span>)) % a % b % (a+b)).str(fr_locale);</div>
</div><!-- fragment --><dl class="section note"><dt>Note</dt><dd>There is one significant difference between <code>boost::format</code> and <code><a class="el" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">boost::locale::format</a></code>: Boost.Locale's format converts its parameters only when written to an <code>ostream</code> or when the <code>str()</code> member function is called. It only saves references to the objects that can be written to a stream.</dd></dl>
<p>This is generally not a problem when all operations are done in one statement, such as:</p>
<div class="fragment"><div class="line">cout &lt;&lt; <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a>(<span class="stringliteral">&quot;Adding {1} to {2}, we get {3}&quot;</span>) % a % b % (a+b);</div>
</div><!-- fragment --><p>Because the temporary value of <code></code>(a+b) exists until the formatted data is actually written to the stream. But following code is wrong:</p>
<div class="fragment"><div class="line"><a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a> fmt(<span class="stringliteral">&quot;Adding {1} to {2}, we get {3}&quot;</span>);</div>
<div class="line">fmt % a;</div>
<div class="line">fmt % b;</div>
<div class="line">fmt % (a+b);</div>
<div class="line">cout &lt;&lt; fmt;</div>
</div><!-- fragment --><p>Because the temporary value of <code></code>(a+b) no longer exists when <code>fmt</code> is written to the stream. A correct solution would be:</p>
<div class="fragment"><div class="line"><a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a> fmt(<span class="stringliteral">&quot;Adding {1} to {2}, we get {3}&quot;</span>);</div>
<div class="line">fmt % a;</div>
<div class="line">fmt % b;</div>
<div class="line"><span class="keywordtype">int</span> a_plus_b = a+b;</div>
<div class="line">fmt % a_plus_b;</div>
<div class="line">cout &lt;&lt; fmt;</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>