]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/translit/main.cpp
1 /***********************************************************************
2 * © 2016 and later: Unicode, Inc. and others.
3 * License & terms of use: http://www.unicode.org/copyright.html#License
4 ***********************************************************************
5 ***********************************************************************
7 * Copyright (c) 1999-2003, International Business Machines Corporation and
8 * others. All Rights Reserved.
9 ***********************************************************************/
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"
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;"
32 int main(int argc
, char **argv
) {
37 Transliterator
*greek_latin
;
38 Transliterator
*rbtUnaccent
;
39 Transliterator
*unaccent
;
41 UErrorCode status
= U_ZERO_ERROR
;
42 Locale
greece("el", "GR");
43 UnicodeString str
, str2
;
45 // Create a calendar in the Greek locale
46 cal
= Calendar::createInstance(greece
, status
);
47 check(status
, "Calendar::createInstance");
50 fmt
= DateFormat::createDateInstance(DateFormat::kFull
, greece
);
51 fmt
->setCalendar(*cal
);
53 // Create a default formatter
54 defFmt
= DateFormat::createDateInstance(DateFormat::kFull
);
55 defFmt
->setCalendar(*cal
);
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");
64 // Create a custom Transliterator
65 rbtUnaccent
= Transliterator::createFromRules("RBTUnaccent",
70 check(status
, "Transliterator::createFromRules");
72 // Create a custom Transliterator
73 unaccent
= new UnaccentTransliterator();
75 // Loop over various months
76 for (int32_t month
= Calendar::JANUARY
;
77 month
<= Calendar::DECEMBER
;
80 // Set the calendar to a date
82 cal
->set(1999, month
, 4);
84 // Format the date in default locale
86 defFmt
->format(cal
->getTime(status
), str
, status
);
87 check(status
, "DateFormat::format");
92 // Format the date for Greece
94 fmt
->format(cal
->getTime(status
), str
, status
);
95 check(status
, "DateFormat::format");
96 printf("Greek formatted date: ");
100 // Transliterate result
101 greek_latin
->transliterate(str
);
102 printf("Transliterated via Greek-Latin: ");
103 uprintf(escape(str
));
106 // Transliterate result
108 rbtUnaccent
->transliterate(str
);
109 printf("Transliterated via RBT unaccent: ");
110 uprintf(escape(str
));
113 unaccent
->transliterate(str2
);
114 printf("Transliterated via normalizer unaccent: ");
115 uprintf(escape(str2
));
126 printf("Exiting successfully\n");