| /_____||_| |_| |_| |
| |
\---------------------------------------------------------------------*/
-/** \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;
///////////////////////////////////////////////////////////////////
//
- // CLASS NAME : TranslatedText::Impl
+ // CLASS NAME : TranslatedText::Impl
//
/** TranslatedText implementation. */
struct TranslatedText::Impl
{
+ typedef std::map<Locale, std::string> TranslationMap;
+
Impl()
{}
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);
}
}
private:
- mutable std::map<Locale, std::string> translations;
+ mutable TranslationMap translations;
public:
/** Offer default Impl. */
///////////////////////////////////////////////////////////////////
//
- // CLASS NAME : TranslatedText
+ // CLASS NAME : TranslatedText
//
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//
- // METHOD NAME : TranslatedText::TranslatedText
- // METHOD TYPE : Ctor
+ // METHOD NAME : TranslatedText::TranslatedText
+ // METHOD TYPE : Ctor
//
TranslatedText::TranslatedText()
: _pimpl( Impl::nullimpl() )
///////////////////////////////////////////////////////////////////
//
- // METHOD NAME : TranslatedText::TranslatedText
- // METHOD TYPE : Ctor
+ // METHOD NAME : TranslatedText::TranslatedText
+ // METHOD TYPE : Ctor
//
TranslatedText::TranslatedText( const std::string &text,
const Locale &lang )
///////////////////////////////////////////////////////////////////
//
- // 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 )
///////////////////////////////////////////////////////////////////
//
- // METHOD NAME : TranslatedText::~TranslatedText
- // METHOD TYPE : Dtor
+ // METHOD NAME : TranslatedText::~TranslatedText
+ // METHOD TYPE : Dtor
//
TranslatedText::~TranslatedText()
{}
Locale TranslatedText::detectLanguage() const
{ return _pimpl->detectLanguage(); }
+ bool TranslatedText::empty() const
+ { return _pimpl->empty(); }
/////////////////////////////////////////////////////////////////
} // namespace zypp
///////////////////////////////////////////////////////////////////