From e5fb17f0ca5ed15bdf9f55021dc5f17f490ea46f Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Fri, 20 Sep 2019 14:23:15 +0200 Subject: [PATCH] BCountry: add a GetPreferredLanguage method. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The APIs for this were introduced in ICU 63, so we'll need an update. ICU 63 does not build with gcc2, so this method is disabled there. Change-Id: Iabe49509ed6d4e578560d497d3ca336a97db4625 Reviewed-on: https://review.haiku-os.org/c/haiku/+/1874 Tested-by: Commit checker robot Reviewed-by: Jérôme Duval --- docs/user/locale/Country.dox | 13 +++++++++++++ headers/os/locale/Country.h | 1 + src/kits/locale/Country.cpp | 29 +++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/docs/user/locale/Country.dox b/docs/user/locale/Country.dox index a7f47691c6..9c690d1b6f 100644 --- a/docs/user/locale/Country.dox +++ b/docs/user/locale/Country.dox @@ -117,6 +117,19 @@ */ +/*! + \fn status_t BCountry::GetPreferredLanguage(BLanguage& language) const + \brief Get the most likely language to use in that country. + + \param language A reference to a BLanguage to set to the preferred language. + + \returns A status code, B_OK if everything went fine, or an error code + otherwise. + + \since Haiku R1 +*/ + + /*! \fn const char* BCountry::Code() const \brief Gets the ISO country code for the country. diff --git a/headers/os/locale/Country.h b/headers/os/locale/Country.h index ae41f5351f..a0fd5243d0 100644 --- a/headers/os/locale/Country.h +++ b/headers/os/locale/Country.h @@ -39,6 +39,7 @@ public: status_t GetName(BString& name, const BLanguage* displayLanguage = NULL ) const; + status_t GetPreferredLanguage(BLanguage&) const; const char* Code() const; // ISO-3166 diff --git a/src/kits/locale/Country.cpp b/src/kits/locale/Country.cpp index 24427f35c2..a292ed81ce 100644 --- a/src/kits/locale/Country.cpp +++ b/src/kits/locale/Country.cpp @@ -139,6 +139,35 @@ BCountry::GetName(BString& name, const BLanguage* displayLanguage) const } +status_t +BCountry::GetPreferredLanguage(BLanguage& language) const +{ +#if __GNUC__ == 2 + return ENOSYS; +#else + status_t status = InitCheck(); + if (status != B_OK) + return status; + + icu::Locale* languageLocale = fICULocale->clone(); + if (languageLocale == NULL) + return B_NO_MEMORY; + + UErrorCode icuError = U_ZERO_ERROR; + languageLocale->addLikelySubtags(icuError); + + if (U_FAILURE(icuError)) + return B_ERROR; + + status = language.SetTo(languageLocale->getLanguage()); + + delete languageLocale; + + return status; +#endif +} + + const char* BCountry::Code() const {