123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <!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 Page</span></a></li>
- <li class="current"><a href="pages.html"><span>Related 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">"en_US.UTF-8"</span>));</div>
- <div class="line">output.imbue(std::locale(<span class="stringliteral">"de_DE.UTF-8"</span>))</div>
- <div class="line">output << <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">boost::format</a>(<span class="stringliteral">"%1%"</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 << <a class="code" href="group__format.html#ga610f3ae827801febc962019cf82a2227">wformat</a>(L<span class="stringliteral">"Today {1,date} I would meet {2} at home"</span>) % <a class="code" href="group__manipulators.html#gae669b101cbeaed6f6d246ebdcaa8f39c">time</a>(0) % name <<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<>]+ ;
- 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 << <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a>(<a class="code" href="group__message.html#ga58e9599005608845d2b022d499dc97f6">translate</a>(<span class="stringliteral">"Unexpected `{{' in line {1} in file {2}"</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> – 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><</code> – align-left.</li>
- <li><code>right</code> or <code>></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"><div class="line">cout << <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a>(<span class="stringliteral">"This article was published at {1,date=l} (Gregorian) {1,locale=he_IL@calendar=hebrew,date=l} (Hebrew)"</span>) % <a class="code" href="group__manipulators.html#gae05b82e6658dc573521518fed5f5c77f">date</a>;</div>
- </div><!-- fragment --></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"><div class="line">cout << <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a>(<span class="stringliteral">"Time is: Local {1,time}, ({1,time,tz=EET} Eastern European Time)"</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 << <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a>(<span class="stringliteral">"Local time is: {1,time,local}, universal time is {1,time,gmt}"</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<< <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a>(<a class="code" href="group__message.html#ga58e9599005608845d2b022d499dc97f6">translate</a>(<span class="stringliteral">"Adding {1} to {2}, we get {3}"</span>)) % a % b % (a+b) << 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 &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">"Adding {1} to {2}, we get {3}"</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">"Adding {1} to {2}, we get {3}"</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 << <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">format</a>(<span class="stringliteral">"Adding {1} to {2}, we get {3}"</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">"Adding {1} to {2}, we get {3}"</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 << 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">"Adding {1} to {2}, we get {3}"</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 << fmt;</div>
- </div><!-- fragment --> </div></div><!-- contents -->
- </div><!-- doc-content -->
- <li class="footer">
- © 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>
|