added a hint to enable all requested repos
[platform/upstream/libzypp.git] / zypp / TranslatedText.cc
index 880d169..a8b44bb 100644 (file)
@@ -6,17 +6,15 @@
 |                         /_____||_| |_| |_|                           |
 |                                                                      |
 \---------------------------------------------------------------------*/
-/** \file      zypp/TranslatedText.cc
+/** \file        zypp/TranslatedText.cc
  *
 */
 #include <iostream>
-//#include "zypp/base/Logger.h"
-
-#include "zypp/ZYppFactory.h"
-#include "zypp/ZYpp.h"
 
 #include "zypp/base/String.h"
+
 #include "zypp/TranslatedText.h"
+#include "zypp/ZConfig.h"
 
 using std::endl;
 
@@ -26,11 +24,13 @@ namespace zypp
 
   ///////////////////////////////////////////////////////////////////
   //
-  //   CLASS NAME : TranslatedText::Impl
+  //        CLASS NAME : TranslatedText::Impl
   //
   /** TranslatedText implementation. */
   struct TranslatedText::Impl
   {
+    typedef std::map<Locale, std::string> TranslationMap;
+
     Impl()
     {}
 
@@ -40,47 +40,49 @@ namespace zypp
     Impl(const std::list<std::string> &text, const Locale &lang)
     { setText(text, lang); }
 
-    std::string text( const Locale &lang = Locale() ) const
+    bool empty() const
+    {
+      return translations.empty();
+    }
+
+    std::string text( const Locale &lang ) const
     {
-      // if there is no translation for this
-      if ( translations[lang].empty() )
+      // Traverse fallback list and return the 1st nonempty match.
+      // Take care NOT to create new map entries in queries.
+      Locale toReturn( lang );
+      if ( lang == Locale::noCode )
       {
-          // 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];
-          }
+        toReturn = ZConfig::instance().textLocale();
       }
-      else
+
+      do
       {
-        return translations[lang]; 
-      }
+        TranslationMap::const_iterator it = translations.find( toReturn );
+        if ( it != translations.end()
+             && ! it->second.empty() )
+        {
+          return it->second;
+        }
+
+       if ( toReturn != Locale::noCode )
+       {
+         // retry using next fallback:
+         toReturn = toReturn.fallback();
+       }
+       else
+       {
+         // there are no further fallbacks
+         return std::string();
+       }
+      } while( true );
+      // not reached.
+      return std::string();
     }
 
     std::set<Locale> locales() const
     {
       std::set<Locale> lcls;
-      for(std::map<Locale, std::string>::const_iterator it = translations.begin(); it != translations.end(); ++it)
+      for( TranslationMap::const_iterator it = translations.begin(); it != translations.end(); ++it )
       {
         lcls.insert((*it).first);
       }
@@ -100,7 +102,7 @@ namespace zypp
     }
 
   private:
-    mutable std::map<Locale, std::string> translations;
+    mutable TranslationMap translations;
 
   public:
     /** Offer default Impl. */
@@ -120,7 +122,7 @@ namespace zypp
 
   ///////////////////////////////////////////////////////////////////
   //
-  //   CLASS NAME : TranslatedText
+  //        CLASS NAME : TranslatedText
   //
   ///////////////////////////////////////////////////////////////////
 
@@ -128,8 +130,8 @@ namespace zypp
 
   ///////////////////////////////////////////////////////////////////
   //
-  //   METHOD NAME : TranslatedText::TranslatedText
-  //   METHOD TYPE : Ctor
+  //        METHOD NAME : TranslatedText::TranslatedText
+  //        METHOD TYPE : Ctor
   //
   TranslatedText::TranslatedText()
   : _pimpl( Impl::nullimpl() )
@@ -137,8 +139,8 @@ namespace zypp
 
   ///////////////////////////////////////////////////////////////////
   //
-  //   METHOD NAME : TranslatedText::TranslatedText
-  //   METHOD TYPE : Ctor
+  //        METHOD NAME : TranslatedText::TranslatedText
+  //        METHOD TYPE : Ctor
   //
   TranslatedText::TranslatedText( const std::string &text,
                                   const Locale &lang )
@@ -147,8 +149,8 @@ namespace zypp
 
   ///////////////////////////////////////////////////////////////////
   //
-  //   METHOD NAME : TranslatedText::TranslatedText
-  //   METHOD TYPE : Ctor
+  //        METHOD NAME : TranslatedText::TranslatedText
+  //        METHOD TYPE : Ctor
   //
   TranslatedText::TranslatedText( const std::list<std::string> &text,
                                   const Locale &lang )
@@ -157,8 +159,8 @@ namespace zypp
 
   ///////////////////////////////////////////////////////////////////
   //
-  //   METHOD NAME : TranslatedText::~TranslatedText
-  //   METHOD TYPE : Dtor
+  //        METHOD NAME : TranslatedText::~TranslatedText
+  //        METHOD TYPE : Dtor
   //
   TranslatedText::~TranslatedText()
   {}
@@ -186,6 +188,8 @@ namespace zypp
   Locale TranslatedText::detectLanguage() const
   { return _pimpl->detectLanguage(); }
 
+  bool TranslatedText::empty() const
+  { return _pimpl->empty(); }
   /////////////////////////////////////////////////////////////////
 } // namespace zypp
 ///////////////////////////////////////////////////////////////////