dcc738bee859e01a07fea38ae039ef2ca7cc1b85
[platform/upstream/boost.git] / libs / locale / doc / html / localized_text_formatting.html
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 <title>Boost.Locale: Localized Text Formatting</title>
6 <link href="tabs.css" rel="stylesheet" type="text/css"/>
7 <link href="doxygen.css" rel="stylesheet" type="text/css"/>
8 <link rel="stylesheet" type="text/css" href="../style/section-basic.css">
9 </head>
10 <body>
11 <div id="boost-common-heading-doc">
12     <div class="heading-inner">
13         <div class="heading-placard"></div>
14
15         <h1 class="heading-title">
16             <a href="http://www.boost.org/">
17                 <img src="../style/space.png" alt= "Boost C++ Libraries" class="heading-logo" />
18                 <span class="heading-boost">Boost</span>
19                 <span class="heading-cpplibraries">C++ Libraries</span>
20             </a>
21         </h1>
22
23         <p class="heading-quote">
24
25             <q>...one of the most highly
26             regarded and expertly designed C++ library projects in the
27             world.</q> 
28             
29             <span class="heading-attribution">&mdash; <a href=
30             "http://www.gotw.ca/" class="external">Herb Sutter</a> and <a href=
31             "http://en.wikipedia.org/wiki/Andrei_Alexandrescu" class="external">Andrei
32             Alexandrescu</a>, <a href=
33             "http://safari.awprofessional.com/?XmlId=0321113586" class="external">C++
34             Coding Standards</a></span>
35         </p>
36     </div>
37 </div>
38
39 <div id="boost-common-heading-doc-spacer"></div> 
40 <!-- Generated by Doxygen 1.7.1 -->
41 <div class="navigation" id="top">
42   <div class="tabs">
43     <ul class="tablist">
44       <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
45       <li><a href="modules.html"><span>Modules</span></a></li>
46       <li><a href="namespaces.html"><span>Namespaces</span></a></li>
47       <li><a href="annotated.html"><span>Classes</span></a></li>
48       <li><a href="files.html"><span>Files</span></a></li>
49       <li><a href="examples.html"><span>Examples</span></a></li>
50     </ul>
51   </div>
52   <div class="navpath">
53     <ul>
54       <li><a class="el" href="main.html">Boost.Locale</a>      </li>
55       <li><a class="el" href="using_boost_locale.html">Using Boost.Locale</a>      </li>
56     </ul>
57   </div>
58 </div>
59 <div class="header">
60   <div class="headertitle">
61 <h1>Localized Text Formatting </h1>  </div>
62 </div>
63 <div class="contents">
64 <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>
65 <p>Unfortunately <code>boost::format</code> has several limitations in context of localization:</p>
66 <ol type="1">
67 <li>It renders all parameters using global locale rather than target <code>ostream</code> locale. For example: <br/>
68  <div class="fragment"><pre class="fragment">    std::locale::global(std::locale(<span class="stringliteral">&quot;en_US.UTF-8&quot;</span>));
69     output.imbue(std::locale(<span class="stringliteral">&quot;de_DE.UTF-8&quot;</span>))
70     output &lt;&lt; <a class="code" href="group__format.html#gad7914df7b54382c1ad7f5360676fe2e8">boost::format</a>(<span class="stringliteral">&quot;%1%&quot;</span>) % 1234.345;
71 </pre></div> <br/>
72  This would write "1,234.235" to output, instead of the "1.234,234" that is expected for "de_DE" locale</li>
73 <li>It knows nothing about the new Boost.Locale manipulators.</li>
74 <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>
75 </ol>
76 <p>Thus a new class, boost::locale::format, was introduced. For example:</p>
77 <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;
78 </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>
79 <p>Here is an example of a format string:</p>
80 <div class="fragment"><pre class="fragment">
81     "Ms. {1} had arrived at {2,ftime='%I o''clock'} at home. The exact time is {2,time=full}"
82 </pre></div><p>The syntax is described by following grammar:</p>
83 <div class="fragment"><pre class="fragment">
84     format : '{' parameters '}'
85     parameters: parameter | parameter ',' parameters;
86     parameter : key ["=" value] ;
87     key : [0-9a-zA-Z&lt;&gt;]+ ;
88     value : ascii-string-excluding-"}"-and="," | local-string ; 
89     local-string : quoted-text | quoted-text local-string;
90     quoted-text : '[^']*' ;
91 </pre></div><p>You can include literal '{' and '}' by inserting double "{{" or "}}" to the text.</p>
92 <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#ga7f35933e136f9202baa66128d87e5d43" 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;
93 </pre></div><p>Would display something like</p>
94 <div class="fragment"><pre class="fragment">
95 Unexpected `{' in line 5 in file source.cpp
96 </pre></div><p>The following format key-value pairs are supported:</p>
97 <ul>
98 <li><code>[0-9]+</code> -- digits, the index of the formatted parameter -- required.</li>
99 <li><code>num</code> or <code>number</code> -- format a number. Options are: <br/>
100 <ul>
101 <li><code>hex</code> -- display in hexadecimal format</li>
102 <li><code>oct</code> -- display in octal format</li>
103 <li><code>sci</code> or <code>scientific</code> -- display in scientific format</li>
104 <li><code>fix</code> or <code>fixed</code> -- display in fixed format <br/>
105  For example, <code>number=sci</code> </li>
106 </ul>
107 </li>
108 <li><code>cur</code> or <code>currency</code> -- format currency. Options are: <br/>
109 <ul>
110 <li><code>iso</code> -- display using ISO currency symbol.</li>
111 <li><code>nat</code> or <code>national</code> -- display using national currency symbol. <br/>
112 </li>
113 </ul>
114 </li>
115 <li><code>per</code> or <code>percent</code> -- format a percentage value.</li>
116 <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/>
117 <ul>
118 <li><code>s</code> or <code>short</code> -- display in short format.</li>
119 <li><code>m</code> or <code>medium</code> -- display in medium format.</li>
120 <li><code>l</code> or <code>long</code> -- display in long format.</li>
121 <li><code>f</code> or <code>full</code> -- display in full format.</li>
122 </ul>
123 </li>
124 <li><code>ftime</code> with string (quoted) parameter -- display as with <code>strftime</code>. See <code>as::ftime</code> manipulator.</li>
125 <li><code>spell</code> or <code>spellout</code> -- spell the number.</li>
126 <li><code>ord</code> or <code>ordinal</code> -- format an ordinal number (1st, 2nd... etc)</li>
127 <li><code>left</code> or <code>&lt;</code> -- align-left.</li>
128 <li><code>right</code> or <code>&gt;</code> -- align-right.</li>
129 <li><code>width</code> or <code>w</code> -- set field width (requires parameter).</li>
130 <li><code>precision</code> or <code>p</code> -- set precision (requires parameter).</li>
131 <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/>
132  <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>) % date;
133 </pre></div></li>
134 <li><code>timezone</code> or <code>tz</code> -- the name of the timezone to display the time in. For example:<br/>
135  <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>) % date;
136 </pre></div></li>
137 <li><code>local</code> - display the time in local time</li>
138 <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>) % time;
139 </pre></div></li>
140 </ul>
141 <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>
142 <p>For example:</p>
143 <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#ga7f35933e136f9202baa66128d87e5d43" 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;
144 </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>
145 <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#ga7f35933e136f9202baa66128d87e5d43" 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);
146     std::wstring fr = (<a class="code" href="group__format.html#ga610f3ae827801febc962019cf82a2227">wformat</a>(<a class="code" href="group__message.html#ga7f35933e136f9202baa66128d87e5d43" 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);
147 </pre></div><dl class="note"><dt><b>Note:</b></dt><dd>There is one significant difference between <code>boost::format</code> and <code>boost::locale::format</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>
148 <p>This is generally not a problem when all operations are done in one statement, such as:</p>
149 <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);
150 </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>
151 <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>);
152     fmt % a;
153     fmt % b;
154     fmt % (a+b);
155     cout &lt;&lt; fmt;
156 </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>
157 <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>);
158     fmt % a;
159     fmt % b;
160     <span class="keywordtype">int</span> a_plus_b = a+b;
161     fmt % a_plus_b;
162     cout &lt;&lt; fmt;
163 </pre></div> </div>
164 <hr class="footer"/><address class="footer"><small>
165 &copy; Copyright 2009-2011 Artyom Beilis,  Distributed under the <a href="http://www.boost.org/LICENSE_1_0.txt">Boost Software License</a>, Version 1.0.
166 </small></address>
167 </body>
168 </html>