ignore
[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 //#include "zypp/base/Logger.h"
14
15 #include "zypp/ZYppFactory.h"
16 #include "zypp/ZYpp.h"
17
18 #include "zypp/base/String.h"
19 #include "zypp/TranslatedText.h"
20
21 using std::endl;
22
23 ///////////////////////////////////////////////////////////////////
24 namespace zypp
25 { /////////////////////////////////////////////////////////////////
26
27   ///////////////////////////////////////////////////////////////////
28   //
29   //    CLASS NAME : TranslatedText::Impl
30   //
31   /** TranslatedText implementation. */
32   struct TranslatedText::Impl
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 = Locale() ) const
49     {
50       Locale empty_locale;
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))
56       {
57           // first, detect the locale
58           ZYpp::Ptr z = getZYpp();
59           Locale detected_lang( z->getTextLocale() );
60           if ( translations[detected_lang].empty() )
61           {
62             Locale fallback_locale = detected_lang.fallback();
63             while ( fallback_locale != empty_locale )
64             {
65               if ( ! translations[fallback_locale].empty() )
66                 return translations[fallback_locale];
67
68               fallback_locale = fallback_locale.fallback();
69             }
70             // we gave up, there are no translations with fallbacks
71             // last try, emtpy locale
72             
73             if ( ! translations[empty_locale].empty() )
74               return translations[empty_locale];
75             else
76               return std::string();
77           }
78           else
79           {
80             return translations[detected_lang];
81           }
82       }
83       else
84       {
85         return translations[lang]; 
86       }
87     }
88
89     std::set<Locale> locales() const
90     {
91       std::set<Locale> lcls;
92       for(std::map<Locale, std::string>::const_iterator it = translations.begin(); it != translations.end(); ++it)
93       {
94         lcls.insert((*it).first);
95       }
96       return lcls;
97     }
98
99     void setText( const std::string &text, const Locale &lang)
100     { translations[lang] = text; }
101
102     void setText( const std::list<std::string> &text, const Locale &lang)
103     { translations[lang] = str::join( text, "\n" ); }
104
105     /** \todo Do it by accessing the global ZYpp. */
106     Locale detectLanguage() const
107     {
108       return Locale();
109     }
110
111   private:
112     mutable std::map<Locale, std::string> translations;
113
114   public:
115     /** Offer default Impl. */
116     static shared_ptr<Impl> nullimpl()
117     {
118       static shared_ptr<Impl> _nullimpl( new Impl );
119       return _nullimpl;
120     }
121
122   private:
123     friend Impl * rwcowClone<Impl>( const Impl * rhs );
124     /** clone for RWCOW_pointer */
125     Impl * clone() const
126     { return new Impl( *this ); }
127   };
128   ///////////////////////////////////////////////////////////////////
129
130   ///////////////////////////////////////////////////////////////////
131   //
132   //    CLASS NAME : TranslatedText
133   //
134   ///////////////////////////////////////////////////////////////////
135
136   const TranslatedText TranslatedText::notext;
137
138   ///////////////////////////////////////////////////////////////////
139   //
140   //    METHOD NAME : TranslatedText::TranslatedText
141   //    METHOD TYPE : Ctor
142   //
143   TranslatedText::TranslatedText()
144   : _pimpl( Impl::nullimpl() )
145   {}
146
147   ///////////////////////////////////////////////////////////////////
148   //
149   //    METHOD NAME : TranslatedText::TranslatedText
150   //    METHOD TYPE : Ctor
151   //
152   TranslatedText::TranslatedText( const std::string &text,
153                                   const Locale &lang )
154   : _pimpl( new Impl(text, lang) )
155   {}
156
157   ///////////////////////////////////////////////////////////////////
158   //
159   //    METHOD NAME : TranslatedText::TranslatedText
160   //    METHOD TYPE : Ctor
161   //
162   TranslatedText::TranslatedText( const std::list<std::string> &text,
163                                   const Locale &lang )
164   : _pimpl( new Impl(text, lang) )
165   {}
166
167   ///////////////////////////////////////////////////////////////////
168   //
169   //    METHOD NAME : TranslatedText::~TranslatedText
170   //    METHOD TYPE : Dtor
171   //
172   TranslatedText::~TranslatedText()
173   {}
174
175   ///////////////////////////////////////////////////////////////////
176   //
177   // Forward to implementation:
178   //
179   ///////////////////////////////////////////////////////////////////
180
181   std::string TranslatedText::text( const Locale &lang ) const
182   { return _pimpl->text( lang ); }
183
184   void TranslatedText::setText( const std::string &text, const Locale &lang )
185   { _pimpl->setText( text, lang ); }
186
187   std::set<Locale> TranslatedText::locales() const
188   {
189     return _pimpl->locales();
190   }
191
192   void TranslatedText::setText( const std::list<std::string> &text, const Locale &lang )
193   { _pimpl->setText( text, lang ); }
194
195   Locale TranslatedText::detectLanguage() const
196   { return _pimpl->detectLanguage(); }
197
198   bool TranslatedText::empty() const
199   { return _pimpl->empty(); }
200   /////////////////////////////////////////////////////////////////
201 } // namespace zypp
202 ///////////////////////////////////////////////////////////////////