ignore
[platform/upstream/libzypp.git] / zypp / Locale.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/Locale.cc
10  *
11 */
12 #include <iostream>
13 #include <map>
14
15 #include "zypp/Locale.h"
16
17 using std::endl;
18
19 ///////////////////////////////////////////////////////////////////
20 namespace zypp
21 { /////////////////////////////////////////////////////////////////
22
23   typedef std::map<std::string, std::string> OtherDefaultLanguage;
24   static OtherDefaultLanguage otherDefaultLanguage;
25     
26   ///////////////////////////////////////////////////////////////////
27   //
28   //    CLASS NAME : Locale::Impl
29   //
30   /** Locale implementation. */
31   struct Locale::Impl
32   {
33     Impl()
34     {}
35
36     Impl( const std::string & code_r )
37     {
38       std::string t;
39       std::string::size_type sep = code_r.find_first_of( "@." );
40       if ( sep == std::string::npos ) {
41         t = code_r;
42       } else {
43         t = code_r.substr( 0, sep );
44       }
45
46       sep = t.find( '_' );
47       if ( sep == std::string::npos ) {
48         _language = LanguageCode( t );
49       } else {
50         _language = LanguageCode( t.substr( 0, sep ) );
51         _country = CountryCode( t.substr( sep+1 ) );
52       }
53     }
54
55     Impl( const LanguageCode & language_r,
56           const CountryCode & country_r )
57     : _language( language_r )
58     , _country( country_r )
59     {}
60
61     const LanguageCode & language() const
62     { return _language; }
63
64     const CountryCode & country() const
65     { return _country; }
66
67     std::string code() const
68     {
69       std::string ret( _language.code() );
70       if ( _country.hasCode() )
71         ret += "_" + _country.code();
72       return ret;
73     }
74
75     std::string name() const
76     {
77       std::string ret( _language.name() );
78       if ( _country.hasCode() )
79         ret += " (" + _country.name() + ")";
80       return ret;
81     }
82
83     Locale fallback() const
84     {
85       if (otherDefaultLanguage.size() == 0) {
86           // initial inserting map
87           otherDefaultLanguage["pt_BR"] = "en";
88       }
89
90       if (otherDefaultLanguage.find(code()) != otherDefaultLanguage.end())
91           return LanguageCode(otherDefaultLanguage[code()]);
92             
93       if ( _country.hasCode() )
94         return _language;
95
96       if ( _language.hasCode() && _language != LanguageCode("en") )
97         return LanguageCode("en");
98
99       return Locale();
100     }
101
102   private:
103
104     LanguageCode _language;
105     CountryCode _country;
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   ///////////////////////////////////////////////////////////////////
116
117   /** \relates Locale::Impl Stream output */
118   inline std::ostream & operator<<( std::ostream & str, const Locale::Impl & obj )
119   {
120     return str << "Locale::Impl";
121   }
122
123   ///////////////////////////////////////////////////////////////////
124   //
125   //    CLASS NAME : Locale
126   //
127   ///////////////////////////////////////////////////////////////////
128
129   const Locale Locale::noCode;
130
131   ///////////////////////////////////////////////////////////////////
132   //
133   //    METHOD NAME : Locale::Locale
134   //    METHOD TYPE : Ctor
135   //
136   Locale::Locale()
137   : _pimpl( Impl::nullimpl() )
138   {}
139
140   ///////////////////////////////////////////////////////////////////
141   //
142   //    METHOD NAME : Locale::Locale
143   //    METHOD TYPE : Ctor
144   //
145   Locale::Locale( const std::string & code_r )
146   : _pimpl( new Impl( code_r ) )
147   {}
148
149   ///////////////////////////////////////////////////////////////////
150   //
151   //    METHOD NAME : Locale::Locale
152   //    METHOD TYPE : Ctor
153   //
154   Locale::Locale( const LanguageCode & language_r,
155                   const CountryCode & country_r )
156   : _pimpl( new Impl( language_r, country_r ) )
157   {}
158
159   ///////////////////////////////////////////////////////////////////
160   //
161   //    METHOD NAME : Locale::~Locale
162   //    METHOD TYPE : Dtor
163   //
164   Locale::~Locale()
165   {}
166
167   ///////////////////////////////////////////////////////////////////
168   //
169   //    METHOD NAME : Locale::
170   //    METHOD TYPE :
171   //
172   const LanguageCode & Locale::language() const
173   { return _pimpl->language(); }
174
175   ///////////////////////////////////////////////////////////////////
176   //
177   //    METHOD NAME : Locale::
178   //    METHOD TYPE :
179   //
180   const CountryCode & Locale::country() const
181   { return _pimpl->country(); }
182
183   ///////////////////////////////////////////////////////////////////
184   //
185   //    METHOD NAME : Locale::
186   //    METHOD TYPE :
187   //
188   std::string Locale::code() const
189   { return _pimpl->code(); }
190
191   ///////////////////////////////////////////////////////////////////
192   //
193   //    METHOD NAME : Locale::
194   //    METHOD TYPE :
195   //
196   std::string Locale::name() const
197   { return _pimpl->name(); }
198
199   ///////////////////////////////////////////////////////////////////
200   //
201   //    METHOD NAME : Locale::
202   //    METHOD TYPE :
203   //
204   Locale Locale::fallback() const
205   { return _pimpl->fallback(); }
206   /////////////////////////////////////////////////////////////////
207 } // namespace zypp
208 ///////////////////////////////////////////////////////////////////