TranslatedText.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <iostream>
00013 
00014 #include "zypp/base/String.h"
00015 
00016 #include "zypp/TranslatedText.h"
00017 #include "zypp/ZConfig.h"
00018 
00019 using std::endl;
00020 
00022 namespace zypp
00023 { 
00024 
00026   //
00027   //        CLASS NAME : TranslatedText::Impl
00028   //
00030   struct TranslatedText::Impl
00031   {
00032     typedef std::map<Locale, std::string> TranslationMap;
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 ) const
00049     {
00050       // Traverse fallback list and return the 1st nonempty match.
00051       // Take care NOT to create new map entries in queries.
00052       Locale toReturn( lang );
00053       if ( lang == Locale::noCode )
00054       {
00055         toReturn = ZConfig::instance().textLocale();
00056       }
00057 
00058       do
00059       {
00060         TranslationMap::const_iterator it = translations.find( toReturn );
00061         if ( it != translations.end()
00062              && ! it->second.empty() )
00063         {
00064           return it->second;
00065         }
00066 
00067         if ( toReturn != Locale::noCode )
00068         {
00069           // retry using next fallback:
00070           toReturn = toReturn.fallback();
00071         }
00072         else
00073         {
00074           // there are no further fallbacks
00075           return std::string();
00076         }
00077       } while( true );
00078       // not reached.
00079       return std::string();
00080     }
00081 
00082     std::set<Locale> locales() const
00083     {
00084       std::set<Locale> lcls;
00085       for( TranslationMap::const_iterator it = translations.begin(); it != translations.end(); ++it )
00086       {
00087         lcls.insert((*it).first);
00088       }
00089       return lcls;
00090     }
00091 
00092     void setText( const std::string &text, const Locale &lang)
00093     { translations[lang] = text; }
00094 
00095     void setText( const std::list<std::string> &text, const Locale &lang)
00096     { translations[lang] = str::join( text, "\n" ); }
00097 
00099     Locale detectLanguage() const
00100     {
00101       return Locale();
00102     }
00103 
00104   private:
00105     mutable TranslationMap translations;
00106 
00107   public:
00109     static shared_ptr<Impl> nullimpl()
00110     {
00111       static shared_ptr<Impl> _nullimpl( new Impl );
00112       return _nullimpl;
00113     }
00114 
00115   private:
00116     friend Impl * rwcowClone<Impl>( const Impl * rhs );
00118     Impl * clone() const
00119     { return new Impl( *this ); }
00120   };
00122 
00124   //
00125   //        CLASS NAME : TranslatedText
00126   //
00128 
00129   const TranslatedText TranslatedText::notext;
00130 
00132   //
00133   //        METHOD NAME : TranslatedText::TranslatedText
00134   //        METHOD TYPE : Ctor
00135   //
00136   TranslatedText::TranslatedText()
00137   : _pimpl( Impl::nullimpl() )
00138   {}
00139 
00141   //
00142   //        METHOD NAME : TranslatedText::TranslatedText
00143   //        METHOD TYPE : Ctor
00144   //
00145   TranslatedText::TranslatedText( const std::string &text,
00146                                   const Locale &lang )
00147   : _pimpl( new Impl(text, lang) )
00148   {}
00149 
00151   //
00152   //        METHOD NAME : TranslatedText::TranslatedText
00153   //        METHOD TYPE : Ctor
00154   //
00155   TranslatedText::TranslatedText( const std::list<std::string> &text,
00156                                   const Locale &lang )
00157   : _pimpl( new Impl(text, lang) )
00158   {}
00159 
00161   //
00162   //        METHOD NAME : TranslatedText::~TranslatedText
00163   //        METHOD TYPE : Dtor
00164   //
00165   TranslatedText::~TranslatedText()
00166   {}
00167 
00169   //
00170   // Forward to implementation:
00171   //
00173 
00174   std::string TranslatedText::text( const Locale &lang ) const
00175   { return _pimpl->text( lang ); }
00176 
00177   void TranslatedText::setText( const std::string &text, const Locale &lang )
00178   { _pimpl->setText( text, lang ); }
00179 
00180   std::set<Locale> TranslatedText::locales() const
00181   {
00182     return _pimpl->locales();
00183   }
00184 
00185   void TranslatedText::setText( const std::list<std::string> &text, const Locale &lang )
00186   { _pimpl->setText( text, lang ); }
00187 
00188   Locale TranslatedText::detectLanguage() const
00189   { return _pimpl->detectLanguage(); }
00190 
00191   bool TranslatedText::empty() const
00192   { return _pimpl->empty(); }
00194 } // namespace zypp

Generated on Tue Sep 25 19:23:10 2007 for libzypp by  doxygen 1.5.3