1 /* 2 * Copyright 2003-2009, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <Language.h> 8 9 #include <iostream> 10 11 #include <Path.h> 12 #include <String.h> 13 #include <FindDirectory.h> 14 15 #include <stdlib.h> 16 #include <stdio.h> 17 #include <string.h> 18 #include <ctype.h> 19 20 #include <ICUWrapper.h> 21 22 #include <unicode/locid.h> 23 24 25 static const char *gBuiltInStrings[] = { 26 "Yesterday", 27 "Today", 28 "Tomorrow", 29 "Future", 30 31 "Sunday", 32 "Monday", 33 "Tuesday", 34 "Wednesday", 35 "Thursday", 36 "Friday", 37 "Saturday", 38 39 "Sun", 40 "Mon", 41 "Tue", 42 "Wed", 43 "Thu", 44 "Fri", 45 "Sat", 46 47 "January", 48 "February", 49 "March", 50 "April", 51 "May", 52 "June", 53 "July", 54 "August", 55 "September", 56 "October", 57 "November", 58 "December", 59 60 "Jan", 61 "Feb", 62 "Mar", 63 "Apr", 64 "May", 65 "Jun", 66 "Jul", 67 "Aug", 68 "Sep", 69 "Oct", 70 "Nov", 71 "Dec", 72 73 "^[yY]", 74 "^[nN]", 75 "yes", 76 "no" 77 }; 78 79 80 // #pragma mark - 81 82 83 BLanguage::BLanguage(const char *language) 84 : 85 fDirection(B_LEFT_TO_RIGHT) 86 { 87 fICULocale = new icu_4_2::Locale(language); 88 89 for (int32 i = B_NUM_LANGUAGE_STRINGS;i-- > 0;) 90 fStrings[i] = NULL; 91 } 92 93 94 BLanguage::~BLanguage() 95 { 96 delete fICULocale; 97 98 for (int32 i = B_NUM_LANGUAGE_STRINGS;i-- > 0;) 99 free(fStrings[i]); 100 } 101 102 103 void 104 BLanguage::Default() 105 { 106 fICULocale = new icu_4_2::Locale("en"); 107 fDirection = B_LEFT_TO_RIGHT; 108 109 for (int32 i = B_NUM_LANGUAGE_STRINGS;i-- > 0;) { 110 free(fStrings[i]); 111 fStrings[i] = strdup(gBuiltInStrings[i]); 112 } 113 } 114 115 116 uint8 117 BLanguage::Direction() const 118 { 119 return fDirection; 120 } 121 122 123 const char * 124 BLanguage::GetString(uint32 id) const 125 { 126 if (id < B_LANGUAGE_STRINGS_BASE || id > B_LANGUAGE_STRINGS_BASE + B_NUM_LANGUAGE_STRINGS) 127 return NULL; 128 129 return fStrings[id - B_LANGUAGE_STRINGS_BASE]; 130 } 131 132 133 status_t 134 BLanguage::GetName(BString* name) 135 { 136 // TODO: This will return the language not in the current be_app_catalog, 137 // but in the current system wide language! Don't know the exact reason. 138 UnicodeString s; 139 fICULocale->getDisplayLanguage(s); 140 BStringByteSink converter(name); 141 s.toUTF8(converter); 142 return B_OK; 143 } 144 145 const char* 146 BLanguage::Code() 147 { 148 return fICULocale->getLanguage(); 149 } 150 151 152 bool 153 BLanguage::IsCountry() 154 { 155 return *(fICULocale->getCountry()) != '\0'; 156 } 157