libzypp: zypp/Locale.cc Source File

libzypp 17.38.11

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 {
30 static IdString withoutTrash( IdString code_r )
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
75 LanguageCode language( IdString index_r )
76 { return getIndex( index_r )._l; }
77
78 CountryCode country( IdString index_r )
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
94 Locale fallback( IdString index_r )
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 ) {}
131 LanguageCode _l;
132 CountryCode _c;
133 };
134 using CodeMap = std::unordered_map<IdString, LC>;
135
137 CodeMaps()
138 : _codeMap( { { IdString::Null, LC( LanguageCode(IdString::Null), CountryCode(IdString::Null) ) }
139 , { IdString::Empty, LC( LanguageCode(IdString::Empty), CountryCode(IdString::Empty) ) } } )
140 {}
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:
169 CodeMap _codeMap;
170 };
171
173 // class Locale
175
176 const Locale Locale::noCode;
177 const LanguageCode LanguageCode::enCode("en"); // from in LanguageCode.cc as Locale::enCode depends on it
178 const Locale Locale::enCode( LanguageCode::enCode );
179
180 Locale::Locale()
181 {}
182
183 Locale::Locale( IdString str_r )
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
199 Locale::~Locale()
200 {}
201
202 LanguageCode Locale::language() const
203 { return CodeMaps::instance().language( _str ); }
204
205 CountryCode Locale::country() const
206 { return CodeMaps::instance().country( _str ); }
207
208 std::string Locale::name() const
209 { return CodeMaps::instance().name( _str ); }
210
211 Locale Locale::fallback() const
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
Locale.h
ZConfig.h
zypp::CountryCode
Country codes (iso3166-1-alpha-2).
Definition CountryCode.h:31
zypp::CountryCode::name
std::string name() const
Return the translated country name; if unknown the country code.
Definition CountryCode.cc:140
zypp::IdString
Access to the sat-pools string space.
Definition IdString.h:55
zypp::IdString::c_str
const char * c_str() const
Conversion to const char *.
Definition IdString.cc:51
zypp::IdString::Null
static const IdString Null
No or Null string ( Id 0 ).
Definition IdString.h:86
zypp::IdString::Empty
static const IdString Empty
Empty string.
Definition IdString.h:89
zypp::LanguageCode
Language codes (iso639_2/iso639_1).
Definition LanguageCode.h:31
zypp::LanguageCode::enCode
static const LanguageCode enCode
Last resort "en".
Definition LanguageCode.h:54
zypp::Locale
'Language[_Country]' codes.
Definition Locale.h:51
zypp::Locale::fallback
Locale fallback() const
Return the fallback locale for this locale, if no fallback exists the empty Locale::noCode.
Definition Locale.cc:211
zypp::Locale::country
CountryCode country() const
The county part.
Definition Locale.cc:205
zypp::Locale::enCode
static const Locale enCode
Last resort "en".
Definition Locale.h:78
zypp::Locale::name
std::string name() const
Return the translated locale name.
Definition Locale.cc:208
zypp::Locale::language
LanguageCode language() const
The language part.
Definition Locale.cc:202
zypp::Locale::noCode
static const Locale noCode
Empty code.
Definition Locale.h:75
zypp::Locale::Locale
Locale()
Default Ctor: noCode.
Definition Locale.cc:180
zypp::Locale::_str
IdString _str
Definition Locale.h:115
zypp::Locale::~Locale
~Locale()
Dtor.
Definition Locale.cc:199
zypp::Locale::bestMatch
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
zypp::ZConfig::textLocale
Locale textLocale() const
The locale for translated texts zypp uses.
Definition ZConfig.cc:875
zypp::ZConfig::instance
static ZConfig & instance()
Singleton ctor.
Definition ZConfig.cc:788
zyppng::IdStringType::c_str
const char * c_str() const
Definition IdStringType.h:115
str
String related utilities and Regular expression matching.
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::LocaleSet
std::unordered_set< Locale > LocaleSet
Definition Locale.h:29
zypp::CodeMaps::LC::_c
CountryCode _c
Definition Locale.cc:132
zypp::CodeMaps::LC::LC
LC()
Definition Locale.cc:128
zypp::CodeMaps::LC::_l
LanguageCode _l
Definition Locale.cc:131
zypp::CodeMaps::LC::LC
LC(const LanguageCode &l_r, const CountryCode &c_r)
Definition Locale.cc:130
zypp::CodeMaps::LC::LC
LC(const LanguageCode &l_r)
Definition Locale.cc:129
zypp::CodeMaps::CodeMaps
CodeMaps()
Ctor initializes the code maps.
Definition Locale.cc:137
zypp::CodeMaps::combineLC
static IdString combineLC(const LanguageCode &language_r, const CountryCode &country_r)
Return IdString from language/country codes.
Definition Locale.cc:47
zypp::CodeMaps::language
LanguageCode language(IdString index_r)
Definition Locale.cc:75
zypp::CodeMaps::getIndex
const LC & getIndex(IdString index_r)
Return LC for index_r, creating it if necessary.
Definition Locale.cc:143
zypp::CodeMaps::fallback
Locale fallback(IdString index_r)
Definition Locale.cc:94
zypp::CodeMaps::name
std::string name(IdString index_r)
Definition Locale.cc:81
zypp::CodeMaps::country
CountryCode country(IdString index_r)
Definition Locale.cc:78
zypp::CodeMaps::withoutTrash
static IdString withoutTrash(boost::string_ref code_r)
Definition Locale.cc:112
zypp::CodeMaps::withoutTrash
static IdString withoutTrash(const char *code_r)
Return IdString without trailing garbage.
Definition Locale.cc:43
zypp::CodeMaps::_codeMap
CodeMap _codeMap
Definition Locale.cc:169
zypp::CodeMaps::withoutTrash
static IdString withoutTrash(IdString code_r)
Return IdString without trailing garbage.
Definition Locale.cc:30
zypp::CodeMaps::instance
static CodeMaps & instance()
The singleton.
Definition Locale.cc:69
zypp::CodeMaps::withoutTrash
static IdString withoutTrash(const std::string &code_r)
Return IdString without trailing garbage.
Definition Locale.cc:39
zypp::CodeMaps::trashStart
static boost::string_ref::size_type trashStart(IdString code_r)
Definition Locale.cc:123
zypp::CodeMaps::trashStart
static boost::string_ref::size_type trashStart(boost::string_ref code_r)
Definition Locale.cc:120
zypp::CodeMaps::CodeMap
std::unordered_map< IdString, LC > CodeMap
Definition Locale.cc:134