localized_text_formatting.txt 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 filetype=cpp.doxygen
  9. /*!
  10. \page localized_text_formatting Localized Text Formatting
  11. The \c iostream manipulators are very useful, but when we create a messages for the user, sometimes we need something
  12. like good old \c printf or \c boost::format.
  13. Unfortunately \c boost::format has several limitations in context of localization:
  14. -# It renders all parameters using global locale rather than target \c ostream locale. For example:
  15. \n
  16. \code
  17. std::locale::global(std::locale("en_US.UTF-8"));
  18. output.imbue(std::locale("de_DE.UTF-8"))
  19. output << boost::format("%1%") % 1234.345;
  20. \endcode
  21. \n
  22. This would write "1,234.235" to output, instead of the "1.234,234" that is expected for "de_DE" locale
  23. -# It knows nothing about the new Boost.Locale manipulators.
  24. -# The \c printf-like syntax is very limited for formatting complex localized data, not allowing
  25. the formatting of dates, times, or currencies
  26. Thus a new class, boost::locale::format, was introduced. For example:
  27. \code
  28. wcout << wformat(L"Today {1,date} I would meet {2} at home") % time(0) % name <<endl;
  29. \endcode
  30. Each format specifier is enclosed within \c {} brackets, is separated with a comma "," and
  31. may have an additional option after an equals symbol '='. This option may be simple ASCII text or single-quoted localized text.
  32. If a single-quote should be inserted within the text, it may be represented with a pair of single-quote characters.
  33. Here is an example of a format string:
  34. \verbatim
  35. "Ms. {1} had arrived at {2,ftime='%I o''clock'} at home. The exact time is {2,time=full}"
  36. \endverbatim
  37. The syntax is described by following grammar:
  38. \verbatim
  39. format : '{' parameters '}'
  40. parameters: parameter | parameter ',' parameters;
  41. parameter : key ["=" value] ;
  42. key : [0-9a-zA-Z<>]+ ;
  43. value : ascii-string-excluding-"}"-and="," | local-string ;
  44. local-string : quoted-text | quoted-text local-string;
  45. quoted-text : '[^']*' ;
  46. \endverbatim
  47. You can include literal '{' and '}' by inserting double "{{" or "}}"
  48. to the text.
  49. \code
  50. cout << format(translate("Unexpected `{{' in line {1} in file {2}")) % pos % file;
  51. \endcode
  52. Would display something like
  53. \verbatim
  54. Unexpected `{' in line 5 in file source.cpp
  55. \endverbatim
  56. The following format key-value pairs are supported:
  57. - <tt>[0-9]+</tt> -- digits, the index of the formatted parameter -- required.
  58. - \c num or \c number -- format a number. Options are:
  59. \n
  60. - \c hex -- display in hexadecimal format
  61. - \c oct -- display in octal format
  62. - \c sci or \c scientific -- display in scientific format
  63. - \c fix or \c fixed -- display in fixed format
  64. \n
  65. For example, \c number=sci
  66. - \c cur or \c currency -- format currency. Options are:
  67. \n
  68. - \c iso -- display using ISO currency symbol.
  69. - \c nat or \c national -- display using national currency symbol.
  70. \n
  71. - \c per or \c percent -- format a percentage value.
  72. - \c date, \c time, \c datetime or \c dt -- format a date, a time, or a date and time. Options are:
  73. \n
  74. - \c s or \c short -- display in short format.
  75. - \c m or \c medium -- display in medium format.
  76. - \c l or \c long -- display in long format.
  77. - \c f or \c full -- display in full format.
  78. - \c ftime with string (quoted) parameter -- display as with \c strftime. See \c as::ftime manipulator.
  79. - \c spell or \c spellout -- spell the number.
  80. - \c ord or \c ordinal -- format an ordinal number (1st, 2nd... etc)
  81. - \c left or \c < -- align-left.
  82. - \c right or \c > -- align-right.
  83. - \c width or \c w -- set field width (requires parameter).
  84. - \c precision or \c p -- set precision (requires parameter).
  85. - \c locale -- with parameter -- switch locales for the current operation. This command generates a locale
  86. with formatting facets, giving more fine grained control of formatting. For example:
  87. \n
  88. \code
  89. cout << format("This article was published at {1,date=l} (Gregorian) {1,locale=he_IL@calendar=hebrew,date=l} (Hebrew)") % date;
  90. \endcode
  91. - \c timezone or \c tz -- the name of the timezone to display the time in. For example:\n
  92. \code
  93. cout << format("Time is: Local {1,time}, ({1,time,tz=EET} Eastern European Time)") % date;
  94. \endcode
  95. - \c local - display the time in local time
  96. - \c gmt - display the time in UTC time scale
  97. \code
  98. cout << format("Local time is: {1,time,local}, universal time is {1,time,gmt}") % time;
  99. \endcode
  100. The constructor for the \ref boost::locale::format "format" class can take an object of type \ref boost::locale::message "message", simplifying integration with message translation code.
  101. For example:
  102. \code
  103. cout<< format(translate("Adding {1} to {2}, we get {3}")) % a % b % (a+b) << endl;
  104. \endcode
  105. A formatted string can be fetched directly by using the \ref boost::locale::format::str() "str(std::locale const &loc=std::locale())" member function. For example:
  106. \code
  107. std::wstring de = (wformat(translate("Adding {1} to {2}, we get {3}")) % a % b % (a+b)).str(de_locale);
  108. std::wstring fr = (wformat(translate("Adding {1} to {2}, we get {3}")) % a % b % (a+b)).str(fr_locale);
  109. \endcode
  110. \note There is one significant difference between \c boost::format and \c boost::locale::format: Boost.Locale's format converts its
  111. parameters only when written to an \c ostream or when the `str()` member function is called. It only saves references to the objects that
  112. can be written to a stream.
  113. This is generally not a problem when all operations are done in one statement, such as:
  114. \code
  115. cout << format("Adding {1} to {2}, we get {3}") % a % b % (a+b);
  116. \endcode
  117. Because the temporary value of \c (a+b) exists until the formatted data is actually written to the stream. But following code is wrong:
  118. \code
  119. format fmt("Adding {1} to {2}, we get {3}");
  120. fmt % a;
  121. fmt % b;
  122. fmt % (a+b);
  123. cout << fmt;
  124. \endcode
  125. Because the temporary value of \c (a+b) no longer exists when \c fmt is written to the stream. A correct solution would be:
  126. \code
  127. format fmt("Adding {1} to {2}, we get {3}");
  128. fmt % a;
  129. fmt % b;
  130. int a_plus_b = a+b;
  131. fmt % a_plus_b;
  132. cout << fmt;
  133. \endcode
  134. */