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 <stdlib.h> 10 #include <stdio.h> 11 #include <string.h> 12 #include <ctype.h> 13 14 #include <iostream> 15 16 #include <Catalog.h> 17 #include <Locale.h> 18 #include <LocaleRoster.h> 19 #include <Path.h> 20 #include <String.h> 21 #include <FindDirectory.h> 22 23 #include <ICUWrapper.h> 24 25 #include <unicode/locid.h> 26 27 28 #define ICU_VERSION icu_44 29 30 31 BLanguage::BLanguage() 32 : 33 fDirection(B_LEFT_TO_RIGHT), 34 fICULocale(NULL) 35 { 36 SetTo(NULL); 37 } 38 39 40 BLanguage::BLanguage(const char* language) 41 : 42 fDirection(B_LEFT_TO_RIGHT), 43 fICULocale(NULL) 44 { 45 SetTo(language); 46 } 47 48 49 BLanguage::BLanguage(const BLanguage& other) 50 : 51 fICULocale(NULL) 52 { 53 *this = other; 54 } 55 56 57 BLanguage::~BLanguage() 58 { 59 delete fICULocale; 60 } 61 62 63 status_t 64 BLanguage::SetTo(const char* language) 65 { 66 delete fICULocale; 67 fICULocale = new ICU_VERSION::Locale(language); 68 if (fICULocale == NULL) 69 return B_NO_MEMORY; 70 71 return B_OK; 72 } 73 74 75 BLanguage& BLanguage::operator=(const BLanguage& source) 76 { 77 if (&source != this) { 78 delete fICULocale; 79 80 fICULocale = source.fICULocale != NULL 81 ? source.fICULocale->clone() 82 : NULL; 83 fDirection = source.fDirection; 84 } 85 86 return *this; 87 } 88 89 90 uint8 91 BLanguage::Direction() const 92 { 93 return fDirection; 94 } 95 96 97 const char* 98 BLanguage::GetString(uint32 id) const 99 { 100 if (id < B_LANGUAGE_STRINGS_BASE 101 || id >= B_LANGUAGE_STRINGS_BASE + B_NUM_LANGUAGE_STRINGS) 102 return NULL; 103 104 return NULL; 105 106 // TODO: fetch string from ICU 107 108 // return fStrings[id - B_LANGUAGE_STRINGS_BASE]; 109 } 110 111 112 status_t 113 BLanguage::GetNativeName(BString& name) const 114 { 115 UnicodeString string; 116 fICULocale->getDisplayName(*fICULocale, string); 117 string.toTitle(NULL, *fICULocale); 118 119 name.Truncate(0); 120 BStringByteSink converter(&name); 121 string.toUTF8(converter); 122 123 return B_OK; 124 } 125 126 127 status_t 128 BLanguage::GetName(BString& name, const BLanguage* displayLanguage) const 129 { 130 BString appLanguage; 131 if (displayLanguage == NULL) { 132 BMessage preferredLanguage; 133 be_locale_roster->GetPreferredLanguages(&preferredLanguage); 134 preferredLanguage.FindString("language", 0, &appLanguage); 135 } else { 136 appLanguage = displayLanguage->Code(); 137 } 138 139 UnicodeString string; 140 fICULocale->getDisplayName(Locale(appLanguage), string); 141 142 name.Truncate(0); 143 BStringByteSink converter(&name); 144 string.toUTF8(converter); 145 146 return B_OK; 147 } 148 149 150 const char* 151 BLanguage::Code() const 152 { 153 return fICULocale->getLanguage(); 154 } 155 156 157 const char* 158 BLanguage::CountryCode() const 159 { 160 const char* country = fICULocale->getCountry(); 161 if (country == NULL || country[0] == '\0') 162 return NULL; 163 164 return country; 165 } 166 167 168 const char* 169 BLanguage::ScriptCode() const 170 { 171 const char* script = fICULocale->getScript(); 172 if (script == NULL || script[0] == '\0') 173 return NULL; 174 175 return script; 176 } 177 178 179 const char* 180 BLanguage::Variant() const 181 { 182 const char* variant = fICULocale->getVariant(); 183 if (variant == NULL || variant[0] == '\0') 184 return NULL; 185 186 return variant; 187 } 188 189 190 const char* 191 BLanguage::ID() const 192 { 193 return fICULocale->getName(); 194 } 195 196 197 bool 198 BLanguage::IsCountrySpecific() const 199 { 200 return CountryCode() != NULL; 201 } 202 203 204 bool 205 BLanguage::IsVariant() const 206 { 207 return Variant() != NULL; 208 } 209