Added Locale
[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 "zypp/base/Logger.h"
14
15 #include "zypp/Locale.h"
16
17 using std::endl;
18
19 ///////////////////////////////////////////////////////////////////
20 namespace zypp
21 { /////////////////////////////////////////////////////////////////
22
23   ///////////////////////////////////////////////////////////////////
24   //
25   //    CLASS NAME : Locale::Impl
26   //
27   /** Locale implementation. */
28   struct Locale::Impl
29   {
30     Impl()
31     {}
32
33     Impl( const std::string & code_r )
34     {
35       std::string t;
36       std::string::size_type sep = code_r.find_first_of( "@." );
37       if ( sep == std::string::npos ) {
38         t = code_r;
39       } else {
40         t = code_r.substr( 0, sep );
41       }
42
43       sep = t.find( '_' );
44       if ( sep == std::string::npos ) {
45         _language = LanguageCode( t );
46       } else {
47         _language = LanguageCode( t.substr( 0, sep ) );
48         _country = CountryCode( t.substr( sep+1 ) );
49       }
50     }
51
52     Impl( const LanguageCode & language_r,
53           const CountryCode & country_r )
54     : _language( language_r )
55     , _country( country_r )
56     {}
57
58     const LanguageCode & language() const
59     { return _language; }
60
61     const CountryCode & country() const
62     { return _country; }
63
64     std::string code() const
65     {
66       std::string ret( _language.code() );
67       if ( _country.hasCode() )
68         ret += "_" + _country.code();
69       return ret;
70     }
71
72     std::string name() const
73     {
74       std::string ret( _language.name() );
75       if ( _country.hasCode() )
76         ret += " (" + _country.name() + ")";
77       return ret;
78     }
79
80   private:
81
82     LanguageCode _language;
83     CountryCode _country;
84
85   public:
86     /** Offer default Impl. */
87     static shared_ptr<Impl> nullimpl()
88     { if ( ! _nullimpl ) _nullimpl.reset( new Impl ); return _nullimpl; }
89
90   private:
91     /** Default Impl. */
92     static shared_ptr<Impl> _nullimpl;
93   };
94   ///////////////////////////////////////////////////////////////////
95
96   shared_ptr<Locale::Impl> Locale::Impl::_nullimpl;
97
98   ///////////////////////////////////////////////////////////////////
99
100   /** \relates Locale::Impl Stream output */
101   inline std::ostream & operator<<( std::ostream & str, const Locale::Impl & obj )
102   {
103     return str << "Locale::Impl";
104   }
105
106   ///////////////////////////////////////////////////////////////////
107   //
108   //    CLASS NAME : Locale
109   //
110   ///////////////////////////////////////////////////////////////////
111
112   const Locale Locale::noCode;
113
114   ///////////////////////////////////////////////////////////////////
115   //
116   //    METHOD NAME : Locale::Locale
117   //    METHOD TYPE : Ctor
118   //
119   Locale::Locale()
120   : _pimpl( Impl::nullimpl() )
121   {}
122
123   ///////////////////////////////////////////////////////////////////
124   //
125   //    METHOD NAME : Locale::Locale
126   //    METHOD TYPE : Ctor
127   //
128   Locale::Locale( const std::string & code_r )
129   : _pimpl( new Impl( code_r ) )
130   {}
131
132   ///////////////////////////////////////////////////////////////////
133   //
134   //    METHOD NAME : Locale::Locale
135   //    METHOD TYPE : Ctor
136   //
137   Locale::Locale( const LanguageCode & language_r,
138                   const CountryCode & country_r )
139   : _pimpl( new Impl( language_r, country_r ) )
140   {}
141
142   ///////////////////////////////////////////////////////////////////
143   //
144   //    METHOD NAME : Locale::~Locale
145   //    METHOD TYPE : Dtor
146   //
147   Locale::~Locale()
148   {}
149
150   ///////////////////////////////////////////////////////////////////
151   //
152   //    METHOD NAME : Locale::
153   //    METHOD TYPE :
154   //
155   const LanguageCode & Locale::language() const
156   { return _pimpl->language(); }
157
158   ///////////////////////////////////////////////////////////////////
159   //
160   //    METHOD NAME : Locale::
161   //    METHOD TYPE :
162   //
163   const CountryCode & Locale::country() const
164   { return _pimpl->country(); }
165
166   ///////////////////////////////////////////////////////////////////
167   //
168   //    METHOD NAME : Locale::
169   //    METHOD TYPE :
170   //
171   std::string Locale::code() const
172   { return _pimpl->code(); }
173
174   ///////////////////////////////////////////////////////////////////
175   //
176   //    METHOD NAME : Locale::
177   //    METHOD TYPE :
178   //
179   std::string Locale::name() const
180   { return _pimpl->name(); }
181
182   /////////////////////////////////////////////////////////////////
183 } // namespace zypp
184 ///////////////////////////////////////////////////////////////////