BPicture: Fix archive constructor.
[haiku.git] / src / kits / locale / Language.cpp
blob047f58258dbcdec9dcb1fec521a0df6f3c4806cc
1 /*
2 * Copyright 2003-2011, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <unicode/uversion.h>
8 #include <Language.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <ctype.h>
15 #include <iostream>
17 #include <Catalog.h>
18 #include <Locale.h>
19 #include <LocaleRoster.h>
20 #include <Path.h>
21 #include <String.h>
22 #include <FindDirectory.h>
24 #include <ICUWrapper.h>
26 #include <unicode/locid.h>
29 BLanguage::BLanguage()
31 fDirection(B_LEFT_TO_RIGHT),
32 fICULocale(NULL)
34 SetTo(NULL);
38 BLanguage::BLanguage(const char* language)
40 fDirection(B_LEFT_TO_RIGHT),
41 fICULocale(NULL)
43 SetTo(language);
47 BLanguage::BLanguage(const BLanguage& other)
49 fICULocale(NULL)
51 *this = other;
55 BLanguage::~BLanguage()
57 delete fICULocale;
61 status_t
62 BLanguage::SetTo(const char* language)
64 delete fICULocale;
65 fICULocale = new icu::Locale(language);
66 if (fICULocale == NULL)
67 return B_NO_MEMORY;
69 return B_OK;
73 status_t
74 BLanguage::GetNativeName(BString& name) const
76 UnicodeString string;
77 fICULocale->getDisplayName(*fICULocale, string);
78 string.toTitle(NULL, *fICULocale);
80 name.Truncate(0);
81 BStringByteSink converter(&name);
82 string.toUTF8(converter);
84 return B_OK;
88 status_t
89 BLanguage::GetName(BString& name, const BLanguage* displayLanguage) const
91 status_t status = B_OK;
93 BString appLanguage;
94 if (displayLanguage == NULL) {
95 BMessage preferredLanguage;
96 status = BLocaleRoster::Default()->GetPreferredLanguages(
97 &preferredLanguage);
98 if (status == B_OK)
99 status = preferredLanguage.FindString("language", 0, &appLanguage);
100 } else {
101 appLanguage = displayLanguage->Code();
104 if (status == B_OK) {
105 UnicodeString string;
106 fICULocale->getDisplayName(Locale(appLanguage), string);
108 name.Truncate(0);
109 BStringByteSink converter(&name);
110 string.toUTF8(converter);
113 return status;
117 status_t
118 BLanguage::GetIcon(BBitmap* result) const
120 return BLocaleRoster::Default()->GetFlagIconForCountry(result, Code());
124 const char*
125 BLanguage::GetString(uint32 id) const
127 if (id < B_LANGUAGE_STRINGS_BASE
128 || id >= B_LANGUAGE_STRINGS_BASE + B_NUM_LANGUAGE_STRINGS)
129 return NULL;
131 return NULL;
133 // TODO: fetch string from ICU
135 // return fStrings[id - B_LANGUAGE_STRINGS_BASE];
139 const char*
140 BLanguage::Code() const
142 return fICULocale->getLanguage();
146 const char*
147 BLanguage::CountryCode() const
149 const char* country = fICULocale->getCountry();
150 if (country == NULL || country[0] == '\0')
151 return NULL;
153 return country;
157 const char*
158 BLanguage::ScriptCode() const
160 const char* script = fICULocale->getScript();
161 if (script == NULL || script[0] == '\0')
162 return NULL;
164 return script;
168 const char*
169 BLanguage::Variant() const
171 const char* variant = fICULocale->getVariant();
172 if (variant == NULL || variant[0] == '\0')
173 return NULL;
175 return variant;
179 const char*
180 BLanguage::ID() const
182 return fICULocale->getName();
186 bool
187 BLanguage::IsCountrySpecific() const
189 return CountryCode() != NULL;
193 bool
194 BLanguage::IsVariant() const
196 return Variant() != NULL;
200 uint8
201 BLanguage::Direction() const
203 return fDirection;
207 BLanguage&
208 BLanguage::operator=(const BLanguage& source)
210 if (&source != this) {
211 delete fICULocale;
213 fICULocale = source.fICULocale != NULL
214 ? source.fICULocale->clone()
215 : NULL;
216 fDirection = source.fDirection;
219 return *this;