Fix locale madness. Add fallback mechanism to Locale, and make rpm insert his text...
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 10 Mar 2006 13:15:07 +0000 (13:15 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 10 Mar 2006 13:15:07 +0000 (13:15 +0000)
zypp/Locale.cc
zypp/Locale.h
zypp/TranslatedText.cc
zypp/source/susetags/SuseTagsPackageImpl.cc
zypp/target/rpm/RpmPackageImpl.cc

index e5d2300..e00555e 100644 (file)
@@ -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
 ///////////////////////////////////////////////////////////////////
index b2ac8f4..c59ff65 100644 (file)
@@ -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<Impl> _pimpl;
index bf0993e..880d169 100644 (file)
@@ -12,6 +12,9 @@
 #include <iostream>
 //#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<Locale> locales() const
     {
index 463e0e4..6a2fe07 100644 (file)
@@ -59,7 +59,7 @@ namespace zypp
       { return std::string(); }
 
       Vendor SuseTagsPackageImpl::vendor() const
-      { return Vendor(); }
+      { return _source.vendor(); }
 
       Label SuseTagsPackageImpl::license() const
       { return _license; }
index 72bf2c6..ccca081 100644 (file)
@@ -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);
       }