ex_localization.xml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
  3. "../../../tools/boostbook/dtd/boostbook.dtd">
  4. <!-- Copyright (c) 2001-2005 CrystalClear Software, Inc.
  5. Subject to the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. -->
  8. <section id="date_time.examples.localization">
  9. <title>Localization Demonstration</title>
  10. <para>
  11. The boost::date_time library provides the ability to create customized locale facets. Date ordering, language, seperators, and abbreviations can be customized.
  12. </para>
  13. <!-- <para>
  14. This example uses the new (as of 1.33) date_time IO code. An example using the old code is also provided to demonstrate how much easier customized output is (see <link linkend="date_time.examples.legacy_localization">legacy_localization example</link>).
  15. </para> -->
  16. <programlisting>
  17. <![CDATA[
  18. /* The following shows the creation of a facet for the output of
  19. * dates in German (please forgive me for any errors in my German --
  20. * I'm not a native speaker).
  21. */
  22. #include "boost/date_time/gregorian/gregorian.hpp"
  23. #include <iostream>
  24. #include <algorithm>
  25. /* Define a series of char arrays for short and long name strings
  26. * to be associated with German date output (US names will be
  27. * retrieved from the locale). */
  28. const char* const de_short_month_names[] =
  29. {
  30. "Jan", "Feb", "Mar", "Apr", "Mai", "Jun",
  31. "Jul", "Aug", "Sep", "Okt", "Nov", "Dez", "NAM"
  32. };
  33. const char* const de_long_month_names[] =
  34. {
  35. "Januar", "Februar", "Marz", "April", "Mai",
  36. "Juni", "Juli", "August", "September", "Oktober",
  37. "November", "Dezember", "NichtDerMonat"
  38. };
  39. const char* const de_long_weekday_names[] =
  40. {
  41. "Sonntag", "Montag", "Dienstag", "Mittwoch",
  42. "Donnerstag", "Freitag", "Samstag"
  43. };
  44. const char* const de_short_weekday_names[] =
  45. {
  46. "Son", "Mon", "Die","Mit", "Don", "Fre", "Sam"
  47. };
  48. int main()
  49. {
  50. using namespace boost::gregorian;
  51. // create some gregorian objects to output
  52. date d1(2002, Oct, 1);
  53. greg_month m = d1.month();
  54. greg_weekday wd = d1.day_of_week();
  55. // create a facet and a locale for German dates
  56. date_facet* german_facet = new date_facet();
  57. std::cout.imbue(std::locale(std::locale::classic(), german_facet));
  58. // create the German name collections
  59. date_facet::input_collection_type short_months, long_months,
  60. short_weekdays, long_weekdays;
  61. std::copy(&de_short_month_names[0], &de_short_month_names[11],
  62. std::back_inserter(short_months));
  63. std::copy(&de_long_month_names[0], &de_long_month_names[11],
  64. std::back_inserter(long_months));
  65. std::copy(&de_short_weekday_names[0], &de_short_weekday_names[6],
  66. std::back_inserter(short_weekdays));
  67. std::copy(&de_long_weekday_names[0], &de_long_weekday_names[6],
  68. std::back_inserter(long_weekdays));
  69. // replace the default names with ours
  70. // NOTE: date_generators and special_values were not replaced as
  71. // they are not used in this example
  72. german_facet->short_month_names(short_months);
  73. german_facet->long_month_names(long_months);
  74. german_facet->short_weekday_names(short_weekdays);
  75. german_facet->long_weekday_names(long_weekdays);
  76. // output the date in German using short month names
  77. german_facet->format("%d.%m.%Y");
  78. std::cout << d1 << std::endl; //01.10.2002
  79. german_facet->month_format("%B");
  80. std::cout << m << std::endl; //Oktober
  81. german_facet->weekday_format("%A");
  82. std::cout << wd << std::endl; //Dienstag
  83. // Output the same gregorian objects using US names
  84. date_facet* us_facet = new date_facet();
  85. std::cout.imbue(std::locale(std::locale::classic(), us_facet));
  86. us_facet->format("%m/%d/%Y");
  87. std::cout << d1 << std::endl; // 10/01/2002
  88. // English names, iso order (year-month-day), '-' separator
  89. us_facet->format("%Y-%b-%d");
  90. std::cout << d1 << std::endl; // 2002-Oct-01
  91. return 0;
  92. }
  93. ]]>
  94. </programlisting>
  95. </section>