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