From f8a46f7a3e3ed208de74bdef13fde69f17c8f697 Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Fri, 10 Mar 2006 13:15:07 +0000 Subject: [PATCH] Fix locale madness. Add fallback mechanism to Locale, and make rpm insert his text in en locale --- zypp/Locale.cc | 32 +++++++++++++++++++++++ zypp/Locale.h | 3 +++ zypp/TranslatedText.cc | 39 ++++++++++++++++++++++++++++- zypp/source/susetags/SuseTagsPackageImpl.cc | 2 +- zypp/target/rpm/RpmPackageImpl.cc | 5 ++-- 5 files changed, 77 insertions(+), 4 deletions(-) diff --git a/zypp/Locale.cc b/zypp/Locale.cc index e5d2300..e00555e 100644 --- a/zypp/Locale.cc +++ b/zypp/Locale.cc @@ -77,6 +77,31 @@ namespace zypp return ret; } + Locale fallback() const + { + // blah_BLAH + if ( _country.hasCode() ) + { + return Locale(_language.code()); + } + else + { + if (_language.code() == "en" ) + { // no fallback for english + return Locale(); + } + else if ( (_language.code() == "") ) + { // for empty locale, we give up + return Locale(); + } + else + { // for all others, english + return Locale("en"); + } + } + return Locale(); + } + private: LanguageCode _language; @@ -174,6 +199,13 @@ namespace zypp std::string Locale::name() const { return _pimpl->name(); } + /////////////////////////////////////////////////////////////////// + // + // METHOD NAME : Locale:: + // METHOD TYPE : + // + Locale Locale::fallback() const + { return _pimpl->fallback(); } ///////////////////////////////////////////////////////////////// } // namespace zypp /////////////////////////////////////////////////////////////////// diff --git a/zypp/Locale.h b/zypp/Locale.h index b2ac8f4..c59ff65 100644 --- a/zypp/Locale.h +++ b/zypp/Locale.h @@ -70,6 +70,9 @@ namespace zypp /** Return the name made of language and country name. */ std::string name() const; + /** Return a fallback locale for this locale, when giving up, returns empty Locale() */ + Locale fallback() const; + private: /** Pointer to implementation */ RW_pointer _pimpl; diff --git a/zypp/TranslatedText.cc b/zypp/TranslatedText.cc index bf0993e..880d169 100644 --- a/zypp/TranslatedText.cc +++ b/zypp/TranslatedText.cc @@ -12,6 +12,9 @@ #include //#include "zypp/base/Logger.h" +#include "zypp/ZYppFactory.h" +#include "zypp/ZYpp.h" + #include "zypp/base/String.h" #include "zypp/TranslatedText.h" @@ -38,7 +41,41 @@ namespace zypp { setText(text, lang); } std::string text( const Locale &lang = Locale() ) const - { return translations[lang]; } + { + // if there is no translation for this + if ( translations[lang].empty() ) + { + // first, detect the locale + ZYpp::Ptr z = getZYpp(); + Locale detected_lang( z->getTextLocale() ); + if ( translations[detected_lang].empty() ) + { + Locale fallback_locale = detected_lang.fallback(); + while ( fallback_locale != Locale() ) + { + if ( ! translations[fallback_locale].empty() ) + return translations[fallback_locale]; + + fallback_locale = fallback_locale.fallback(); + } + // we gave up, there are no translations with fallbacks + // last try, emtpy locale + Locale empty_locale; + if ( ! translations[empty_locale].empty() ) + return translations[empty_locale]; + else + return std::string(); + } + else + { + return translations[detected_lang]; + } + } + else + { + return translations[lang]; + } + } std::set locales() const { diff --git a/zypp/source/susetags/SuseTagsPackageImpl.cc b/zypp/source/susetags/SuseTagsPackageImpl.cc index 463e0e4..6a2fe07 100644 --- a/zypp/source/susetags/SuseTagsPackageImpl.cc +++ b/zypp/source/susetags/SuseTagsPackageImpl.cc @@ -59,7 +59,7 @@ namespace zypp { return std::string(); } Vendor SuseTagsPackageImpl::vendor() const - { return Vendor(); } + { return _source.vendor(); } Label SuseTagsPackageImpl::license() const { return _license; } diff --git a/zypp/target/rpm/RpmPackageImpl.cc b/zypp/target/rpm/RpmPackageImpl.cc index 72bf2c6..ccca081 100644 --- a/zypp/target/rpm/RpmPackageImpl.cc +++ b/zypp/target/rpm/RpmPackageImpl.cc @@ -38,7 +38,7 @@ namespace zypp RPMPackageImpl::RPMPackageImpl( const RpmHeader::constPtr data ) - : _summary(data->tag_summary()), + : _summary(data->tag_summary(), Locale("en")), _description(), _buildtime(data->tag_buildtime()), _buildhost(data->tag_buildhost()), @@ -61,7 +61,8 @@ namespace zypp _dir_sizes(parsed.dirSizes), #endif { - _description.setText(data->tag_description()); + // we know we are reading english. + _description.setText(data->tag_description(), Locale("en")); data->tag_du(_disk_usage); } -- 2.7.4