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