libzypp  13.10.6
Locale.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 #include <map>
14 
15 #include "zypp/Locale.h"
16 #include "zypp/ZConfig.h"
17 
18 using std::endl;
19 
21 namespace zypp
22 {
23 
24  typedef std::map<std::string, std::string> OtherDefaultLanguage;
26 
28  //
29  // CLASS NAME : Locale::Impl
30  //
32  struct Locale::Impl
33  {
34  Impl()
35  {}
36 
37  Impl( const std::string & code_r )
38  {
39  std::string t;
40  std::string::size_type sep = code_r.find_first_of( "@." );
41  if ( sep == std::string::npos ) {
42  t = code_r;
43  } else {
44  t = code_r.substr( 0, sep );
45  }
46 
47  sep = t.find( '_' );
48  if ( sep == std::string::npos ) {
49  _language = LanguageCode( t );
50  } else {
51  _language = LanguageCode( t.substr( 0, sep ) );
52  _country = CountryCode( t.substr( sep+1 ) );
53  }
54  }
55 
56  Impl( const LanguageCode & language_r,
57  const CountryCode & country_r )
58  : _language( language_r )
59  , _country( country_r )
60  {}
61 
62  const LanguageCode & language() const
63  { return _language; }
64 
65  const CountryCode & country() const
66  { return _country; }
67 
68  std::string code() const
69  {
70  std::string ret( _language.code() );
71  if ( _country.hasCode() )
72  ret += "_" + _country.code();
73  return ret;
74  }
75 
76  std::string name() const
77  {
78  std::string ret( _language.name() );
79  if ( _country.hasCode() )
80  ret += " (" + _country.name() + ")";
81  return ret;
82  }
83 
84  Locale fallback() const
85  {
86  if (otherDefaultLanguage.size() == 0) {
87  // initial inserting map
88  otherDefaultLanguage["pt_BR"] = "en";
89  }
90 
91  if (otherDefaultLanguage.find(code()) != otherDefaultLanguage.end())
93 
94  if ( _country.hasCode() )
95  return _language;
96 
97  if ( _language.hasCode() && _language != LanguageCode("en") )
98  return LanguageCode("en");
99 
100  return Locale();
101  }
102 
103  private:
104 
107 
108  public:
110  static shared_ptr<Impl> nullimpl()
111  {
112  static shared_ptr<Impl> _nullimpl( new Impl );
113  return _nullimpl;
114  }
115  };
117 
119  inline std::ostream & operator<<( std::ostream & str, const Locale::Impl & obj )
120  {
121  return str << "Locale::Impl";
122  }
123 
125  //
126  // CLASS NAME : Locale
127  //
129 
130  const Locale Locale::noCode;
131 
133  //
134  // METHOD NAME : Locale::Locale
135  // METHOD TYPE : Ctor
136  //
138  : _pimpl( Impl::nullimpl() )
139  {}
140 
142  //
143  // METHOD NAME : Locale::Locale
144  // METHOD TYPE : Ctor
145  //
147  : _pimpl( new Impl( code_r.asString() ) )
148  {}
149 
151  //
152  // METHOD NAME : Locale::Locale
153  // METHOD TYPE : Ctor
154  //
155  Locale::Locale( const std::string & code_r )
156  : _pimpl( new Impl( code_r ) )
157  {}
158 
160  //
161  // METHOD NAME : Locale::Locale
162  // METHOD TYPE : Ctor
163  //
164  Locale::Locale( const char * code_r )
165  : _pimpl( new Impl( C_Str(code_r).c_str() ) )
166  {}
167 
169  //
170  // METHOD NAME : Locale::Locale
171  // METHOD TYPE : Ctor
172  //
173  Locale::Locale( const LanguageCode & language_r,
174  const CountryCode & country_r )
175  : _pimpl( new Impl( language_r, country_r ) )
176  {}
177 
179  //
180  // METHOD NAME : Locale::~Locale
181  // METHOD TYPE : Dtor
182  //
184  {}
185 
187  //
188  // METHOD NAME : Locale::
189  // METHOD TYPE :
190  //
192  { return _pimpl->language(); }
193 
195  //
196  // METHOD NAME : Locale::
197  // METHOD TYPE :
198  //
199  const CountryCode & Locale::country() const
200  { return _pimpl->country(); }
201 
203  //
204  // METHOD NAME : Locale::
205  // METHOD TYPE :
206  //
207  std::string Locale::code() const
208  { return _pimpl->code(); }
209 
211  //
212  // METHOD NAME : Locale::
213  // METHOD TYPE :
214  //
215  std::string Locale::name() const
216  { return _pimpl->name(); }
217 
219  //
220  // METHOD NAME : Locale::
221  // METHOD TYPE :
222  //
224  { return _pimpl->fallback(); }
225 
226 
228 
229  Locale Locale::bestMatch( const LocaleSet & avLocales_r, const Locale & requested_r )
230  {
231  if ( ! avLocales_r.empty() )
232  {
233  for ( Locale check( requested_r == noCode ? ZConfig::instance().textLocale() : requested_r );
234  check != noCode; check = check.fallback() )
235  {
236  if ( avLocales_r.find( check ) != avLocales_r.end() )
237  return check;
238  }
239  }
240  return noCode;
241  }
242 
243 
245 } // namespace zypp
std::string code() const
Definition: Locale.cc:68
static const Locale noCode
No or empty code.
Definition: Locale.h:71
Impl(const std::string &code_r)
Definition: Locale.cc:37
RW_pointer< Impl > _pimpl
Pointer to implementation.
Definition: Locale.h:103
std::string name() const
Definition: Locale.cc:76
static ZConfig & instance()
Singleton ctor.
Definition: ZConfig.cc:655
std::map< std::string, std::string > OtherDefaultLanguage
Definition: Locale.cc:24
const CountryCode & country() const
Definition: Locale.cc:199
Locale()
Default ctor.
Definition: Locale.cc:137
std::ostream & operator<<(std::ostream &str, const Locale::Impl &obj)
Definition: Locale.cc:119
Access to the sat-pools string space.
Definition: IdString.h:39
std::string code() const
Return the language code.
const LanguageCode & language() const
Definition: Locale.cc:62
Impl(const LanguageCode &language_r, const CountryCode &country_r)
Definition: Locale.cc:56
std::string name() const
Return the language name; if not available the language code.
static shared_ptr< Impl > nullimpl()
Offer default Impl.
Definition: Locale.cc:110
Locale fallback() const
Return a fallback locale for this locale, when giving up, returns empty Locale()
Definition: Locale.cc:223
Locale fallback() const
Definition: Locale.cc:84
LanguageCode _language
Definition: Locale.cc:105
Convenience char* constructible from std::string and char*, it maps (char*)0 to an empty string...
Definition: String.h:81
std::tr1::unordered_set< Locale > LocaleSet
Definition: Locale.h:28
SolvableIdType size_type
Definition: PoolMember.h:99
std::string asString(const Patch::SeverityFlag &obj)
Definition: Patch.cc:149
std::string name() const
Return the name made of language and country name.
Definition: Locale.cc:215
CountryCode _country
Definition: Locale.cc:106
std::string code() const
Return the locale code.
Definition: Locale.cc:207
std::string name() const
Return the country name; if not available the country code.
Definition: CountryCode.cc:183
bool hasCode() const
*this != noCode.
Definition: LanguageCode.h:71
bool hasCode() const
*this != noCode.
Definition: CountryCode.h:72
const CountryCode & country() const
Definition: Locale.cc:65
const LanguageCode & language() const
Definition: Locale.cc:191
Locale implementation.
Definition: Locale.cc:32
bool check(const std::string &sequenceinfo_r, bool quick_r)
Check via sequence info.
std::string code() const
Return the country code.
Definition: CountryCode.cc:175
~Locale()
Dtor.
Definition: Locale.cc:183
static Locale bestMatch(const LocaleSet &avLocales_r, const Locale &requested_r=Locale())
Return the best match for Locale requested_r within the available avLocales_r.
Definition: Locale.cc:229
static OtherDefaultLanguage otherDefaultLanguage
Definition: Locale.cc:25
Language codes (iso639_2/iso639_1).
Definition: LanguageCode.h:37
Country codes (iso3166-1-alpha-2).
Definition: CountryCode.h:37