1 /******************************************************************** 2 * COPYRIGHT: 3 * Copyright (c) 1999-2002, International Business Machines Corporation and 4 * others. All Rights Reserved. 5 ********************************************************************/ 6 7 #include "unicode/translit.h" 8 #include "unicode/rbt.h" 9 #include "unicode/unistr.h" 10 #include "unicode/calendar.h" 11 #include "unicode/datefmt.h" 12 #include <stdio.h> 13 #include <stdlib.h> 14 #include "util.h" 15 #include "unaccent.h" 16 17 // RuleBasedTransliterator rules to remove accents from characters 18 // so they can be displayed as ASCIIx 19 UnicodeString UNACCENT_RULES( 20 "[\\u00C0-\\u00C5] > A;" 21 "[\\u00C8-\\u00CB] > E;" 22 "[\\u00CC-\\u00CF] > I;" 23 "[\\u00E0-\\u00E5] > a;" 24 "[\\u00E8-\\u00EB] > e;" 25 "[\\u00EC-\\u00EF] > i;" 26 ); 27 28 int main(int argc, char **argv) { 29 30 Calendar *cal; 31 DateFormat *fmt; 32 DateFormat *defFmt; 33 Transliterator *greek_latin; 34 Transliterator *rbtUnaccent; 35 Transliterator *unaccent; 36 UErrorCode status = U_ZERO_ERROR; 37 Locale greece("el", "GR"); 38 UnicodeString str, str2; 39 40 // Create a calendar in the Greek locale 41 cal = Calendar::createInstance(greece, status); 42 check(status, "Calendar::createInstance"); 43 44 // Create a formatter 45 fmt = DateFormat::createDateInstance(DateFormat::kFull, greece); 46 fmt->setCalendar(*cal); 47 48 // Create a default formatter 49 defFmt = DateFormat::createDateInstance(DateFormat::kFull); 50 defFmt->setCalendar(*cal); 51 52 // Create a Greek-Latin Transliterator 53 greek_latin = Transliterator::createInstance("Greek-Latin"); 54 if (greek_latin == 0) { 55 printf("ERROR: Transliterator::createInstance() failed\n"); 56 exit(1); 57 } 58 59 // Create a custom Transliterator 60 rbtUnaccent = new RuleBasedTransliterator("RBTUnaccent", 61 UNACCENT_RULES, 62 UTRANS_FORWARD, 63 status); 64 check(status, "RuleBasedTransliterator::ct"); 65 66 // Create a custom Transliterator 67 unaccent = new UnaccentTransliterator(); 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"); 106 107 unaccent->transliterate(str2); 108 printf("Transliterated via normalizer unaccent: "); 109 uprintf(escape(str2)); 110 printf("\n\n"); 111 } 112 113 // Clean up 114 delete fmt; 115 delete cal; 116 delete greek_latin; 117 delete unaccent; 118 delete rbtUnaccent; 119 120 printf("Exiting successfully\n"); 121 return 0; 122 } 123