merge REFACTORING-10_3 back to trunk
[platform/upstream/libzypp.git] / zypp / TranslatedText.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file        zypp/TranslatedText.cc
10  *
11 */
12 #include <iostream>
13
14 #include "zypp/base/String.h"
15
16 #include "zypp/TranslatedText.h"
17 #include "zypp/ZConfig.h"
18
19 using std::endl;
20
21 ///////////////////////////////////////////////////////////////////
22 namespace zypp
23 { /////////////////////////////////////////////////////////////////
24
25   ///////////////////////////////////////////////////////////////////
26   //
27   //        CLASS NAME : TranslatedText::Impl
28   //
29   /** TranslatedText implementation. */
30   struct TranslatedText::Impl
31   {
32     typedef std::map<Locale, std::string> TranslationMap;
33
34     Impl()
35     {}
36
37     Impl(const std::string &text, const Locale &lang)
38     { setText(text, lang); }
39
40     Impl(const std::list<std::string> &text, const Locale &lang)
41     { setText(text, lang); }
42
43     bool empty() const
44     {
45       return translations.empty();
46     }
47
48     std::string text( const Locale &lang ) const
49     {
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 )
54       {
55         toReturn = ZConfig().defaultTextLocale();
56       }
57
58       do
59       {
60         TranslationMap::const_iterator it = translations.find( toReturn );
61         if ( it != translations.end()
62              && ! it->second.empty() )
63         {
64           return it->second;
65         }
66
67         if ( toReturn != Locale::noCode )
68         {
69           // retry using next fallback:
70           toReturn = toReturn.fallback();
71         }
72         else
73         {
74           // there are no further fallbacks
75           return std::string();
76         }
77       } while( true );
78       // not reached.
79       return std::string();
80     }
81
82     std::set<Locale> locales() const
83     {
84       std::set<Locale> lcls;
85       for( TranslationMap::const_iterator it = translations.begin(); it != translations.end(); ++it )
86       {
87         lcls.insert((*it).first);
88       }
89       return lcls;
90     }
91
92     void setText( const std::string &text, const Locale &lang)
93     { translations[lang] = text; }
94
95     void setText( const std::list<std::string> &text, const Locale &lang)
96     { translations[lang] = str::join( text, "\n" ); }
97
98     /** \todo Do it by accessing the global ZYpp. */
99     Locale detectLanguage() const
100     {
101       return Locale();
102     }
103
104   private:
105     mutable TranslationMap translations;
106
107   public:
108     /** Offer default Impl. */
109     static shared_ptr<Impl> nullimpl()
110     {
111       static shared_ptr<Impl> _nullimpl( new Impl );
112       return _nullimpl;
113     }
114
115   private:
116     friend Impl * rwcowClone<Impl>( const Impl * rhs );
117     /** clone for RWCOW_pointer */
118     Impl * clone() const
119     { return new Impl( *this ); }
120   };
121   ///////////////////////////////////////////////////////////////////
122
123   ///////////////////////////////////////////////////////////////////
124   //
125   //        CLASS NAME : TranslatedText
126   //
127   ///////////////////////////////////////////////////////////////////
128
129   const TranslatedText TranslatedText::notext;
130
131   ///////////////////////////////////////////////////////////////////
132   //
133   //        METHOD NAME : TranslatedText::TranslatedText
134   //        METHOD TYPE : Ctor
135   //
136   TranslatedText::TranslatedText()
137   : _pimpl( Impl::nullimpl() )
138   {}
139
140   ///////////////////////////////////////////////////////////////////
141   //
142   //        METHOD NAME : TranslatedText::TranslatedText
143   //        METHOD TYPE : Ctor
144   //
145   TranslatedText::TranslatedText( const std::string &text,
146                                   const Locale &lang )
147   : _pimpl( new Impl(text, lang) )
148   {}
149
150   ///////////////////////////////////////////////////////////////////
151   //
152   //        METHOD NAME : TranslatedText::TranslatedText
153   //        METHOD TYPE : Ctor
154   //
155   TranslatedText::TranslatedText( const std::list<std::string> &text,
156                                   const Locale &lang )
157   : _pimpl( new Impl(text, lang) )
158   {}
159
160   ///////////////////////////////////////////////////////////////////
161   //
162   //        METHOD NAME : TranslatedText::~TranslatedText
163   //        METHOD TYPE : Dtor
164   //
165   TranslatedText::~TranslatedText()
166   {}
167
168   ///////////////////////////////////////////////////////////////////
169   //
170   // Forward to implementation:
171   //
172   ///////////////////////////////////////////////////////////////////
173
174   std::string TranslatedText::text( const Locale &lang ) const
175   { return _pimpl->text( lang ); }
176
177   void TranslatedText::setText( const std::string &text, const Locale &lang )
178   { _pimpl->setText( text, lang ); }
179
180   std::set<Locale> TranslatedText::locales() const
181   {
182     return _pimpl->locales();
183   }
184
185   void TranslatedText::setText( const std::list<std::string> &text, const Locale &lang )
186   { _pimpl->setText( text, lang ); }
187
188   Locale TranslatedText::detectLanguage() const
189   { return _pimpl->detectLanguage(); }
190
191   bool TranslatedText::empty() const
192   { return _pimpl->empty(); }
193   /////////////////////////////////////////////////////////////////
194 } // namespace zypp
195 ///////////////////////////////////////////////////////////////////