Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | # |
2 | # Locale::Country - ISO codes for country identification (ISO 3166) | |
3 | # | |
4 | # $Id: Country.pm,v 2.6 2002/07/10 16:33:27 neilb Exp $ | |
5 | # | |
6 | ||
7 | package Locale::Country; | |
8 | use strict; | |
9 | require 5.002; | |
10 | ||
11 | require Exporter; | |
12 | use Carp; | |
13 | use Locale::Constants; | |
14 | ||
15 | ||
16 | #----------------------------------------------------------------------- | |
17 | # Public Global Variables | |
18 | #----------------------------------------------------------------------- | |
19 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); | |
20 | $VERSION = sprintf("%d.%02d", q$Revision: 2.6 $ =~ /(\d+)\.(\d+)/); | |
21 | @ISA = qw(Exporter); | |
22 | @EXPORT = qw(code2country country2code | |
23 | all_country_codes all_country_names | |
24 | country_code2code | |
25 | LOCALE_CODE_ALPHA_2 LOCALE_CODE_ALPHA_3 LOCALE_CODE_NUMERIC); | |
26 | ||
27 | #----------------------------------------------------------------------- | |
28 | # Private Global Variables | |
29 | #----------------------------------------------------------------------- | |
30 | my $CODES = []; | |
31 | my $COUNTRIES = []; | |
32 | ||
33 | ||
34 | #======================================================================= | |
35 | # | |
36 | # code2country ( CODE [, CODESET ] ) | |
37 | # | |
38 | #======================================================================= | |
39 | sub code2country | |
40 | { | |
41 | my $code = shift; | |
42 | my $codeset = @_ > 0 ? shift : LOCALE_CODE_DEFAULT; | |
43 | ||
44 | ||
45 | return undef unless defined $code; | |
46 | ||
47 | #------------------------------------------------------------------- | |
48 | # Make sure the code is in the right form before we use it | |
49 | # to look up the corresponding country. | |
50 | # We have to sprintf because the codes are given as 3-digits, | |
51 | # with leading 0's. Eg 052 for Barbados. | |
52 | #------------------------------------------------------------------- | |
53 | if ($codeset == LOCALE_CODE_NUMERIC) | |
54 | { | |
55 | return undef if ($code =~ /\D/); | |
56 | $code = sprintf("%.3d", $code); | |
57 | } | |
58 | else | |
59 | { | |
60 | $code = lc($code); | |
61 | } | |
62 | ||
63 | if (exists $CODES->[$codeset]->{$code}) | |
64 | { | |
65 | return $CODES->[$codeset]->{$code}; | |
66 | } | |
67 | else | |
68 | { | |
69 | #--------------------------------------------------------------- | |
70 | # no such country code! | |
71 | #--------------------------------------------------------------- | |
72 | return undef; | |
73 | } | |
74 | } | |
75 | ||
76 | ||
77 | #======================================================================= | |
78 | # | |
79 | # country2code ( NAME [, CODESET ] ) | |
80 | # | |
81 | #======================================================================= | |
82 | sub country2code | |
83 | { | |
84 | my $country = shift; | |
85 | my $codeset = @_ > 0 ? shift : LOCALE_CODE_DEFAULT; | |
86 | ||
87 | ||
88 | return undef unless defined $country; | |
89 | $country = lc($country); | |
90 | if (exists $COUNTRIES->[$codeset]->{$country}) | |
91 | { | |
92 | return $COUNTRIES->[$codeset]->{$country}; | |
93 | } | |
94 | else | |
95 | { | |
96 | #--------------------------------------------------------------- | |
97 | # no such country! | |
98 | #--------------------------------------------------------------- | |
99 | return undef; | |
100 | } | |
101 | } | |
102 | ||
103 | ||
104 | #======================================================================= | |
105 | # | |
106 | # country_code2code ( NAME [, CODESET ] ) | |
107 | # | |
108 | #======================================================================= | |
109 | sub country_code2code | |
110 | { | |
111 | (@_ == 3) or croak "country_code2code() takes 3 arguments!"; | |
112 | ||
113 | my $code = shift; | |
114 | my $inset = shift; | |
115 | my $outset = shift; | |
116 | my $outcode; | |
117 | my $country; | |
118 | ||
119 | ||
120 | return undef if $inset == $outset; | |
121 | $country = code2country($code, $inset); | |
122 | return undef if not defined $country; | |
123 | $outcode = country2code($country, $outset); | |
124 | return $outcode; | |
125 | } | |
126 | ||
127 | ||
128 | #======================================================================= | |
129 | # | |
130 | # all_country_codes ( [ CODESET ] ) | |
131 | # | |
132 | #======================================================================= | |
133 | sub all_country_codes | |
134 | { | |
135 | my $codeset = @_ > 0 ? shift : LOCALE_CODE_DEFAULT; | |
136 | ||
137 | return keys %{ $CODES->[$codeset] }; | |
138 | } | |
139 | ||
140 | ||
141 | #======================================================================= | |
142 | # | |
143 | # all_country_names ( [ CODESET ] ) | |
144 | # | |
145 | #======================================================================= | |
146 | sub all_country_names | |
147 | { | |
148 | my $codeset = @_ > 0 ? shift : LOCALE_CODE_DEFAULT; | |
149 | ||
150 | return values %{ $CODES->[$codeset] }; | |
151 | } | |
152 | ||
153 | ||
154 | #======================================================================= | |
155 | # | |
156 | # alias_code ( ALIAS => CODE [ , CODESET ] ) | |
157 | # | |
158 | # Add an alias for an existing code. If the CODESET isn't specified, | |
159 | # then we use the default (currently the alpha-2 codeset). | |
160 | # | |
161 | # Locale::Country::alias_code('uk' => 'gb'); | |
162 | # | |
163 | #======================================================================= | |
164 | sub alias_code | |
165 | { | |
166 | my $alias = shift; | |
167 | my $real = shift; | |
168 | my $codeset = @_ > 0 ? shift : LOCALE_CODE_DEFAULT; | |
169 | ||
170 | my $country; | |
171 | ||
172 | ||
173 | if (not exists $CODES->[$codeset]->{$real}) | |
174 | { | |
175 | carp "attempt to alias \"$alias\" to unknown country code \"$real\"\n"; | |
176 | return undef; | |
177 | } | |
178 | $country = $CODES->[$codeset]->{$real}; | |
179 | $CODES->[$codeset]->{$alias} = $country; | |
180 | $COUNTRIES->[$codeset]->{"\L$country"} = $alias; | |
181 | ||
182 | return $alias; | |
183 | } | |
184 | ||
185 | # old name of function for backwards compatibility | |
186 | *_alias_code = *alias_code; | |
187 | ||
188 | ||
189 | #======================================================================= | |
190 | # | |
191 | # rename_country | |
192 | # | |
193 | # change the official name for a country, eg: | |
194 | # gb => 'Great Britain' | |
195 | # rather than the standard 'United Kingdom'. The original is retained | |
196 | # as an alias, but the new name will be returned if you lookup the | |
197 | # name from code. | |
198 | # | |
199 | #======================================================================= | |
200 | sub rename_country | |
201 | { | |
202 | my $code = shift; | |
203 | my $new_name = shift; | |
204 | my $codeset = @_ > 0 ? shift : _code2codeset($code); | |
205 | my $country; | |
206 | my $c; | |
207 | ||
208 | ||
209 | if (not defined $codeset) | |
210 | { | |
211 | carp "rename_country(): unknown country code \"$code\"\n"; | |
212 | return 0; | |
213 | } | |
214 | ||
215 | $country = $CODES->[$codeset]->{$code}; | |
216 | ||
217 | foreach my $cset (LOCALE_CODE_ALPHA_2, | |
218 | LOCALE_CODE_ALPHA_3, | |
219 | LOCALE_CODE_NUMERIC) | |
220 | { | |
221 | if ($cset == $codeset) | |
222 | { | |
223 | $c = $code; | |
224 | } | |
225 | else | |
226 | { | |
227 | $c = country_code2code($code, $codeset, $cset); | |
228 | } | |
229 | ||
230 | $CODES->[$cset]->{$c} = $new_name; | |
231 | $COUNTRIES->[$cset]->{"\L$new_name"} = $c; | |
232 | } | |
233 | ||
234 | return 1; | |
235 | } | |
236 | ||
237 | ||
238 | #======================================================================= | |
239 | # | |
240 | # _code2codeset | |
241 | # | |
242 | # given a country code in an unknown codeset, return the codeset | |
243 | # it is from, or undef. | |
244 | # | |
245 | #======================================================================= | |
246 | sub _code2codeset | |
247 | { | |
248 | my $code = shift; | |
249 | ||
250 | ||
251 | foreach my $codeset (LOCALE_CODE_ALPHA_2, LOCALE_CODE_ALPHA_3, | |
252 | LOCALE_CODE_NUMERIC) | |
253 | { | |
254 | return $codeset if (exists $CODES->[$codeset]->{$code}) | |
255 | } | |
256 | ||
257 | return undef; | |
258 | } | |
259 | ||
260 | ||
261 | #======================================================================= | |
262 | # | |
263 | # initialisation code - stuff the DATA into the ALPHA2 hash | |
264 | # | |
265 | #======================================================================= | |
266 | { | |
267 | my ($alpha2, $alpha3, $numeric); | |
268 | my ($country, @countries); | |
269 | ||
270 | ||
271 | while (<DATA>) | |
272 | { | |
273 | next unless /\S/; | |
274 | chop; | |
275 | ($alpha2, $alpha3, $numeric, @countries) = split(/:/, $_); | |
276 | ||
277 | $CODES->[LOCALE_CODE_ALPHA_2]->{$alpha2} = $countries[0]; | |
278 | foreach $country (@countries) | |
279 | { | |
280 | $COUNTRIES->[LOCALE_CODE_ALPHA_2]->{"\L$country"} = $alpha2; | |
281 | } | |
282 | ||
283 | if ($alpha3) | |
284 | { | |
285 | $CODES->[LOCALE_CODE_ALPHA_3]->{$alpha3} = $countries[0]; | |
286 | foreach $country (@countries) | |
287 | { | |
288 | $COUNTRIES->[LOCALE_CODE_ALPHA_3]->{"\L$country"} = $alpha3; | |
289 | } | |
290 | } | |
291 | ||
292 | if ($numeric) | |
293 | { | |
294 | $CODES->[LOCALE_CODE_NUMERIC]->{$numeric} = $countries[0]; | |
295 | foreach $country (@countries) | |
296 | { | |
297 | $COUNTRIES->[LOCALE_CODE_NUMERIC]->{"\L$country"} = $numeric; | |
298 | } | |
299 | } | |
300 | ||
301 | } | |
302 | ||
303 | close(DATA); | |
304 | } | |
305 | ||
306 | 1; | |
307 | ||
308 | __DATA__ | |
309 | ad:and:020:Andorra | |
310 | ae:are:784:United Arab Emirates | |
311 | af:afg:004:Afghanistan | |
312 | ag:atg:028:Antigua and Barbuda | |
313 | ai:aia:660:Anguilla | |
314 | al:alb:008:Albania | |
315 | am:arm:051:Armenia | |
316 | an:ant:530:Netherlands Antilles | |
317 | ao:ago:024:Angola | |
318 | aq:ata:010:Antarctica | |
319 | ar:arg:032:Argentina | |
320 | as:asm:016:American Samoa | |
321 | at:aut:040:Austria | |
322 | au:aus:036:Australia | |
323 | aw:abw:533:Aruba | |
324 | az:aze:031:Azerbaijan | |
325 | ba:bih:070:Bosnia and Herzegovina | |
326 | bb:brb:052:Barbados | |
327 | bd:bgd:050:Bangladesh | |
328 | be:bel:056:Belgium | |
329 | bf:bfa:854:Burkina Faso | |
330 | bg:bgr:100:Bulgaria | |
331 | bh:bhr:048:Bahrain | |
332 | bi:bdi:108:Burundi | |
333 | bj:ben:204:Benin | |
334 | bm:bmu:060:Bermuda | |
335 | bn:brn:096:Brunei Darussalam | |
336 | bo:bol:068:Bolivia | |
337 | br:bra:076:Brazil | |
338 | bs:bhs:044:Bahamas | |
339 | bt:btn:064:Bhutan | |
340 | bv:bvt:074:Bouvet Island | |
341 | bw:bwa:072:Botswana | |
342 | by:blr:112:Belarus | |
343 | bz:blz:084:Belize | |
344 | ca:can:124:Canada | |
345 | cc:cck:166:Cocos (Keeling) Islands | |
346 | cf:caf:140:Central African Republic | |
347 | cg:cog:178:Congo:Congo, Republic of the | |
348 | ch:che:756:Switzerland | |
349 | ci:civ:384:Cote D'Ivoire | |
350 | ck:cok:184:Cook Islands | |
351 | cl:chl:152:Chile | |
352 | cm:cmr:120:Cameroon | |
353 | cn:chn:156:China | |
354 | co:col:170:Colombia | |
355 | cr:cri:188:Costa Rica | |
356 | cu:cub:192:Cuba | |
357 | cv:cpv:132:Cape Verde | |
358 | cx:cxr:162:Christmas Island | |
359 | cy:cyp:196:Cyprus | |
360 | cz:cze:203:Czech Republic | |
361 | de:deu:276:Germany | |
362 | dj:dji:262:Djibouti | |
363 | dk:dnk:208:Denmark | |
364 | dm:dma:212:Dominica | |
365 | do:dom:214:Dominican Republic | |
366 | dz:dza:012:Algeria | |
367 | ec:ecu:218:Ecuador | |
368 | ee:est:233:Estonia | |
369 | eg:egy:818:Egypt | |
370 | eh:esh:732:Western Sahara | |
371 | er:eri:232:Eritrea | |
372 | es:esp:724:Spain | |
373 | et:eth:231:Ethiopia | |
374 | fi:fin:246:Finland | |
375 | fj:fji:242:Fiji | |
376 | fk:flk:238:Falkland Islands (Malvinas):Falkland Islands (Islas Malvinas) | |
377 | fm:fsm:583:Micronesia, Federated States of | |
378 | fo:fro:234:Faroe Islands | |
379 | fr:fra:250:France | |
380 | fx:fxx:249:France, Metropolitan | |
381 | ga:gab:266:Gabon | |
382 | gb:gbr:826:United Kingdom:Great Britain | |
383 | gd:grd:308:Grenada | |
384 | ge:geo:268:Georgia | |
385 | gf:guf:254:French Guiana | |
386 | gh:gha:288:Ghana | |
387 | gi:gib:292:Gibraltar | |
388 | gl:grl:304:Greenland | |
389 | gm:gmb:270:Gambia | |
390 | gn:gin:324:Guinea | |
391 | gp:glp:312:Guadeloupe | |
392 | gq:gnq:226:Equatorial Guinea | |
393 | gr:grc:300:Greece | |
394 | gs:sgs:239:South Georgia and the South Sandwich Islands | |
395 | gt:gtm:320:Guatemala | |
396 | gu:gum:316:Guam | |
397 | gw:gnb:624:Guinea-Bissau | |
398 | gy:guy:328:Guyana | |
399 | hk:hkg:344:Hong Kong | |
400 | hm:hmd:334:Heard Island and McDonald Islands | |
401 | hn:hnd:340:Honduras | |
402 | hr:hrv:191:Croatia | |
403 | ht:hti:332:Haiti | |
404 | hu:hun:348:Hungary | |
405 | id:idn:360:Indonesia | |
406 | ie:irl:372:Ireland | |
407 | il:isr:376:Israel | |
408 | in:ind:356:India | |
409 | io:iot:086:British Indian Ocean Territory | |
410 | iq:irq:368:Iraq | |
411 | ir:irn:364:Iran, Islamic Republic of:Iran | |
412 | is:isl:352:Iceland | |
413 | it:ita:380:Italy | |
414 | jm:jam:388:Jamaica | |
415 | jo:jor:400:Jordan | |
416 | jp:jpn:392:Japan | |
417 | ke:ken:404:Kenya | |
418 | kg:kgz:417:Kyrgyzstan | |
419 | kh:khm:116:Cambodia | |
420 | ki:kir:296:Kiribati | |
421 | km:com:174:Comoros | |
422 | kn:kna:659:Saint Kitts and Nevis | |
423 | kp:prk:408:Korea, Democratic People's Republic of:Korea, North:North Korea | |
424 | kr:kor:410:Korea, Republic of:Korea, South:South Korea | |
425 | kw:kwt:414:Kuwait | |
426 | ky:cym:136:Cayman Islands | |
427 | kz:kaz:398:Kazakhstan:Kazakstan | |
428 | la:lao:418:Lao People's Democratic Republic | |
429 | lb:lbn:422:Lebanon | |
430 | lc:lca:662:Saint Lucia | |
431 | li:lie:438:Liechtenstein | |
432 | lk:lka:144:Sri Lanka | |
433 | lr:lbr:430:Liberia | |
434 | ls:lso:426:Lesotho | |
435 | lt:ltu:440:Lithuania | |
436 | lu:lux:442:Luxembourg | |
437 | lv:lva:428:Latvia | |
438 | ly:lby:434:Libyan Arab Jamahiriya:Libya | |
439 | ma:mar:504:Morocco | |
440 | mc:mco:492:Monaco | |
441 | md:mda:498:Moldova, Republic of:Moldova | |
442 | mg:mdg:450:Madagascar | |
443 | mh:mhl:584:Marshall Islands | |
444 | mk:mkd:807:Macedonia, the Former Yugoslav Republic of:Macedonia, Former Yugoslav Republic of:Macedonia | |
445 | ml:mli:466:Mali | |
446 | mm:mmr:104:Myanmar | |
447 | mn:mng:496:Mongolia | |
448 | mo:mac:446:Macao:Macau | |
449 | mp:mnp:580:Northern Mariana Islands | |
450 | mq:mtq:474:Martinique | |
451 | mr:mrt:478:Mauritania | |
452 | ms:msr:500:Montserrat | |
453 | mt:mlt:470:Malta | |
454 | mu:mus:480:Mauritius | |
455 | mv:mdv:462:Maldives | |
456 | mw:mwi:454:Malawi | |
457 | mx:mex:484:Mexico | |
458 | my:mys:458:Malaysia | |
459 | mz:moz:508:Mozambique | |
460 | na:nam:516:Namibia | |
461 | nc:ncl:540:New Caledonia | |
462 | ne:ner:562:Niger | |
463 | nf:nfk:574:Norfolk Island | |
464 | ng:nga:566:Nigeria | |
465 | ni:nic:558:Nicaragua | |
466 | nl:nld:528:Netherlands | |
467 | no:nor:578:Norway | |
468 | np:npl:524:Nepal | |
469 | nr:nru:520:Nauru | |
470 | nu:niu:570:Niue | |
471 | nz:nzl:554:New Zealand | |
472 | om:omn:512:Oman | |
473 | pa:pan:591:Panama | |
474 | pe:per:604:Peru | |
475 | pf:pyf:258:French Polynesia | |
476 | pg:png:598:Papua New Guinea | |
477 | ph:phl:608:Philippines | |
478 | pk:pak:586:Pakistan | |
479 | pl:pol:616:Poland | |
480 | pm:spm:666:Saint Pierre and Miquelon | |
481 | pn:pcn:612:Pitcairn:Pitcairn Island | |
482 | pr:pri:630:Puerto Rico | |
483 | ps:pse:275:Palestinian Territory, Occupied | |
484 | pt:prt:620:Portugal | |
485 | pw:plw:585:Palau | |
486 | py:pry:600:Paraguay | |
487 | qa:qat:634:Qatar | |
488 | re:reu:638:Reunion | |
489 | ro:rom:642:Romania | |
490 | ru:rus:643:Russian Federation:Russia | |
491 | rw:rwa:646:Rwanda | |
492 | sa:sau:682:Saudi Arabia | |
493 | sb:slb:090:Solomon Islands | |
494 | sc:syc:690:Seychelles | |
495 | sd:sdn:736:Sudan | |
496 | se:swe:752:Sweden | |
497 | sg:sgp:702:Singapore | |
498 | sh:shn:654:Saint Helena | |
499 | si:svn:705:Slovenia | |
500 | sj:sjm:744:Svalbard and Jan Mayen:Jan Mayen:Svalbard | |
501 | sk:svk:703:Slovakia | |
502 | sl:sle:694:Sierra Leone | |
503 | sm:smr:674:San Marino | |
504 | sn:sen:686:Senegal | |
505 | so:som:706:Somalia | |
506 | sr:sur:740:Suriname | |
507 | st:stp:678:Sao Tome and Principe | |
508 | sv:slv:222:El Salvador | |
509 | sy:syr:760:Syrian Arab Republic:Syria | |
510 | sz:swz:748:Swaziland | |
511 | tc:tca:796:Turks and Caicos Islands | |
512 | td:tcd:148:Chad | |
513 | tf:atf:260:French Southern Territories | |
514 | tg:tgo:768:Togo | |
515 | th:tha:764:Thailand | |
516 | tj:tjk:762:Tajikistan | |
517 | tk:tkl:772:Tokelau | |
518 | tm:tkm:795:Turkmenistan | |
519 | tn:tun:788:Tunisia | |
520 | to:ton:776:Tonga | |
521 | tl:tls:626:East Timor | |
522 | tr:tur:792:Turkey | |
523 | tt:tto:780:Trinidad and Tobago | |
524 | tv:tuv:798:Tuvalu | |
525 | tw:twn:158:Taiwan, Province of China:Taiwan | |
526 | tz:tza:834:Tanzania, United Republic of:Tanzania | |
527 | ua:ukr:804:Ukraine | |
528 | ug:uga:800:Uganda | |
529 | um:umi:581:United States Minor Outlying Islands | |
530 | us:usa:840:United States:USA:United States of America | |
531 | uy:ury:858:Uruguay | |
532 | uz:uzb:860:Uzbekistan | |
533 | va:vat:336:Holy See (Vatican City State):Holy See (Vatican City) | |
534 | vc:vct:670:Saint Vincent and the Grenadines | |
535 | ve:ven:862:Venezuela | |
536 | vg:vgb:092:Virgin Islands, British:British Virgin Islands | |
537 | vi:vir:850:Virgin Islands, U.S. | |
538 | vn:vnm:704:Vietnam | |
539 | vu:vut:548:Vanuatu | |
540 | wf:wlf:876:Wallis and Futuna | |
541 | ws:wsm:882:Samoa | |
542 | ye:yem:887:Yemen | |
543 | yt:myt:175:Mayotte | |
544 | yu:yug:891:Yugoslavia | |
545 | za:zaf:710:South Africa | |
546 | zm:zmb:894:Zambia | |
547 | zr:zar:180:Zaire:Congo, The Democratic Republic of the:Congo, Democratic Republic of the | |
548 | zw:zwe:716:Zimbabwe |