libzypp  15.28.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 {
24  struct CodeMaps
25  {
27  static IdString withoutTrash( IdString code_r )
28  {
30  if ( sep != boost::string_ref::npos )
31  code_r = IdString( code_r.c_str(), sep );
32  return code_r;
33  }
34 
36  static IdString withoutTrash( const std::string & code_r )
37  { return withoutTrash( boost::string_ref(code_r) ); }
38 
40  static IdString withoutTrash( const char * code_r )
41  { return( code_r ? withoutTrash( boost::string_ref(code_r) ) : IdString::Null ); }
42 
44  static IdString combineLC( LanguageCode language_r, CountryCode country_r )
45  {
46  IdString ret;
47  if ( language_r )
48  {
49  if ( country_r )
50  ret = IdString( std::string(language_r) + "_" + country_r.c_str() );
51  else
52  ret = IdString(language_r);
53  }
54  else
55  {
56  if ( country_r )
57  ret = IdString( "_" + std::string(country_r) );
58  else if ( ! ( IdString(language_r) || IdString(country_r) ) )
59  ret = IdString::Null;
60  // else IdString::Empty
61  }
62  return ret;
63  }
64 
66  static CodeMaps & instance()
67  {
68  static CodeMaps _instance;
69  return _instance;
70  }
71 
73  { return getIndex( index_r )._l; }
74 
76  { return getIndex( index_r )._c; }
77 
78  std::string name( IdString index_r )
79  {
80  const LC & lc( getIndex( index_r ) );
81  std::string ret( lc._l.name() );
82  if ( lc._c )
83  {
84  ret += " (";
85  ret += lc._c.name();
86  ret += ")";
87  }
88  return ret;
89  }
90 
92  {
93  static const IdString special( "pt_BR" );
94  Locale ret;
95  if ( index_r == special ) // "pt_BR"->"en" - by now the only fallback exception
96  ret = Locale::enCode;
97  else
98  {
99  const LC & lc( getIndex( index_r ) );
100  if ( lc._c )
101  ret = lc._l;
102  else if ( lc._l && lc._l != LanguageCode::enCode )
103  ret = Locale::enCode;
104  }
105  return ret;
106  }
107 
108  private:
109  static IdString withoutTrash( boost::string_ref code_r )
110  {
112  if ( sep != boost::string_ref::npos )
113  code_r = code_r.substr( 0, sep );
114  return IdString( code_r );
115  }
116 
117  static boost::string_ref::size_type trashStart( boost::string_ref code_r )
118  { return code_r.find_first_of( "@." ); }
119 
121  { return trashStart( boost::string_ref(code_r.c_str()) ); }
122 
123  private:
124  struct LC {
125  LC() {}
126  LC( LanguageCode l_r ) : _l( l_r ) {}
127  LC( LanguageCode l_r, CountryCode c_r ) : _l( l_r ), _c( c_r ) {}
130  };
131  typedef std::unordered_map<IdString,LC> CodeMap;
132 
137  {}
138 
140  const LC & getIndex( IdString index_r )
141  {
142  auto it = _codeMap.find( index_r );
143  if ( it == _codeMap.end() )
144  {
145  CodeMap::value_type newval( index_r, LC() );
146 
147  boost::string_ref str( index_r.c_str() );
148  boost::string_ref::size_type sep = str.find( '_' );
149  if ( sep == boost::string_ref::npos )
150  newval.second._l = LanguageCode( index_r );
151  else
152  {
153  // bsc#1064999: dup! Creating a new IdString may invalidate the IdString.c_str() stored in str.
154  std::string dup( str );
155  str = dup;
156  newval.second._l = LanguageCode( IdString(str.substr( 0, sep )) );
157  newval.second._c = CountryCode( IdString(str.substr( sep+1 )) );
158  }
159 
160  it = _codeMap.insert( std::move(newval) ).first;
161  }
162  return it->second;
163  }
164 
165  private:
167  };
168 
170  // class Locale
172 
173  const Locale Locale::noCode;
174  const LanguageCode LanguageCode::enCode("en"); // from in LanguageCode.cc as Locale::enCode depends on it
176 
178  {}
179 
181  : _str( CodeMaps::withoutTrash( str_r ) )
182  {}
183 
184  Locale::Locale( const std::string & str_r )
185  : _str( CodeMaps::withoutTrash( str_r ) )
186  {}
187 
188  Locale::Locale( const char * str_r )
189  : _str( CodeMaps::withoutTrash( str_r ) )
190  {}
191 
192  Locale::Locale( LanguageCode language_r, CountryCode country_r )
193  : _str( CodeMaps::combineLC( language_r, country_r ) )
194  {}
195 
197  {}
198 
200  { return CodeMaps::instance().language( _str ); }
201 
203  { return CodeMaps::instance().country( _str ); }
204 
205  std::string Locale::name() const
206  { return CodeMaps::instance().name( _str ); }
207 
209  { return CodeMaps::instance().fallback( _str ); }
210 
212 
213  Locale Locale::bestMatch( const LocaleSet & avLocales_r, Locale requested_r )
214  {
215  if ( ! avLocales_r.empty() )
216  {
217  if ( ! requested_r )
218  requested_r = ZConfig::instance().textLocale();
219  for ( ; requested_r; requested_r = requested_r.fallback() )
220  {
221  if ( avLocales_r.count( requested_r ) )
222  return requested_r;
223  }
224  }
225  return Locale();
226  }
227 
228 } // namespace zypp
static const Locale noCode
Empty code.
Definition: Locale.h:74
LanguageCode language() const
The language part.
Definition: Locale.cc:199
static IdString combineLC(LanguageCode language_r, CountryCode country_r)
Return IdString from language/country codes.
Definition: Locale.cc:44
static ZConfig & instance()
Singleton ctor.
Definition: Resolver.cc:125
static const Locale enCode
Last resort "en".
Definition: Locale.h:77
static IdString withoutTrash(const char *code_r)
Return IdString without trailing garbage.
Definition: Locale.cc:40
static const LanguageCode enCode
Last resort "en".
Definition: LanguageCode.h:54
Locale()
Default Ctor: noCode.
Definition: Locale.cc:177
std::unordered_map< IdString, LC > CodeMap
Definition: Locale.cc:131
LanguageCode language(IdString index_r)
Definition: Locale.cc:72
Locale fallback(IdString index_r)
Definition: Locale.cc:91
IdString _str
Definition: Locale.h:112
Access to the sat-pools string space.
Definition: IdString.h:41
static boost::string_ref::size_type trashStart(IdString code_r)
Definition: Locale.cc:120
std::string name() const
Return the translated language name; if unknown the language code.
static Locale bestMatch(const LocaleSet &avLocales_r, Locale requested_r=Locale())
Return the best match for Locale requested_r within the available avLocales_r.
Definition: Locale.cc:213
static boost::string_ref::size_type trashStart(boost::string_ref code_r)
Definition: Locale.cc:117
static const IdString Empty
Empty string.
Definition: IdString.h:70
Locale fallback() const
Return the fallback locale for this locale, if no fallback exists the empty Locale::noCode.
Definition: Locale.cc:208
static CodeMaps & instance()
The singleton.
Definition: Locale.cc:66
static IdString withoutTrash(IdString code_r)
Return IdString without trailing garbage.
Definition: Locale.cc:27
CodeMap _codeMap
Definition: CountryCode.cc:108
CountryCode country() const
The county part.
Definition: Locale.cc:202
const char * c_str() const
Conversion to const char *
Definition: IdString.cc:50
SolvableIdType size_type
Definition: PoolMember.h:152
std::string name() const
Return the translated locale name.
Definition: Locale.cc:205
LanguageCode _l
Definition: Locale.cc:128
'Language[_Country]' codes.
Definition: Locale.h:49
const char * c_str() const
Definition: IdStringType.h:105
std::string name() const
Return the translated country name; if unknown the country code.
Definition: CountryCode.cc:139
LC(LanguageCode l_r)
Definition: Locale.cc:126
LC(LanguageCode l_r, CountryCode c_r)
Definition: Locale.cc:127
CountryCode country(IdString index_r)
Definition: Locale.cc:75
static IdString withoutTrash(const std::string &code_r)
Return IdString without trailing garbage.
Definition: Locale.cc:36
static const IdString Null
No or Null string ( Id 0 ).
Definition: IdString.h:67
std::string name(IdString index_r)
Definition: Locale.cc:78
CodeMaps()
Ctor initializes the code maps.
Definition: Locale.cc:134
Locale textLocale() const
The locale for translated texts zypp uses.
Definition: ZConfig.cc:844
static IdString withoutTrash(boost::string_ref code_r)
Definition: Locale.cc:109
~Locale()
Dtor.
Definition: Locale.cc:196
const LC & getIndex(IdString index_r)
Return LC for index_r, creating it if necessary.
Definition: Locale.cc:140
CountryCode _c
Definition: Locale.cc:129
std::unordered_set< Locale > LocaleSet
Definition: Locale.h:27
Language codes (iso639_2/iso639_1).
Definition: LanguageCode.h:30
Country codes (iso3166-1-alpha-2).
Definition: CountryCode.h:30
CodeMap _codeMap
Definition: Locale.cc:166