Imported Upstream version 58.1
[platform/upstream/icu.git] / source / samples / translit / answers / main_3.cpp
1 /***********************************************************************
2  * Copyright (C) 2016 and later: Unicode, Inc. and others.
3  * License & terms of use: http://www.unicode.org/copyright.html#License
4  ***********************************************************************
5  ***********************************************************************
6  * COPYRIGHT:
7  * Copyright (c) 1999-2002, International Business Machines Corporation and
8  * others. All Rights Reserved.
9  ***********************************************************************/
10
11 #include "unicode/translit.h"
12 #include "unicode/rbt.h"
13 #include "unicode/unistr.h"
14 #include "unicode/calendar.h"
15 #include "unicode/datefmt.h"
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include "util.h"
19 #include "unaccent.h"
20
21 // RuleBasedTransliterator rules to remove accents from characters
22 // so they can be displayed as ASCIIx
23 UnicodeString UNACCENT_RULES(
24     "[\\u00C0-\\u00C5] > A;"
25     "[\\u00C8-\\u00CB] > E;"
26     "[\\u00CC-\\u00CF] > I;"
27     "[\\u00E0-\\u00E5] > a;"
28     "[\\u00E8-\\u00EB] > e;"
29     "[\\u00EC-\\u00EF] > i;"
30     );
31
32 int main(int argc, char **argv) {
33
34     Calendar *cal;
35     DateFormat *fmt;
36     DateFormat *defFmt;
37     Transliterator *greek_latin;
38     Transliterator *rbtUnaccent;
39     UErrorCode status = U_ZERO_ERROR;
40     Locale greece("el", "GR");
41     UnicodeString str, str2;
42
43     // Create a calendar in the Greek locale
44     cal = Calendar::createInstance(greece, status);
45     check(status, "Calendar::createInstance");
46
47     // Create a formatter
48     fmt = DateFormat::createDateInstance(DateFormat::kFull, greece);
49     fmt->setCalendar(*cal);
50
51     // Create a default formatter
52     defFmt = DateFormat::createDateInstance(DateFormat::kFull);
53     defFmt->setCalendar(*cal);
54
55     // Create a Greek-Latin Transliterator
56     greek_latin = Transliterator::createInstance("Greek-Latin");
57     if (greek_latin == 0) {
58         printf("ERROR: Transliterator::createInstance() failed\n");
59         exit(1);
60     }
61
62     // Create a custom Transliterator
63     rbtUnaccent = new RuleBasedTransliterator("RBTUnaccent",
64                                               UNACCENT_RULES,
65                                               UTRANS_FORWARD,
66                                               status);
67     check(status, "RuleBasedTransliterator::ct");
68
69     // Loop over various months
70     for (int32_t month = Calendar::JANUARY;
71          month <= Calendar::DECEMBER;
72          ++month) {
73
74         // Set the calendar to a date
75         cal->clear();
76         cal->set(1999, month, 4);
77         
78         // Format the date in default locale
79         str.remove();
80         defFmt->format(cal->getTime(status), str, status);
81         check(status, "DateFormat::format");
82         printf("Date: ");
83         uprintf(escape(str));
84         printf("\n");
85         
86         // Format the date for Greece
87         str.remove();
88         fmt->format(cal->getTime(status), str, status);
89         check(status, "DateFormat::format");
90         printf("Greek formatted date: ");
91         uprintf(escape(str));
92         printf("\n");
93         
94         // Transliterate result
95         greek_latin->transliterate(str);
96         printf("Transliterated via Greek-Latin: ");
97         uprintf(escape(str));
98         printf("\n");
99         
100         // Transliterate result
101         str2 = str;
102         rbtUnaccent->transliterate(str);
103         printf("Transliterated via RBT unaccent: ");
104         uprintf(escape(str));
105         printf("\n\n");
106     }
107
108     // Clean up
109     delete fmt;
110     delete cal;
111     delete greek_latin;
112     delete rbtUnaccent;
113
114     printf("Exiting successfully\n");
115     return 0;
116 }