localized_text_formatting.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  5. <meta http-equiv="X-UA-Compatible" content="IE=9"/>
  6. <meta name="generator" content="Doxygen 1.8.6"/>
  7. <title>Boost.Locale: Localized Text Formatting</title>
  8. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  9. <script type="text/javascript" src="jquery.js"></script>
  10. <script type="text/javascript" src="dynsections.js"></script>
  11. <link href="navtree.css" rel="stylesheet" type="text/css"/>
  12. <script type="text/javascript" src="resize.js"></script>
  13. <script type="text/javascript" src="navtree.js"></script>
  14. <script type="text/javascript">
  15. $(document).ready(initResizable);
  16. $(window).load(resizeHeight);
  17. </script>
  18. <link href="doxygen.css" rel="stylesheet" type="text/css" />
  19. </head>
  20. <body>
  21. <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
  22. <div id="titlearea">
  23. <table cellspacing="0" cellpadding="0">
  24. <tbody>
  25. <tr style="height: 56px;">
  26. <td id="projectlogo"><img alt="Logo" src="boost-small.png"/></td>
  27. <td style="padding-left: 0.5em;">
  28. <div id="projectname">Boost.Locale
  29. </div>
  30. </td>
  31. </tr>
  32. </tbody>
  33. </table>
  34. </div>
  35. <!-- end header part -->
  36. <!-- Generated by Doxygen 1.8.6 -->
  37. <div id="navrow1" class="tabs">
  38. <ul class="tablist">
  39. <li><a href="index.html"><span>Main&#160;Page</span></a></li>
  40. <li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
  41. <li><a href="modules.html"><span>Modules</span></a></li>
  42. <li><a href="namespaces.html"><span>Namespaces</span></a></li>
  43. <li><a href="annotated.html"><span>Classes</span></a></li>
  44. <li><a href="files.html"><span>Files</span></a></li>
  45. <li><a href="examples.html"><span>Examples</span></a></li>
  46. </ul>
  47. </div>
  48. </div><!-- top -->
  49. <div id="side-nav" class="ui-resizable side-nav-resizable">
  50. <div id="nav-tree">
  51. <div id="nav-tree-contents">
  52. <div id="nav-sync" class="sync"></div>
  53. </div>
  54. </div>
  55. <div id="splitbar" style="-moz-user-select:none;"
  56. class="ui-resizable-handle">
  57. </div>
  58. </div>
  59. <script type="text/javascript">
  60. $(document).ready(function(){initNavTree('localized_text_formatting.html','');});
  61. </script>
  62. <div id="doc-content">
  63. <div class="header">
  64. <div class="headertitle">
  65. <div class="title">Localized Text Formatting </div> </div>
  66. </div><!--header-->
  67. <div class="contents">
  68. <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>
  69. <p>Unfortunately <code>boost::format</code> has several limitations in context of localization:</p>
  70. <ol type="1">
  71. <li>It renders all parameters using global locale rather than target <code>ostream</code> locale. For example: <br/>
  72. <div class="fragment"><div class="line">std::locale::global(std::locale(<span class="stringliteral">&quot;en_US.UTF-8&quot;</span>));</div>
  73. <div class="line">output.imbue(std::locale(<span class="stringliteral">&quot;de_DE.UTF-8&quot;</span>))</div>
  74. <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>
  75. </div><!-- fragment --> <br/>
  76. This would write "1,234.235" to output, instead of the "1.234,234" that is expected for "de_DE" locale</li>
  77. <li>It knows nothing about the new Boost.Locale manipulators.</li>
  78. <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>
  79. </ol>
  80. <p>Thus a new class, <a class="el" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">boost::locale::format</a>, was introduced. For example:</p>
  81. <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>
  82. </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>
  83. <p>Here is an example of a format string:</p>
  84. <pre class="fragment"> "Ms. {1} had arrived at {2,ftime='%I o''clock'} at home. The exact time is {2,time=full}"
  85. </pre><p>The syntax is described by following grammar:</p>
  86. <pre class="fragment"> format : '{' parameters '}'
  87. parameters: parameter | parameter ',' parameters;
  88. parameter : key ["=" value] ;
  89. key : [0-9a-zA-Z&lt;&gt;]+ ;
  90. value : ascii-string-excluding-"}"-and="," | local-string ;
  91. local-string : quoted-text | quoted-text local-string;
  92. quoted-text : '[^']*' ;
  93. </pre><p>You can include literal '{' and '}' by inserting double "{{" or "}}" to the text.</p>
  94. <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>
  95. </div><!-- fragment --><p>Would display something like</p>
  96. <pre class="fragment">Unexpected `{' in line 5 in file source.cpp
  97. </pre><p>The following format key-value pairs are supported:</p>
  98. <ul>
  99. <li><code>[0-9]+</code> &ndash; digits, the index of the formatted parameter &ndash; required.</li>
  100. <li><code>num</code> or <code>number</code> &ndash; format a number. Options are: <br/>
  101. <ul>
  102. <li><code>hex</code> &ndash; display in hexadecimal format</li>
  103. <li><code>oct</code> &ndash; display in octal format</li>
  104. <li><code>sci</code> or <code>scientific</code> &ndash; display in scientific format</li>
  105. <li><code>fix</code> or <code>fixed</code> &ndash; display in fixed format <br/>
  106. For example, <code>number=sci</code> </li>
  107. </ul>
  108. </li>
  109. <li><code>cur</code> or <code>currency</code> &ndash; format currency. Options are: <br/>
  110. <ul>
  111. <li><code>iso</code> &ndash; display using ISO currency symbol.</li>
  112. <li><code>nat</code> or <code>national</code> &ndash; display using national currency symbol. <br/>
  113. </li>
  114. </ul>
  115. </li>
  116. <li><code>per</code> or <code>percent</code> &ndash; format a percentage value.</li>
  117. <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/>
  118. <ul>
  119. <li><code>s</code> or <code>short</code> &ndash; display in short format.</li>
  120. <li><code>m</code> or <code>medium</code> &ndash; display in medium format.</li>
  121. <li><code>l</code> or <code>long</code> &ndash; display in long format.</li>
  122. <li><code>f</code> or <code>full</code> &ndash; display in full format.</li>
  123. </ul>
  124. </li>
  125. <li><code>ftime</code> with string (quoted) parameter &ndash; display as with <code>strftime</code>. See <code>as::ftime</code> manipulator.</li>
  126. <li><code>spell</code> or <code>spellout</code> &ndash; spell the number.</li>
  127. <li><code>ord</code> or <code>ordinal</code> &ndash; format an ordinal number (1st, 2nd... etc)</li>
  128. <li><code>left</code> or <code>&lt;</code> &ndash; align-left.</li>
  129. <li><code>right</code> or <code>&gt;</code> &ndash; align-right.</li>
  130. <li><code>width</code> or <code>w</code> &ndash; set field width (requires parameter).</li>
  131. <li><code>precision</code> or <code>p</code> &ndash; set precision (requires parameter).</li>
  132. <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/>
  133. <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>
  134. </div><!-- fragment --></li>
  135. <li><code>timezone</code> or <code>tz</code> &ndash; the name of the timezone to display the time in. For example:<br/>
  136. <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>
  137. </div><!-- fragment --></li>
  138. <li><code>local</code> - display the time in local time</li>
  139. <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>
  140. </div><!-- fragment --></li>
  141. </ul>
  142. <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>
  143. <p>For example:</p>
  144. <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>
  145. </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>
  146. <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>
  147. <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>
  148. </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>
  149. <p>This is generally not a problem when all operations are done in one statement, such as:</p>
  150. <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>
  151. </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>
  152. <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>
  153. <div class="line">fmt % a;</div>
  154. <div class="line">fmt % b;</div>
  155. <div class="line">fmt % (a+b);</div>
  156. <div class="line">cout &lt;&lt; fmt;</div>
  157. </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>
  158. <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>
  159. <div class="line">fmt % a;</div>
  160. <div class="line">fmt % b;</div>
  161. <div class="line"><span class="keywordtype">int</span> a_plus_b = a+b;</div>
  162. <div class="line">fmt % a_plus_b;</div>
  163. <div class="line">cout &lt;&lt; fmt;</div>
  164. </div><!-- fragment --> </div></div><!-- contents -->
  165. </div><!-- doc-content -->
  166. <li class="footer">
  167. &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.
  168. </li>
  169. </ul>
  170. </div>
  171. </body>
  172. </html>