blob: 044e5d6b5d81fa156b4ae5467f983b92ca1c8729 [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: Localized Text Formatting</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('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"><pre class="fragment"> std::locale::global(std::locale(<span class="stringliteral">&quot;en_US.UTF-8&quot;</span>));
output.imbue(std::locale(<span class="stringliteral">&quot;de_DE.UTF-8&quot;</span>))
output &lt;&lt; <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">boost::format</a>(<span class="stringliteral">&quot;%1%&quot;</span>) % 1234.345;
</pre></div> <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"><pre class="fragment"> 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;
</pre></div><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>
<div class="fragment"><pre class="fragment">
"Ms. {1} had arrived at {2,ftime='%I o''clock'} at home. The exact time is {2,time=full}"
</pre></div><p>The syntax is described by following grammar:</p>
<div class="fragment"><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></div><p>You can include literal '{' and '}' by inserting double "{{" or "}}" to the text.</p>
<div class="fragment"><pre class="fragment">cout &lt;&lt; <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a>(<a class="code" href="group__message.html#ga58e9599005608845d2b022d499dc97f6" title="Translate a message, msg is not copied.">translate</a>(<span class="stringliteral">&quot;Unexpected `{{&#39; in line {1} in file {2}&quot;</span>)) % pos % file;
</pre></div><p>Would display something like</p>
<div class="fragment"><pre class="fragment">
Unexpected `{' in line 5 in file source.cpp
</pre></div><p>The following format key-value pairs are supported:</p>
<ul>
<li><code>[0-9]+</code> -- digits, the index of the formatted parameter -- required.</li>
<li><code>num</code> or <code>number</code> -- format a number. Options are: <br/>
<ul>
<li><code>hex</code> -- display in hexadecimal format</li>
<li><code>oct</code> -- display in octal format</li>
<li><code>sci</code> or <code>scientific</code> -- display in scientific format</li>
<li><code>fix</code> or <code>fixed</code> -- display in fixed format <br/>
For example, <code>number=sci</code> </li>
</ul>
</li>
<li><code>cur</code> or <code>currency</code> -- format currency. Options are: <br/>
<ul>
<li><code>iso</code> -- display using ISO currency symbol.</li>
<li><code>nat</code> or <code>national</code> -- display using national currency symbol. <br/>
</li>
</ul>
</li>
<li><code>per</code> or <code>percent</code> -- format a percentage value.</li>
<li><code>date</code>, <code>time</code>, <code>datetime</code> or <code>dt</code> -- format a date, a time, or a date and time. Options are: <br/>
<ul>
<li><code>s</code> or <code>short</code> -- display in short format.</li>
<li><code>m</code> or <code>medium</code> -- display in medium format.</li>
<li><code>l</code> or <code>long</code> -- display in long format.</li>
<li><code>f</code> or <code>full</code> -- display in full format.</li>
</ul>
</li>
<li><code>ftime</code> with string (quoted) parameter -- display as with <code>strftime</code>. See <code>as::ftime</code> manipulator.</li>
<li><code>spell</code> or <code>spellout</code> -- spell the number.</li>
<li><code>ord</code> or <code>ordinal</code> -- format an ordinal number (1st, 2nd... etc)</li>
<li><code>left</code> or <code>&lt;</code> -- align-left.</li>
<li><code>right</code> or <code>&gt;</code> -- align-right.</li>
<li><code>width</code> or <code>w</code> -- set field width (requires parameter).</li>
<li><code>precision</code> or <code>p</code> -- set precision (requires parameter).</li>
<li><code>locale</code> -- with parameter -- 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"><pre class="fragment"> 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>;
</pre></div></li>
<li><code>timezone</code> or <code>tz</code> -- the name of the timezone to display the time in. For example:<br/>
<div class="fragment"><pre class="fragment"> 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>;
</pre></div></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"><pre class="fragment"> 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>;
</pre></div></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"><pre class="fragment"> cout&lt;&lt; <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a>(<a class="code" href="group__message.html#ga58e9599005608845d2b022d499dc97f6" title="Translate a message, msg is not copied.">translate</a>(<span class="stringliteral">&quot;Adding {1} to {2}, we get {3}&quot;</span>)) % a % b % (a+b) &lt;&lt; endl;
</pre></div><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"><pre class="fragment"> std::wstring de = (<a class="code" href="group__format.html#ga610f3ae827801febc962019cf82a2227">wformat</a>(<a class="code" href="group__message.html#ga58e9599005608845d2b022d499dc97f6" title="Translate a message, msg is not copied.">translate</a>(<span class="stringliteral">&quot;Adding {1} to {2}, we get {3}&quot;</span>)) % a % b % (a+b)).str(de_locale);
std::wstring fr = (<a class="code" href="group__format.html#ga610f3ae827801febc962019cf82a2227">wformat</a>(<a class="code" href="group__message.html#ga58e9599005608845d2b022d499dc97f6" title="Translate a message, msg is not copied.">translate</a>(<span class="stringliteral">&quot;Adding {1} to {2}, we get {3}&quot;</span>)) % a % b % (a+b)).str(fr_locale);
</pre></div><dl class="note"><dt><b>Note:</b></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 `str()` 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"><pre class="fragment"> 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);
</pre></div><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"><pre class="fragment"> <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a> fmt(<span class="stringliteral">&quot;Adding {1} to {2}, we get {3}&quot;</span>);
fmt % a;
fmt % b;
fmt % (a+b);
cout &lt;&lt; fmt;
</pre></div><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"><pre class="fragment"> <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a> fmt(<span class="stringliteral">&quot;Adding {1} to {2}, we get {3}&quot;</span>);
fmt % a;
fmt % b;
<span class="keywordtype">int</span> a_plus_b = a+b;
fmt % a_plus_b;
cout &lt;&lt; fmt;
</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="navelem"><a class="el" href="using_boost_locale.html">Using 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>