libzypp 17.38.7
CountryCode.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12
13#include "CountryCode.h"
14
15#include <iostream>
16
20#include <zypp-core/base/Hash.h>
21
22using std::endl;
23
25namespace zypp
26{
28 namespace
29 {
30
32 struct CodeMaps // singleton
33 {
35 static CodeMaps & instance()
36 {
37 static CodeMaps _instance;
38 return _instance;
39 }
40
42 std::string name( IdString index_r )
43 {
44 Link link( getIndex( index_r ) );
45
46 std::string ret;
47 if ( link->second )
48 { ret = _(link->second); }
49 else
50 {
51 ret = _("Unknown country: ");
52 ret += "'";
53 ret += index_r.c_str();
54 ret += "'";
55 }
56 return ret;
57 }
58
59 private:
60 using CodeMap = std::unordered_map<std::string, const char *>;
61 using Link = CodeMap::const_iterator;
62
63 using IndexMap = std::unordered_map<IdString, Link>;
64
68 CodeMaps();
69
71 Link getIndex( IdString index_r )
72 {
73 auto it = _indexMap.find( index_r );
74 return( it != _indexMap.end()
75 ? it->second
76 : newIndex( index_r, index_r.asString() ) );
77 }
78
80 Link newIndex( IdString index_r, const std::string & code_r )
81 {
82 Link link = _codeMap.find( code_r );
83 if ( link != _codeMap.end() )
84 return (_indexMap[index_r] = link);
85
86 // not found: Remember a new code
87 CodeMap::value_type nval( code_r, nullptr );
88
89 if ( code_r.size() != 2 )
90 WAR << "Malformed CountryCode '" << code_r << "' (expect 2-letter)" << endl;
91
92 std::string ucode( str::toUpper( code_r ) );
93 if ( ucode != code_r )
94 {
95 WAR << "Malformed CountryCode '" << code_r << "' (not upper case)" << endl;
96 // but maybe we're lucky with the lower case code
97 // and find a language name.
98 link = _codeMap.find( ucode );
99 if ( link != _codeMap.end() )
100 {
101 nval.second = link->second;
102 }
103 }
104 MIL << "Remember CountryCode '" << code_r << "': '" << (nval.second?nval.second:"Unknown country") << "'" << endl;
105 return (_indexMap[index_r] = _codeMap.insert( nval ).first);
106 }
107
108 private:
109 CodeMap _codeMap;
110 IndexMap _indexMap;
111 };
112 } // namespace
114
116 // class CountryCode
118
120
123
125 : _str( str_r )
126 {}
127
128 CountryCode::CountryCode( const std::string & str_r )
129 : _str( str_r )
130 {}
131
132 CountryCode::CountryCode( const char * str_r )
133 : _str( str_r )
134 {}
135
138
139
140 std::string CountryCode::name() const
141 { return CodeMaps::instance().name( _str ); }
142
144 namespace
145 {
146
147 CodeMaps::CodeMaps()
148 {
149 // Defined CountryCode constants
150 _codeMap[""] = N_( "No Code" );
151
152 struct Init
153 {
154 const char *iso3166;
155 const char *name;
156 };
157
158 const Init init[] = {
159 {"AD", N_( "Andorra" ) }, // :AND:020:
160 {"AE", N_( "United Arab Emirates" ) }, // :ARE:784:
161 {"AF", N_( "Afghanistan" ) }, // :AFG:004:
162 {"AG", N_( "Antigua and Barbuda" ) }, // :ATG:028:
163 {"AI", N_( "Anguilla" ) }, // :AIA:660:
164 {"AL", N_( "Albania" ) }, // :ALB:008:
165 {"AM", N_( "Armenia" ) }, // :ARM:051:
166 {"AN", N_( "Netherlands Antilles" ) }, // :ANT:530:
167 {"AO", N_( "Angola" ) }, // :AGO:024:
168 {"AQ", N_( "Antarctica" ) }, // :ATA:010:
169 {"AR", N_( "Argentina" ) }, // :ARG:032:
170 {"AS", N_( "American Samoa" ) }, // :ASM:016:
171 {"AT", N_( "Austria" ) }, // :AUT:040:
172 {"AU", N_( "Australia" ) }, // :AUS:036:
173 {"AW", N_( "Aruba" ) }, // :ABW:533:
174 {"AX", N_( "Aland Islands" ) }, // :ALA:248:
175 {"AZ", N_( "Azerbaijan" ) }, // :AZE:031:
176 {"BA", N_( "Bosnia and Herzegovina" ) }, // :BIH:070:
177 {"BB", N_( "Barbados" ) }, // :BRB:052:
178 {"BD", N_( "Bangladesh" ) }, // :BGD:050:
179 {"BE", N_( "Belgium" ) }, // :BEL:056:
180 {"BF", N_( "Burkina Faso" ) }, // :BFA:854:
181 {"BG", N_( "Bulgaria" ) }, // :BGR:100:
182 {"BH", N_( "Bahrain" ) }, // :BHR:048:
183 {"BI", N_( "Burundi" ) }, // :BDI:108:
184 {"BJ", N_( "Benin" ) }, // :BEN:204:
185 {"BM", N_( "Bermuda" ) }, // :BMU:060:
186 {"BN", N_( "Brunei Darussalam" ) }, // :BRN:096:
187 {"BO", N_( "Bolivia" ) }, // :BOL:068:
188 {"BR", N_( "Brazil" ) }, // :BRA:076:
189 {"BS", N_( "Bahamas" ) }, // :BHS:044:
190 {"BT", N_( "Bhutan" ) }, // :BTN:064:
191 {"BV", N_( "Bouvet Island" ) }, // :BVT:074:
192 {"BW", N_( "Botswana" ) }, // :BWA:072:
193 {"BY", N_( "Belarus" ) }, // :BLR:112:
194 {"BZ", N_( "Belize" ) }, // :BLZ:084:
195 {"CA", N_( "Canada" ) }, // :CAN:124:
196 {"CC", N_( "Cocos (Keeling) Islands" ) }, // :CCK:166:
197 {"CD", N_( "Congo" ) }, // :COD:180:
198 {"CF", N_( "Central African Republic" ) }, // :CAF:140:
199 {"CG", N_( "Congo" ) }, // :COG:178:
200 {"CH", N_( "Switzerland" ) }, // :CHE:756:
201 {"CI", N_( "Cote D'Ivoire" ) }, // :CIV:384:
202 {"CK", N_( "Cook Islands" ) }, // :COK:184:
203 {"CL", N_( "Chile" ) }, // :CHL:152:
204 {"CM", N_( "Cameroon" ) }, // :CMR:120:
205 {"CN", N_( "China" ) }, // :CHN:156:
206 {"CO", N_( "Colombia" ) }, // :COL:170:
207 {"CR", N_( "Costa Rica" ) }, // :CRI:188:
208 {"CU", N_( "Cuba" ) }, // :CUB:192:
209 {"CV", N_( "Cape Verde" ) }, // :CPV:132:
210 {"CX", N_( "Christmas Island" ) }, // :CXR:162:
211 {"CY", N_( "Cyprus" ) }, // :CYP:196:
212 {"CZ", N_( "Czech Republic" ) }, // :CZE:203:
213 {"DE", N_( "Germany" ) }, // :DEU:276:
214 {"DJ", N_( "Djibouti" ) }, // :DJI:262:
215 {"DK", N_( "Denmark" ) }, // :DNK:208:
216 {"DM", N_( "Dominica" ) }, // :DMA:212:
217 {"DO", N_( "Dominican Republic" ) }, // :DOM:214:
218 {"DZ", N_( "Algeria" ) }, // :DZA:012:
219 {"EC", N_( "Ecuador" ) }, // :ECU:218:
220 {"EE", N_( "Estonia" ) }, // :EST:233:
221 {"EG", N_( "Egypt" ) }, // :EGY:818:
222 {"EH", N_( "Western Sahara" ) }, // :ESH:732:
223 {"ER", N_( "Eritrea" ) }, // :ERI:232:
224 {"ES", N_( "Spain" ) }, // :ESP:724:
225 {"ET", N_( "Ethiopia" ) }, // :ETH:231:
226 {"FI", N_( "Finland" ) }, // :FIN:246:
227 {"FJ", N_( "Fiji" ) }, // :FJI:242:
228 {"FK", N_( "Falkland Islands (Malvinas)" ) }, // :FLK:238:
229 {"FM", N_( "Federated States of Micronesia" ) }, // :FSM:583:
230 {"FO", N_( "Faroe Islands" ) }, // :FRO:234:
231 {"FR", N_( "France" ) }, // :FRA:250:
232 {"FX", N_( "Metropolitan France" ) }, // :FXX:249:
233 {"GA", N_( "Gabon" ) }, // :GAB:266:
234 {"GB", N_( "United Kingdom" ) }, // :GBR:826:
235 {"GD", N_( "Grenada" ) }, // :GRD:308:
236 {"GE", N_( "Georgia" ) }, // :GEO:268:
237 {"GF", N_( "French Guiana" ) }, // :GUF:254:
238 {"GG", N_( "Guernsey" ) },
239 {"GH", N_( "Ghana" ) }, // :GHA:288:
240 {"GI", N_( "Gibraltar" ) }, // :GIB:292:
241 {"GL", N_( "Greenland" ) }, // :GRL:304:
242 {"GM", N_( "Gambia" ) }, // :GMB:270:
243 {"GN", N_( "Guinea" ) }, // :GIN:324:
244 {"GP", N_( "Guadeloupe" ) }, // :GLP:312:
245 {"GQ", N_( "Equatorial Guinea" ) }, // :GNQ:226:
246 {"GR", N_( "Greece" ) }, // :GRC:300:
247 {"GS", N_( "South Georgia and the South Sandwich Islands" ) }, // :SGS:239:
248 {"GT", N_( "Guatemala" ) }, // :GTM:320:
249 {"GU", N_( "Guam" ) }, // :GUM:316:
250 {"GW", N_( "Guinea-Bissau" ) }, // :GNB:624:
251 {"GY", N_( "Guyana" ) }, // :GUY:328:
252 {"HK", N_( "Hong Kong" ) }, // :HKG:344:
253 {"HM", N_( "Heard Island and McDonald Islands" ) }, // :HMD:334:
254 {"HN", N_( "Honduras" ) }, // :HND:340:
255 {"HR", N_( "Croatia" ) }, // :HRV:191:
256 {"HT", N_( "Haiti" ) }, // :HTI:332:
257 {"HU", N_( "Hungary" ) }, // :HUN:348:
258 {"ID", N_( "Indonesia" ) }, // :IDN:360:
259 {"IE", N_( "Ireland" ) }, // :IRL:372:
260 {"IL", N_( "Israel" ) }, // :ISR:376:
261 {"IM", N_( "Isle of Man" ) },
262 {"IN", N_( "India" ) }, // :IND:356:
263 {"IO", N_( "British Indian Ocean Territory" ) }, // :IOT:086:
264 {"IQ", N_( "Iraq" ) }, // :IRQ:368:
265 {"IR", N_( "Iran" ) }, // :IRN:364:
266 {"IS", N_( "Iceland" ) }, // :ISL:352:
267 {"IT", N_( "Italy" ) }, // :ITA:380:
268 {"JE", N_( "Jersey" ) },
269 {"JM", N_( "Jamaica" ) }, // :JAM:388:
270 {"JO", N_( "Jordan" ) }, // :JOR:400:
271 {"JP", N_( "Japan" ) }, // :JPN:392:
272 {"KE", N_( "Kenya" ) }, // :KEN:404:
273 {"KG", N_( "Kyrgyzstan" ) }, // :KGZ:417:
274 {"KH", N_( "Cambodia" ) }, // :KHM:116:
275 {"KI", N_( "Kiribati" ) }, // :KIR:296:
276 {"KM", N_( "Comoros" ) }, // :COM:174:
277 {"KN", N_( "Saint Kitts and Nevis" ) }, // :KNA:659:
278 {"KP", N_( "North Korea" ) }, // :PRK:408:
279 {"KR", N_( "South Korea" ) }, // :KOR:410:
280 {"KW", N_( "Kuwait" ) }, // :KWT:414:
281 {"KY", N_( "Cayman Islands" ) }, // :CYM:136:
282 {"KZ", N_( "Kazakhstan" ) }, // :KAZ:398:
283 {"LA", N_( "Lao People's Democratic Republic" ) }, // :LAO:418:
284 {"LB", N_( "Lebanon" ) }, // :LBN:422:
285 {"LC", N_( "Saint Lucia" ) }, // :LCA:662:
286 {"LI", N_( "Liechtenstein" ) }, // :LIE:438:
287 {"LK", N_( "Sri Lanka" ) }, // :LKA:144:
288 {"LR", N_( "Liberia" ) }, // :LBR:430:
289 {"LS", N_( "Lesotho" ) }, // :LSO:426:
290 {"LT", N_( "Lithuania" ) }, // :LTU:440:
291 {"LU", N_( "Luxembourg" ) }, // :LUX:442:
292 {"LV", N_( "Latvia" ) }, // :LVA:428:
293 {"LY", N_( "Libya" ) }, // :LBY:434:
294 {"MA", N_( "Morocco" ) }, // :MAR:504:
295 {"MC", N_( "Monaco" ) }, // :MCO:492:
296 {"MD", N_( "Moldova" ) }, // :MDA:498:
297 {"ME", N_( "Montenegro" ) },
298 {"MF", N_( "Saint Martin" ) },
299 {"MG", N_( "Madagascar" ) }, // :MDG:450:
300 {"MH", N_( "Marshall Islands" ) }, // :MHL:584:
301 {"MK", N_( "Macedonia" ) }, // :MKD:807:
302 {"ML", N_( "Mali" ) }, // :MLI:466:
303 {"MM", N_( "Myanmar" ) }, // :MMR:104:
304 {"MN", N_( "Mongolia" ) }, // :MNG:496:
305 {"MO", N_( "Macao" ) }, // :MAC:446:
306 {"MP", N_( "Northern Mariana Islands" ) }, // :MNP:580:
307 {"MQ", N_( "Martinique" ) }, // :MTQ:474:
308 {"MR", N_( "Mauritania" ) }, // :MRT:478:
309 {"MS", N_( "Montserrat" ) }, // :MSR:500:
310 {"MT", N_( "Malta" ) }, // :MLT:470:
311 {"MU", N_( "Mauritius" ) }, // :MUS:480:
312 {"MV", N_( "Maldives" ) }, // :MDV:462:
313 {"MW", N_( "Malawi" ) }, // :MWI:454:
314 {"MX", N_( "Mexico" ) }, // :MEX:484:
315 {"MY", N_( "Malaysia" ) }, // :MYS:458:
316 {"MZ", N_( "Mozambique" ) }, // :MOZ:508:
317 {"NA", N_( "Namibia" ) }, // :NAM:516:
318 {"NC", N_( "New Caledonia" ) }, // :NCL:540:
319 {"NE", N_( "Niger" ) }, // :NER:562:
320 {"NF", N_( "Norfolk Island" ) }, // :NFK:574:
321 {"NG", N_( "Nigeria" ) }, // :NGA:566:
322 {"NI", N_( "Nicaragua" ) }, // :NIC:558:
323 {"NL", N_( "Netherlands" ) }, // :NLD:528:
324 {"NO", N_( "Norway" ) }, // :NOR:578:
325 {"NP", N_( "Nepal" ) }, // :NPL:524:
326 {"NR", N_( "Nauru" ) }, // :NRU:520:
327 {"NU", N_( "Niue" ) }, // :NIU:570:
328 {"NZ", N_( "New Zealand" ) }, // :NZL:554:
329 {"OM", N_( "Oman" ) }, // :OMN:512:
330 {"PA", N_( "Panama" ) }, // :PAN:591:
331 {"PE", N_( "Peru" ) }, // :PER:604:
332 {"PF", N_( "French Polynesia" ) }, // :PYF:258:
333 {"PG", N_( "Papua New Guinea" ) }, // :PNG:598:
334 {"PH", N_( "Philippines" ) }, // :PHL:608:
335 {"PK", N_( "Pakistan" ) }, // :PAK:586:
336 {"PL", N_( "Poland" ) }, // :POL:616:
337 {"PM", N_( "Saint Pierre and Miquelon" ) }, // :SPM:666:
338 {"PN", N_( "Pitcairn" ) }, // :PCN:612:
339 {"PR", N_( "Puerto Rico" ) }, // :PRI:630:
340 {"PS", N_( "Palestinian Territory" ) }, // :PSE:275:
341 {"PT", N_( "Portugal" ) }, // :PRT:620:
342 {"PW", N_( "Palau" ) }, // :PLW:585:
343 {"PY", N_( "Paraguay" ) }, // :PRY:600:
344 {"QA", N_( "Qatar" ) }, // :QAT:634:
345 {"RE", N_( "Reunion" ) }, // :REU:638:
346 {"RO", N_( "Romania" ) }, // :ROU:642:
347 {"RS", N_( "Serbia" ) },
348 {"RU", N_( "Russian Federation" ) }, // :RUS:643:
349 {"RW", N_( "Rwanda" ) }, // :RWA:646:
350 {"SA", N_( "Saudi Arabia" ) }, // :SAU:682:
351 {"SB", N_( "Solomon Islands" ) }, // :SLB:090:
352 {"SC", N_( "Seychelles" ) }, // :SYC:690:
353 {"SD", N_( "Sudan" ) }, // :SDN:736:
354 {"SE", N_( "Sweden" ) }, // :SWE:752:
355 {"SG", N_( "Singapore" ) }, // :SGP:702:
356 {"SH", N_( "Saint Helena" ) }, // :SHN:654:
357 {"SI", N_( "Slovenia" ) }, // :SVN:705:
358 {"SJ", N_( "Svalbard and Jan Mayen" ) }, // :SJM:744:
359 {"SK", N_( "Slovakia" ) }, // :SVK:703:
360 {"SL", N_( "Sierra Leone" ) }, // :SLE:694:
361 {"SM", N_( "San Marino" ) }, // :SMR:674:
362 {"SN", N_( "Senegal" ) }, // :SEN:686:
363 {"SO", N_( "Somalia" ) }, // :SOM:706:
364 {"SR", N_( "Suriname" ) }, // :SUR:740:
365 {"ST", N_( "Sao Tome and Principe" ) }, // :STP:678:
366 {"SV", N_( "El Salvador" ) }, // :SLV:222:
367 {"SY", N_( "Syria" ) }, // :SYR:760:
368 {"SZ", N_( "Swaziland" ) }, // :SWZ:748:
369 {"TC", N_( "Turks and Caicos Islands" ) }, // :TCA:796:
370 {"TD", N_( "Chad" ) }, // :TCD:148:
371 {"TF", N_( "French Southern Territories" ) }, // :ATF:260:
372 {"TG", N_( "Togo" ) }, // :TGO:768:
373 {"TH", N_( "Thailand" ) }, // :THA:764:
374 {"TJ", N_( "Tajikistan" ) }, // :TJK:762:
375 {"TK", N_( "Tokelau" ) }, // :TKL:772:
376 {"TM", N_( "Turkmenistan" ) }, // :TKM:795:
377 {"TN", N_( "Tunisia" ) }, // :TUN:788:
378 {"TO", N_( "Tonga" ) }, // :TON:776:
379 {"TL", N_( "East Timor" ) }, // :TLS:626:
380 {"TR", N_( "Turkey" ) }, // :TUR:792:
381 {"TT", N_( "Trinidad and Tobago" ) }, // :TTO:780:
382 {"TV", N_( "Tuvalu" ) }, // :TUV:798:
383 {"TW", N_( "Taiwan" ) }, // :TWN:158:
384 {"TZ", N_( "Tanzania" ) }, // :TZA:834:
385 {"UA", N_( "Ukraine" ) }, // :UKR:804:
386 {"UG", N_( "Uganda" ) }, // :UGA:800:
387 {"UM", N_( "United States Minor Outlying Islands" ) },// :UMI:581:
388 {"US", N_( "United States" ) }, // :USA:840:
389 {"UY", N_( "Uruguay" ) }, // :URY:858:
390 {"UZ", N_( "Uzbekistan" ) }, // :UZB:860:
391 {"VA", N_( "Holy See (Vatican City State)" ) }, // :VAT:336:
392 {"VC", N_( "Saint Vincent and the Grenadines" ) }, // :VCT:670:
393 {"VE", N_( "Venezuela" ) }, // :VEN:862:
394 {"VG", N_( "British Virgin Islands" ) }, // :VGB:092:
395 {"VI", N_( "Virgin Islands, U.S." ) }, // :VIR:850:
396 {"VN", N_( "Vietnam" ) }, // :VNM:704:
397 {"VU", N_( "Vanuatu" ) }, // :VUT:548:
398 {"WF", N_( "Wallis and Futuna" ) }, // :WLF:876:
399 {"WS", N_( "Samoa" ) }, // :WSM:882:
400 {"YE", N_( "Yemen" ) }, // :YEM:887:
401 {"YT", N_( "Mayotte" ) }, // :MYT:175:
402 {"ZA", N_( "South Africa" ) }, // :ZAF:710:
403 {"ZM", N_( "Zambia" ) }, // :ZMB:894:
404 {"ZW", N_( "Zimbabwe" ) }, // :ZWE:716:
405
406 { NULL, NULL }
407 };
408
409 for (const Init * i = init; i->iso3166 != NULL; ++i)
410 _codeMap[i->iso3166] = i->name;
411 }
412
414 } // namespace
416
418} // namespace zypp
#define N_(MSG)
Definition Gettext.h:36
#define _(MSG)
Definition Gettext.h:39
#define MIL
Definition Logger.h:103
#define WAR
Definition Logger.h:104
Country codes (iso3166-1-alpha-2).
Definition CountryCode.h:31
std::string name() const
Return the translated country name; if unknown the country code.
static const CountryCode noCode
Empty code.
Definition CountryCode.h:53
CountryCode()
Default Ctor: noCode.
Access to the sat-pools string space.
Definition IdString.h:55
std::string toUpper(const std::string &s)
Return uppercase version of s.
Definition String.cc:203
Easy-to use interface to the ZYPP dependency resolver.
CodeMaps()
Ctor initializes the code maps.
Definition Locale.cc:137
const LC & getIndex(IdString index_r)
Return LC for index_r, creating it if necessary.
Definition Locale.cc:143
std::string name(IdString index_r)
Definition Locale.cc:81
static CodeMaps & instance()
The singleton.
Definition Locale.cc:69