libzypp 17.38.7
Locale.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <iostream>
13#include <boost/utility/string_ref.hpp>
14
15#include <zypp/Locale.h>
16
17#ifndef ZYPPNG
18#include <zypp/ZConfig.h>
19#endif
20
21using std::endl;
22
24namespace zypp
25{
27 struct CodeMaps
28 {
31 {
32 boost::string_ref::size_type sep = trashStart( code_r );
33 if ( sep != boost::string_ref::npos )
34 code_r = IdString( code_r.c_str(), sep );
35 return code_r;
36 }
37
39 static IdString withoutTrash( const std::string & code_r )
40 { return withoutTrash( boost::string_ref(code_r) ); }
41
43 static IdString withoutTrash( const char * code_r )
44 { return( code_r ? withoutTrash( boost::string_ref(code_r) ) : IdString::Null ); }
45
47 static IdString combineLC( const LanguageCode& language_r, const CountryCode& country_r )
48 {
49 IdString ret;
50 if ( language_r )
51 {
52 if ( country_r )
53 ret = IdString( std::string(language_r) + "_" + country_r.c_str() );
54 else
55 ret = IdString(language_r);
56 }
57 else
58 {
59 if ( country_r )
60 ret = IdString( "_" + std::string(country_r) );
61 else if ( ! ( IdString(language_r) || IdString(country_r) ) )
62 ret = IdString::Null;
63 // else IdString::Empty
64 }
65 return ret;
66 }
67
69 static CodeMaps & instance()
70 {
71 static CodeMaps _instance;
72 return _instance;
73 }
74
76 { return getIndex( index_r )._l; }
77
79 { return getIndex( index_r )._c; }
80
81 std::string name( IdString index_r )
82 {
83 const LC & lc( getIndex( index_r ) );
84 std::string ret( lc._l.name() );
85 if ( lc._c )
86 {
87 ret += " (";
88 ret += lc._c.name();
89 ret += ")";
90 }
91 return ret;
92 }
93
95 {
96 static const IdString special( "pt_BR" );
97 Locale ret;
98 if ( index_r == special ) // "pt_BR"->"en" - by now the only fallback exception
99 ret = Locale::enCode;
100 else
101 {
102 const LC & lc( getIndex( index_r ) );
103 if ( lc._c )
104 ret = lc._l;
105 else if ( lc._l && lc._l != LanguageCode::enCode )
106 ret = Locale::enCode;
107 }
108 return ret;
109 }
110
111 private:
112 static IdString withoutTrash( boost::string_ref code_r )
113 {
114 boost::string_ref::size_type sep = trashStart( code_r );
115 if ( sep != boost::string_ref::npos )
116 code_r = code_r.substr( 0, sep );
117 return IdString( code_r );
118 }
119
120 static boost::string_ref::size_type trashStart( boost::string_ref code_r )
121 { return code_r.find_first_of( "@." ); }
122
123 static boost::string_ref::size_type trashStart( IdString code_r )
124 { return trashStart( boost::string_ref(code_r.c_str()) ); }
125
126 private:
127 struct LC {
128 LC() {}
129 LC( const LanguageCode& l_r ) : _l( l_r ) {}
130 LC( const LanguageCode& l_r, const CountryCode& c_r ) : _l( l_r ), _c( c_r ) {}
133 };
134 using CodeMap = std::unordered_map<IdString, LC>;
135
141
143 const LC & getIndex( IdString index_r )
144 {
145 auto it = _codeMap.find( index_r );
146 if ( it == _codeMap.end() )
147 {
148 CodeMap::value_type newval( index_r, LC() );
149
150 boost::string_ref str( index_r.c_str() );
151 boost::string_ref::size_type sep = str.find( '_' );
152 if ( sep == boost::string_ref::npos )
153 newval.second._l = LanguageCode( index_r );
154 else
155 {
156 // bsc#1064999: dup! Creating a new IdString may invalidate the IdString.c_str() stored in str.
157 std::string dup( str );
158 str = dup;
159 newval.second._l = LanguageCode( IdString(str.substr( 0, sep )) );
160 newval.second._c = CountryCode( IdString(str.substr( sep+1 )) );
161 }
162
163 it = _codeMap.insert( std::move(newval) ).first;
164 }
165 return it->second;
166 }
167
168 private:
170 };
171
173 // class Locale
175
177 const LanguageCode LanguageCode::enCode("en"); // from in LanguageCode.cc as Locale::enCode depends on it
179
182
184 : _str( CodeMaps::withoutTrash( str_r ) )
185 {}
186
187 Locale::Locale( const std::string & str_r )
188 : _str( CodeMaps::withoutTrash( str_r ) )
189 {}
190
191 Locale::Locale( const char * str_r )
192 : _str( CodeMaps::withoutTrash( str_r ) )
193 {}
194
195 Locale::Locale( const LanguageCode& language_r, const CountryCode& country_r )
196 : _str( CodeMaps::combineLC( language_r, country_r ) )
197 {}
198
201
203 { return CodeMaps::instance().language( _str ); }
204
206 { return CodeMaps::instance().country( _str ); }
207
208 std::string Locale::name() const
209 { return CodeMaps::instance().name( _str ); }
210
212 { return CodeMaps::instance().fallback( _str ); }
213
214#ifndef ZYPPNG
215 Locale Locale::bestMatch( const LocaleSet & avLocales_r, Locale requested_r )
216 {
217 if ( ! avLocales_r.empty() )
218 {
219 if ( ! requested_r )
220 requested_r = ZConfig::instance().textLocale();
221 for ( ; requested_r; requested_r = requested_r.fallback() )
222 {
223 if ( avLocales_r.count( requested_r ) )
224 return requested_r;
225 }
226 }
227 return Locale();
228 }
229#endif
230
231} // namespace zypp
Country codes (iso3166-1-alpha-2).
Definition CountryCode.h:31
std::string name() const
Return the translated country name; if unknown the country code.
Access to the sat-pools string space.
Definition IdString.h:55
const char * c_str() const
Conversion to const char *.
Definition IdString.cc:51
static const IdString Null
No or Null string ( Id 0 ).
Definition IdString.h:86
static const IdString Empty
Empty string.
Definition IdString.h:89
Language codes (iso639_2/iso639_1).
static const LanguageCode enCode
Last resort "en".
'Language[_Country]' codes.
Definition Locale.h:51
Locale fallback() const
Return the fallback locale for this locale, if no fallback exists the empty Locale::noCode.
Definition Locale.cc:211
CountryCode country() const
The county part.
Definition Locale.cc:205
static const Locale enCode
Last resort "en".
Definition Locale.h:78
std::string name() const
Return the translated locale name.
Definition Locale.cc:208
LanguageCode language() const
The language part.
Definition Locale.cc:202
static const Locale noCode
Empty code.
Definition Locale.h:75
Locale()
Default Ctor: noCode.
Definition Locale.cc:180
IdString _str
Definition Locale.h:115
~Locale()
Dtor.
Definition Locale.cc:199
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:215
Locale textLocale() const
The locale for translated texts zypp uses.
Definition ZConfig.cc:843
static ZConfig & instance()
Singleton ctor.
Definition ZConfig.cc:756
const char * c_str() const
String related utilities and Regular expression matching.
Easy-to use interface to the ZYPP dependency resolver.
std::unordered_set< Locale > LocaleSet
Definition Locale.h:29
CountryCode _c
Definition Locale.cc:132
LanguageCode _l
Definition Locale.cc:131
LC(const LanguageCode &l_r, const CountryCode &c_r)
Definition Locale.cc:130
LC(const LanguageCode &l_r)
Definition Locale.cc:129
CodeMaps()
Ctor initializes the code maps.
Definition Locale.cc:137
static IdString combineLC(const LanguageCode &language_r, const CountryCode &country_r)
Return IdString from language/country codes.
Definition Locale.cc:47
LanguageCode language(IdString index_r)
Definition Locale.cc:75
const LC & getIndex(IdString index_r)
Return LC for index_r, creating it if necessary.
Definition Locale.cc:143
Locale fallback(IdString index_r)
Definition Locale.cc:94
std::string name(IdString index_r)
Definition Locale.cc:81
CountryCode country(IdString index_r)
Definition Locale.cc:78
static IdString withoutTrash(boost::string_ref code_r)
Definition Locale.cc:112
static IdString withoutTrash(const char *code_r)
Return IdString without trailing garbage.
Definition Locale.cc:43
CodeMap _codeMap
Definition Locale.cc:169
static IdString withoutTrash(IdString code_r)
Return IdString without trailing garbage.
Definition Locale.cc:30
static CodeMaps & instance()
The singleton.
Definition Locale.cc:69
static IdString withoutTrash(const std::string &code_r)
Return IdString without trailing garbage.
Definition Locale.cc:39
static boost::string_ref::size_type trashStart(IdString code_r)
Definition Locale.cc:123
static boost::string_ref::size_type trashStart(boost::string_ref code_r)
Definition Locale.cc:120
std::unordered_map< IdString, LC > CodeMap
Definition Locale.cc:134