2 * Copyright 2003-2011, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
7 #include <unicode/uversion.h>
19 #include <LocaleRoster.h>
22 #include <FindDirectory.h>
24 #include <ICUWrapper.h>
26 #include <unicode/locid.h>
29 BLanguage::BLanguage()
31 fDirection(B_LEFT_TO_RIGHT
),
38 BLanguage::BLanguage(const char* language
)
40 fDirection(B_LEFT_TO_RIGHT
),
47 BLanguage::BLanguage(const BLanguage
& other
)
55 BLanguage::~BLanguage()
62 BLanguage::SetTo(const char* language
)
65 fICULocale
= new icu::Locale(language
);
66 if (fICULocale
== NULL
)
74 BLanguage::GetNativeName(BString
& name
) const
77 fICULocale
->getDisplayName(*fICULocale
, string
);
78 string
.toTitle(NULL
, *fICULocale
);
81 BStringByteSink
converter(&name
);
82 string
.toUTF8(converter
);
89 BLanguage::GetName(BString
& name
, const BLanguage
* displayLanguage
) const
91 status_t status
= B_OK
;
94 if (displayLanguage
== NULL
) {
95 BMessage preferredLanguage
;
96 status
= BLocaleRoster::Default()->GetPreferredLanguages(
99 status
= preferredLanguage
.FindString("language", 0, &appLanguage
);
101 appLanguage
= displayLanguage
->Code();
104 if (status
== B_OK
) {
105 UnicodeString string
;
106 fICULocale
->getDisplayName(Locale(appLanguage
), string
);
109 BStringByteSink
converter(&name
);
110 string
.toUTF8(converter
);
118 BLanguage::GetIcon(BBitmap
* result
) const
120 return BLocaleRoster::Default()->GetFlagIconForCountry(result
, Code());
125 BLanguage::GetString(uint32 id
) const
127 if (id
< B_LANGUAGE_STRINGS_BASE
128 || id
>= B_LANGUAGE_STRINGS_BASE
+ B_NUM_LANGUAGE_STRINGS
)
133 // TODO: fetch string from ICU
135 // return fStrings[id - B_LANGUAGE_STRINGS_BASE];
140 BLanguage::Code() const
142 return fICULocale
->getLanguage();
147 BLanguage::CountryCode() const
149 const char* country
= fICULocale
->getCountry();
150 if (country
== NULL
|| country
[0] == '\0')
158 BLanguage::ScriptCode() const
160 const char* script
= fICULocale
->getScript();
161 if (script
== NULL
|| script
[0] == '\0')
169 BLanguage::Variant() const
171 const char* variant
= fICULocale
->getVariant();
172 if (variant
== NULL
|| variant
[0] == '\0')
180 BLanguage::ID() const
182 return fICULocale
->getName();
187 BLanguage::IsCountrySpecific() const
189 return CountryCode() != NULL
;
194 BLanguage::IsVariant() const
196 return Variant() != NULL
;
201 BLanguage::Direction() const
208 BLanguage::operator=(const BLanguage
& source
)
210 if (&source
!= this) {
213 fICULocale
= source
.fICULocale
!= NULL
214 ? source
.fICULocale
->clone()
216 fDirection
= source
.fDirection
;