1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/TranslatedText.cc
13 //#include "zypp/base/Logger.h"
15 #include "zypp/ZYppFactory.h"
16 #include "zypp/ZYpp.h"
18 #include "zypp/base/String.h"
19 #include "zypp/TranslatedText.h"
23 ///////////////////////////////////////////////////////////////////
25 { /////////////////////////////////////////////////////////////////
27 ///////////////////////////////////////////////////////////////////
29 // CLASS NAME : TranslatedText::Impl
31 /** TranslatedText implementation. */
32 struct TranslatedText::Impl
37 Impl(const std::string &text, const Locale &lang)
38 { setText(text, lang); }
40 Impl(const std::list<std::string> &text, const Locale &lang)
41 { setText(text, lang); }
45 return translations.empty();
48 std::string text( const Locale &lang = Locale() ) const
51 // if there are no translation for the requested locale
52 // or the passed locale is a empty one, we activate the
53 // fallback mechanism, otherwise (else case), we just provide
54 // the (existant) requested locale)
55 if ( translations[lang].empty() || (lang == empty_locale))
57 // first, detect the locale
58 ZYpp::Ptr z = getZYpp();
59 Locale detected_lang( z->getTextLocale() );
60 if ( translations[detected_lang].empty() )
62 Locale fallback_locale = detected_lang.fallback();
63 while ( fallback_locale != empty_locale )
65 if ( ! translations[fallback_locale].empty() )
66 return translations[fallback_locale];
68 fallback_locale = fallback_locale.fallback();
70 // we gave up, there are no translations with fallbacks
71 // last try, emtpy locale
73 if ( ! translations[empty_locale].empty() )
74 return translations[empty_locale];
80 return translations[detected_lang];
85 return translations[lang];
89 std::set<Locale> locales() const
91 std::set<Locale> lcls;
92 for(std::map<Locale, std::string>::const_iterator it = translations.begin(); it != translations.end(); ++it)
94 lcls.insert((*it).first);
99 void setText( const std::string &text, const Locale &lang)
100 { translations[lang] = text; }
102 void setText( const std::list<std::string> &text, const Locale &lang)
103 { translations[lang] = str::join( text, "\n" ); }
105 /** \todo Do it by accessing the global ZYpp. */
106 Locale detectLanguage() const
112 mutable std::map<Locale, std::string> translations;
115 /** Offer default Impl. */
116 static shared_ptr<Impl> nullimpl()
118 static shared_ptr<Impl> _nullimpl( new Impl );
123 friend Impl * rwcowClone<Impl>( const Impl * rhs );
124 /** clone for RWCOW_pointer */
126 { return new Impl( *this ); }
128 ///////////////////////////////////////////////////////////////////
130 ///////////////////////////////////////////////////////////////////
132 // CLASS NAME : TranslatedText
134 ///////////////////////////////////////////////////////////////////
136 const TranslatedText TranslatedText::notext;
138 ///////////////////////////////////////////////////////////////////
140 // METHOD NAME : TranslatedText::TranslatedText
141 // METHOD TYPE : Ctor
143 TranslatedText::TranslatedText()
144 : _pimpl( Impl::nullimpl() )
147 ///////////////////////////////////////////////////////////////////
149 // METHOD NAME : TranslatedText::TranslatedText
150 // METHOD TYPE : Ctor
152 TranslatedText::TranslatedText( const std::string &text,
154 : _pimpl( new Impl(text, lang) )
157 ///////////////////////////////////////////////////////////////////
159 // METHOD NAME : TranslatedText::TranslatedText
160 // METHOD TYPE : Ctor
162 TranslatedText::TranslatedText( const std::list<std::string> &text,
164 : _pimpl( new Impl(text, lang) )
167 ///////////////////////////////////////////////////////////////////
169 // METHOD NAME : TranslatedText::~TranslatedText
170 // METHOD TYPE : Dtor
172 TranslatedText::~TranslatedText()
175 ///////////////////////////////////////////////////////////////////
177 // Forward to implementation:
179 ///////////////////////////////////////////////////////////////////
181 std::string TranslatedText::text( const Locale &lang ) const
182 { return _pimpl->text( lang ); }
184 void TranslatedText::setText( const std::string &text, const Locale &lang )
185 { _pimpl->setText( text, lang ); }
187 std::set<Locale> TranslatedText::locales() const
189 return _pimpl->locales();
192 void TranslatedText::setText( const std::list<std::string> &text, const Locale &lang )
193 { _pimpl->setText( text, lang ); }
195 Locale TranslatedText::detectLanguage() const
196 { return _pimpl->detectLanguage(); }
198 bool TranslatedText::empty() const
199 { return _pimpl->empty(); }
200 /////////////////////////////////////////////////////////////////
202 ///////////////////////////////////////////////////////////////////