Imported Upstream version 58.1
[platform/upstream/icu.git] / source / samples / translit / main.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-2003, 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     Transliterator *unaccent;
40     UParseError pError;
41     UErrorCode status = U_ZERO_ERROR;
42     Locale greece("el", "GR");
43     UnicodeString str, str2;
44
45     // Create a calendar in the Greek locale
46     cal = Calendar::createInstance(greece, status);
47     check(status, "Calendar::createInstance");
48
49     // Create a formatter
50     fmt = DateFormat::createDateInstance(DateFormat::kFull, greece);
51     fmt->setCalendar(*cal);
52
53     // Create a default formatter
54     defFmt = DateFormat::createDateInstance(DateFormat::kFull);
55     defFmt->setCalendar(*cal);
56
57     // Create a Greek-Latin Transliterator
58     greek_latin = Transliterator::createInstance("Greek-Latin", UTRANS_FORWARD, status);
59     if (greek_latin == 0) {
60         printf("ERROR: Transliterator::createInstance() failed\n");
61         exit(1);
62     }
63
64     // Create a custom Transliterator
65     rbtUnaccent = Transliterator::createFromRules("RBTUnaccent",
66                                                   UNACCENT_RULES,
67                                                   UTRANS_FORWARD,
68                                                   pError,
69                                                   status);
70     check(status, "Transliterator::createFromRules");
71
72     // Create a custom Transliterator
73     unaccent = new UnaccentTransliterator();
74
75     // Loop over various months
76     for (int32_t month = Calendar::JANUARY;
77          month <= Calendar::DECEMBER;
78          ++month) {
79
80         // Set the calendar to a date
81         cal->clear();
82         cal->set(1999, month, 4);
83         
84         // Format the date in default locale
85         str.remove();
86         defFmt->format(cal->getTime(status), str, status);
87         check(status, "DateFormat::format");
88         printf("Date: ");
89         uprintf(escape(str));
90         printf("\n");
91         
92         // Format the date for Greece
93         str.remove();
94         fmt->format(cal->getTime(status), str, status);
95         check(status, "DateFormat::format");
96         printf("Greek formatted date: ");
97         uprintf(escape(str));
98         printf("\n");
99         
100         // Transliterate result
101         greek_latin->transliterate(str);
102         printf("Transliterated via Greek-Latin: ");
103         uprintf(escape(str));
104         printf("\n");
105         
106         // Transliterate result
107         str2 = str;
108         rbtUnaccent->transliterate(str);
109         printf("Transliterated via RBT unaccent: ");
110         uprintf(escape(str));
111         printf("\n");
112
113         unaccent->transliterate(str2);
114         printf("Transliterated via normalizer unaccent: ");
115         uprintf(escape(str2));
116         printf("\n\n");
117     }
118
119     // Clean up
120     delete fmt;
121     delete cal;
122     delete greek_latin;
123     delete unaccent;
124     delete rbtUnaccent;
125
126     printf("Exiting successfully\n");
127     return 0;
128 }