1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/TranslatedText.cc
14 #include "zypp/base/String.h"
16 #include "zypp/TranslatedText.h"
17 #include "zypp/ZConfig.h"
21 ///////////////////////////////////////////////////////////////////
23 { /////////////////////////////////////////////////////////////////
25 ///////////////////////////////////////////////////////////////////
27 // CLASS NAME : TranslatedText::Impl
29 /** TranslatedText implementation. */
30 struct TranslatedText::Impl
32 typedef std::map<Locale, std::string> TranslationMap;
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 ) const
50 // Traverse fallback list and return the 1st nonempty match.
51 // Take care NOT to create new map entries in queries.
52 Locale toReturn( lang );
53 if ( lang == Locale::noCode )
55 toReturn = ZConfig::instance().textLocale();
60 TranslationMap::const_iterator it = translations.find( toReturn );
61 if ( it != translations.end()
62 && ! it->second.empty() )
67 if ( toReturn != Locale::noCode )
69 // retry using next fallback:
70 toReturn = toReturn.fallback();
74 // there are no further fallbacks
82 std::set<Locale> locales() const
84 std::set<Locale> lcls;
85 for( TranslationMap::const_iterator it = translations.begin(); it != translations.end(); ++it )
87 lcls.insert((*it).first);
92 void setText( const std::string &text, const Locale &lang)
93 { translations[lang] = text; }
95 void setText( const std::list<std::string> &text, const Locale &lang)
96 { translations[lang] = str::join( text, "\n" ); }
98 /** \todo Do it by accessing the global ZYpp. */
99 Locale detectLanguage() const
105 mutable TranslationMap translations;
108 /** Offer default Impl. */
109 static shared_ptr<Impl> nullimpl()
111 static shared_ptr<Impl> _nullimpl( new Impl );
116 friend Impl * rwcowClone<Impl>( const Impl * rhs );
117 /** clone for RWCOW_pointer */
119 { return new Impl( *this ); }
121 ///////////////////////////////////////////////////////////////////
123 ///////////////////////////////////////////////////////////////////
125 // CLASS NAME : TranslatedText
127 ///////////////////////////////////////////////////////////////////
129 const TranslatedText TranslatedText::notext;
131 ///////////////////////////////////////////////////////////////////
133 // METHOD NAME : TranslatedText::TranslatedText
134 // METHOD TYPE : Ctor
136 TranslatedText::TranslatedText()
137 : _pimpl( Impl::nullimpl() )
140 ///////////////////////////////////////////////////////////////////
142 // METHOD NAME : TranslatedText::TranslatedText
143 // METHOD TYPE : Ctor
145 TranslatedText::TranslatedText( const std::string &text,
147 : _pimpl( new Impl(text, lang) )
150 ///////////////////////////////////////////////////////////////////
152 // METHOD NAME : TranslatedText::TranslatedText
153 // METHOD TYPE : Ctor
155 TranslatedText::TranslatedText( const std::list<std::string> &text,
157 : _pimpl( new Impl(text, lang) )
160 ///////////////////////////////////////////////////////////////////
162 // METHOD NAME : TranslatedText::~TranslatedText
163 // METHOD TYPE : Dtor
165 TranslatedText::~TranslatedText()
168 ///////////////////////////////////////////////////////////////////
170 // Forward to implementation:
172 ///////////////////////////////////////////////////////////////////
174 std::string TranslatedText::text( const Locale &lang ) const
175 { return _pimpl->text( lang ); }
177 void TranslatedText::setText( const std::string &text, const Locale &lang )
178 { _pimpl->setText( text, lang ); }
180 std::set<Locale> TranslatedText::locales() const
182 return _pimpl->locales();
185 void TranslatedText::setText( const std::list<std::string> &text, const Locale &lang )
186 { _pimpl->setText( text, lang ); }
188 Locale TranslatedText::detectLanguage() const
189 { return _pimpl->detectLanguage(); }
191 bool TranslatedText::empty() const
192 { return _pimpl->empty(); }
193 /////////////////////////////////////////////////////////////////
195 ///////////////////////////////////////////////////////////////////