TranslatedText.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <iostream>
00013 //#include "zypp/base/Logger.h"
00014 
00015 #include "zypp/ZYppFactory.h"
00016 #include "zypp/ZYpp.h"
00017 
00018 #include "zypp/base/String.h"
00019 #include "zypp/TranslatedText.h"
00020 
00021 using std::endl;
00022 
00024 namespace zypp
00025 { 
00026 
00028   //
00029   //    CLASS NAME : TranslatedText::Impl
00030   //
00032   struct TranslatedText::Impl
00033   {
00034     Impl()
00035     {}
00036 
00037     Impl(const std::string &text, const Locale &lang)
00038     { setText(text, lang); }
00039 
00040     Impl(const std::list<std::string> &text, const Locale &lang)
00041     { setText(text, lang); }
00042 
00043     bool empty() const
00044     {
00045       return translations.empty();
00046     }
00047     
00048     std::string text( const Locale &lang = Locale() ) const
00049     {
00050       Locale empty_locale;
00051       // if there are no translation for the requested locale
00052       // or the passed locale is a empty one, we activate the
00053       // fallback mechanism, otherwise (else case), we just provide
00054       // the (existant) requested locale)
00055       if ( translations[lang].empty() || (lang == empty_locale))
00056       {
00057           // first, detect the locale
00058           ZYpp::Ptr z = getZYpp();
00059           Locale detected_lang( z->getTextLocale() );
00060           if ( translations[detected_lang].empty() )
00061           {
00062             Locale fallback_locale = detected_lang.fallback();
00063             while ( fallback_locale != empty_locale )
00064             {
00065               if ( ! translations[fallback_locale].empty() )
00066                 return translations[fallback_locale];
00067 
00068               fallback_locale = fallback_locale.fallback();
00069             }
00070             // we gave up, there are no translations with fallbacks
00071             // last try, emtpy locale
00072             
00073             if ( ! translations[empty_locale].empty() )
00074               return translations[empty_locale];
00075             else
00076               return std::string();
00077           }
00078           else
00079           {
00080             return translations[detected_lang];
00081           }
00082       }
00083       else
00084       {
00085         return translations[lang]; 
00086       }
00087     }
00088 
00089     std::set<Locale> locales() const
00090     {
00091       std::set<Locale> lcls;
00092       for(std::map<Locale, std::string>::const_iterator it = translations.begin(); it != translations.end(); ++it)
00093       {
00094         lcls.insert((*it).first);
00095       }
00096       return lcls;
00097     }
00098 
00099     void setText( const std::string &text, const Locale &lang)
00100     { translations[lang] = text; }
00101 
00102     void setText( const std::list<std::string> &text, const Locale &lang)
00103     { translations[lang] = str::join( text, "\n" ); }
00104 
00106     Locale detectLanguage() const
00107     {
00108       return Locale();
00109     }
00110 
00111   private:
00112     mutable std::map<Locale, std::string> translations;
00113 
00114   public:
00116     static shared_ptr<Impl> nullimpl()
00117     {
00118       static shared_ptr<Impl> _nullimpl( new Impl );
00119       return _nullimpl;
00120     }
00121 
00122   private:
00123     friend Impl * rwcowClone<Impl>( const Impl * rhs );
00125     Impl * clone() const
00126     { return new Impl( *this ); }
00127   };
00129 
00131   //
00132   //    CLASS NAME : TranslatedText
00133   //
00135 
00136   const TranslatedText TranslatedText::notext;
00137 
00139   //
00140   //    METHOD NAME : TranslatedText::TranslatedText
00141   //    METHOD TYPE : Ctor
00142   //
00143   TranslatedText::TranslatedText()
00144   : _pimpl( Impl::nullimpl() )
00145   {}
00146 
00148   //
00149   //    METHOD NAME : TranslatedText::TranslatedText
00150   //    METHOD TYPE : Ctor
00151   //
00152   TranslatedText::TranslatedText( const std::string &text,
00153                                   const Locale &lang )
00154   : _pimpl( new Impl(text, lang) )
00155   {}
00156 
00158   //
00159   //    METHOD NAME : TranslatedText::TranslatedText
00160   //    METHOD TYPE : Ctor
00161   //
00162   TranslatedText::TranslatedText( const std::list<std::string> &text,
00163                                   const Locale &lang )
00164   : _pimpl( new Impl(text, lang) )
00165   {}
00166 
00168   //
00169   //    METHOD NAME : TranslatedText::~TranslatedText
00170   //    METHOD TYPE : Dtor
00171   //
00172   TranslatedText::~TranslatedText()
00173   {}
00174 
00176   //
00177   // Forward to implementation:
00178   //
00180 
00181   std::string TranslatedText::text( const Locale &lang ) const
00182   { return _pimpl->text( lang ); }
00183 
00184   void TranslatedText::setText( const std::string &text, const Locale &lang )
00185   { _pimpl->setText( text, lang ); }
00186 
00187   std::set<Locale> TranslatedText::locales() const
00188   {
00189     return _pimpl->locales();
00190   }
00191 
00192   void TranslatedText::setText( const std::list<std::string> &text, const Locale &lang )
00193   { _pimpl->setText( text, lang ); }
00194 
00195   Locale TranslatedText::detectLanguage() const
00196   { return _pimpl->detectLanguage(); }
00197 
00198   bool TranslatedText::empty() const
00199   { return _pimpl->empty(); }
00201 } // namespace zypp

Generated on Thu Jul 6 00:07:28 2006 for zypp by  doxygen 1.4.6