1 package SL::Helper::ISO3166;
7 use Exporter qw(import);
8 our @EXPORT_OK = qw(map_name_to_alpha_2_code);
10 use List::Util qw(first);
12 my @alpha_2_mappings = (
13 [ 'AD', qr{^(?:AD|Andorra)$}i ],
14 [ 'AE', qr{^(?:AE|United Arab Emirates)$}i ],
15 [ 'AF', qr{^(?:AF|Afghanistan)$}i ],
16 [ 'AG', qr{^(?:AG|Antigua and Barbuda)$}i ],
17 [ 'AI', qr{^(?:AI|Anguilla)$}i ],
18 [ 'AL', qr{^(?:AL|Albania)$}i ],
19 [ 'AM', qr{^(?:AM|Armenia)$}i ],
20 [ 'AO', qr{^(?:AO|Angola)$}i ],
21 [ 'AQ', qr{^(?:AQ|Antarctica)$}i ],
22 [ 'AR', qr{^(?:AR|Argentina)$}i ],
23 [ 'AS', qr{^(?:AS|American Samoa)$}i ],
24 [ 'AT', qr{^(?:AT|A|Austria|Österreich)$}i ],
25 [ 'AU', qr{^(?:AU|Australia)$}i ],
26 [ 'AW', qr{^(?:AW|Aruba)$}i ],
27 [ 'AX', qr{^(?:AX|Åland Islands)$}i ],
28 [ 'AZ', qr{^(?:AZ|Azerbaijan)$}i ],
29 [ 'BA', qr{^(?:BA|Bosnia and Herzegovina)$}i ],
30 [ 'BB', qr{^(?:BB|Barbados)$}i ],
31 [ 'BD', qr{^(?:BD|Bangladesh)$}i ],
32 [ 'BE', qr{^(?:BE|Belgium|Belgien)$}i ],
33 [ 'BF', qr{^(?:BF|Burkina Faso)$}i ],
34 [ 'BG', qr{^(?:BG|Bulgaria)$}i ],
35 [ 'BH', qr{^(?:BH|Bahrain)$}i ],
36 [ 'BI', qr{^(?:BI|Burundi)$}i ],
37 [ 'BJ', qr{^(?:BJ|Benin)$}i ],
38 [ 'BL', qr{^(?:BL|Saint Barthélemy)$}i ],
39 [ 'BM', qr{^(?:BM|Bermuda)$}i ],
40 [ 'BN', qr{^(?:BN|Brunei Darussalam)$}i ],
41 [ 'BO', qr{^(?:BO|Bolivia \(Plurinational State of\))$}i ],
42 [ 'BQ', qr{^(?:BQ|Bonaire, Sint Eustatius and Saba)$}i ],
43 [ 'BR', qr{^(?:BR|Brazil)$}i ],
44 [ 'BS', qr{^(?:BS|Bahamas)$}i ],
45 [ 'BT', qr{^(?:BT|Bhutan)$}i ],
46 [ 'BV', qr{^(?:BV|Bouvet Island)$}i ],
47 [ 'BW', qr{^(?:BW|Botswana)$}i ],
48 [ 'BY', qr{^(?:BY|Belarus)$}i ],
49 [ 'BZ', qr{^(?:BZ|Belize)$}i ],
50 [ 'CA', qr{^(?:CA|Canada)$}i ],
51 [ 'CC', qr{^(?:CC|Cocos \(Keeling\) Islands|Cocos Islands|Keeling Islands)$}i ],
52 [ 'CD', qr{^(?:CD|Congo, Democratic Republic of the)$}i ],
53 [ 'CF', qr{^(?:CF|Central African Republic)$}i ],
54 [ 'CG', qr{^(?:CG|Congo)$}i ],
55 [ 'CH', qr{^(?:CH|Switzerland|Schweiz)$}i ],
56 [ 'CI', qr{^(?:CI|Côte d'Ivoire)$}i ],
57 [ 'CK', qr{^(?:CK|Cook Islands)$}i ],
58 [ 'CL', qr{^(?:CL|Chile)$}i ],
59 [ 'CM', qr{^(?:CM|Cameroon)$}i ],
60 [ 'CN', qr{^(?:CN|China)$}i ],
61 [ 'CO', qr{^(?:CO|Colombia)$}i ],
62 [ 'CR', qr{^(?:CR|Costa Rica)$}i ],
63 [ 'CU', qr{^(?:CU|Cuba)$}i ],
64 [ 'CV', qr{^(?:CV|Cabo Verde)$}i ],
65 [ 'CW', qr{^(?:CW|Curaçao)$}i ],
66 [ 'CX', qr{^(?:CX|Christmas Island)$}i ],
67 [ 'CY', qr{^(?:CY|Cyprus)$}i ],
68 [ 'CZ', qr{^(?:CZ|Czechia)$}i ],
69 [ 'DE', qr{^(?:DE|Germany|D|Deutschland)$}i ],
70 [ 'DJ', qr{^(?:DJ|Djibouti)$}i ],
71 [ 'DK', qr{^(?:DK|Denmark)$}i ],
72 [ 'DM', qr{^(?:DM|Dominica)$}i ],
73 [ 'DO', qr{^(?:DO|Dominican Republic)$}i ],
74 [ 'DZ', qr{^(?:DZ|Algeria)$}i ],
75 [ 'EC', qr{^(?:EC|Ecuador)$}i ],
76 [ 'EE', qr{^(?:EE|Estonia)$}i ],
77 [ 'EG', qr{^(?:EG|Egypt)$}i ],
78 [ 'EH', qr{^(?:EH|Western Sahara)$}i ],
79 [ 'ER', qr{^(?:ER|Eritrea)$}i ],
80 [ 'ES', qr{^(?:ES|Spain)$}i ],
81 [ 'ET', qr{^(?:ET|Ethiopia)$}i ],
82 [ 'FI', qr{^(?:FI|Finland)$}i ],
83 [ 'FJ', qr{^(?:FJ|Fiji)$}i ],
84 [ 'FK', qr{^(?:FK|Falkland Islands \(Malvinas\)|Falkland Islands|Falklands)$}i ],
85 [ 'FM', qr{^(?:FM|Micronesia \(Federated States of\)|Micronesia)$}i ],
86 [ 'FO', qr{^(?:FO|Faroe Islands)$}i ],
87 [ 'FR', qr{^(?:FR|France)$}i ],
88 [ 'GA', qr{^(?:GA|Gabon)$}i ],
89 [ 'GB', qr{^(?:GB|United Kingdom of Great Britain and Northern Ireland)$}i ],
90 [ 'GD', qr{^(?:GD|Grenada)$}i ],
91 [ 'GE', qr{^(?:GE|Georgia)$}i ],
92 [ 'GF', qr{^(?:GF|French Guiana)$}i ],
93 [ 'GG', qr{^(?:GG|Guernsey)$}i ],
94 [ 'GH', qr{^(?:GH|Ghana)$}i ],
95 [ 'GI', qr{^(?:GI|Gibraltar)$}i ],
96 [ 'GL', qr{^(?:GL|Greenland)$}i ],
97 [ 'GM', qr{^(?:GM|Gambia)$}i ],
98 [ 'GN', qr{^(?:GN|Guinea)$}i ],
99 [ 'GP', qr{^(?:GP|Guadeloupe)$}i ],
100 [ 'GQ', qr{^(?:GQ|Equatorial Guinea)$}i ],
101 [ 'GR', qr{^(?:GR|Greece)$}i ],
102 [ 'GS', qr{^(?:GS|South Georgia and the South Sandwich Islands)$}i ],
103 [ 'GT', qr{^(?:GT|Guatemala)$}i ],
104 [ 'GU', qr{^(?:GU|Guam)$}i ],
105 [ 'GW', qr{^(?:GW|Guinea-Bissau)$}i ],
106 [ 'GY', qr{^(?:GY|Guyana)$}i ],
107 [ 'HK', qr{^(?:HK|Hong Kong)$}i ],
108 [ 'HM', qr{^(?:HM|Heard Island and McDonald Islands)$}i ],
109 [ 'HN', qr{^(?:HN|Honduras)$}i ],
110 [ 'HR', qr{^(?:HR|Croatia)$}i ],
111 [ 'HT', qr{^(?:HT|Haiti)$}i ],
112 [ 'HU', qr{^(?:HU|Hungary)$}i ],
113 [ 'ID', qr{^(?:ID|Indonesia)$}i ],
114 [ 'IE', qr{^(?:IE|Ireland)$}i ],
115 [ 'IL', qr{^(?:IL|Israel)$}i ],
116 [ 'IM', qr{^(?:IM|Isle of Man)$}i ],
117 [ 'IN', qr{^(?:IN|India)$}i ],
118 [ 'IO', qr{^(?:IO|British Indian Ocean Territory)$}i ],
119 [ 'IQ', qr{^(?:IQ|Iraq)$}i ],
120 [ 'IR', qr{^(?:IR|Iran \(Islamic Republic of\)|Iran)$}i ],
121 [ 'IS', qr{^(?:IS|Iceland)$}i ],
122 [ 'IT', qr{^(?:IT|Italy)$}i ],
123 [ 'JE', qr{^(?:JE|Jersey)$}i ],
124 [ 'JM', qr{^(?:JM|Jamaica)$}i ],
125 [ 'JO', qr{^(?:JO|Jordan)$}i ],
126 [ 'JP', qr{^(?:JP|Japan)$}i ],
127 [ 'KE', qr{^(?:KE|Kenya)$}i ],
128 [ 'KG', qr{^(?:KG|Kyrgyzstan)$}i ],
129 [ 'KH', qr{^(?:KH|Cambodia)$}i ],
130 [ 'KI', qr{^(?:KI|Kiribati)$}i ],
131 [ 'KM', qr{^(?:KM|Comoros)$}i ],
132 [ 'KN', qr{^(?:KN|Saint Kitts and Nevis)$}i ],
133 [ 'KP', qr{^(?:KP|Korea \(Democratic People's Republic of\))$}i ],
134 [ 'KR', qr{^(?:KR|Korea, Republic of|Republic of Korea|Korea)$}i ],
135 [ 'KW', qr{^(?:KW|Kuwait)$}i ],
136 [ 'KY', qr{^(?:KY|Cayman Islands)$}i ],
137 [ 'KZ', qr{^(?:KZ|Kazakhstan)$}i ],
138 [ 'LA', qr{^(?:LA|Lao People's Democratic Republic)$}i ],
139 [ 'LB', qr{^(?:LB|Lebanon)$}i ],
140 [ 'LC', qr{^(?:LC|Saint Lucia)$}i ],
141 [ 'LI', qr{^(?:LI|Liechtenstein)$}i ],
142 [ 'LK', qr{^(?:LK|Sri Lanka)$}i ],
143 [ 'LR', qr{^(?:LR|Liberia)$}i ],
144 [ 'LS', qr{^(?:LS|Lesotho)$}i ],
145 [ 'LT', qr{^(?:LT|Lithuania)$}i ],
146 [ 'LU', qr{^(?:LU|Luxembourg)$}i ],
147 [ 'LV', qr{^(?:LV|Latvia)$}i ],
148 [ 'LY', qr{^(?:LY|Libya)$}i ],
149 [ 'MA', qr{^(?:MA|Morocco)$}i ],
150 [ 'MC', qr{^(?:MC|Monaco)$}i ],
151 [ 'MD', qr{^(?:MD|Moldova, Republic of)$}i ],
152 [ 'ME', qr{^(?:ME|Montenegro)$}i ],
153 [ 'MF', qr{^(?:MF|Saint Martin \(French part\)|Saint Martin)$}i ],
154 [ 'MG', qr{^(?:MG|Madagascar)$}i ],
155 [ 'MH', qr{^(?:MH|Marshall Islands)$}i ],
156 [ 'MK', qr{^(?:MK|North Macedonia)$}i ],
157 [ 'ML', qr{^(?:ML|Mali)$}i ],
158 [ 'MM', qr{^(?:MM|Myanmar)$}i ],
159 [ 'MN', qr{^(?:MN|Mongolia)$}i ],
160 [ 'MO', qr{^(?:MO|Macao)$}i ],
161 [ 'MP', qr{^(?:MP|Northern Mariana Islands)$}i ],
162 [ 'MQ', qr{^(?:MQ|Martinique)$}i ],
163 [ 'MR', qr{^(?:MR|Mauritania)$}i ],
164 [ 'MS', qr{^(?:MS|Montserrat)$}i ],
165 [ 'MT', qr{^(?:MT|Malta)$}i ],
166 [ 'MU', qr{^(?:MU|Mauritius)$}i ],
167 [ 'MV', qr{^(?:MV|Maldives)$}i ],
168 [ 'MW', qr{^(?:MW|Malawi)$}i ],
169 [ 'MX', qr{^(?:MX|Mexico)$}i ],
170 [ 'MY', qr{^(?:MY|Malaysia)$}i ],
171 [ 'MZ', qr{^(?:MZ|Mozambique)$}i ],
172 [ 'NA', qr{^(?:NA|Namibia)$}i ],
173 [ 'NC', qr{^(?:NC|New Caledonia)$}i ],
174 [ 'NE', qr{^(?:NE|Niger)$}i ],
175 [ 'NF', qr{^(?:NF|Norfolk Island)$}i ],
176 [ 'NG', qr{^(?:NG|Nigeria)$}i ],
177 [ 'NI', qr{^(?:NI|Nicaragua)$}i ],
178 [ 'NL', qr{^(?:NL|Netherlands)$}i ],
179 [ 'NO', qr{^(?:NO|Norway)$}i ],
180 [ 'NP', qr{^(?:NP|Nepal)$}i ],
181 [ 'NR', qr{^(?:NR|Nauru)$}i ],
182 [ 'NU', qr{^(?:NU|Niue)$}i ],
183 [ 'NZ', qr{^(?:NZ|New Zealand)$}i ],
184 [ 'OM', qr{^(?:OM|Oman)$}i ],
185 [ 'PA', qr{^(?:PA|Panama)$}i ],
186 [ 'PE', qr{^(?:PE|Peru)$}i ],
187 [ 'PF', qr{^(?:PF|French Polynesia)$}i ],
188 [ 'PG', qr{^(?:PG|Papua New Guinea)$}i ],
189 [ 'PH', qr{^(?:PH|Philippines)$}i ],
190 [ 'PK', qr{^(?:PK|Pakistan)$}i ],
191 [ 'PL', qr{^(?:PL|Poland)$}i ],
192 [ 'PM', qr{^(?:PM|Saint Pierre and Miquelon)$}i ],
193 [ 'PN', qr{^(?:PN|Pitcairn)$}i ],
194 [ 'PR', qr{^(?:PR|Puerto Rico)$}i ],
195 [ 'PS', qr{^(?:PS|Palestine, State of)$}i ],
196 [ 'PT', qr{^(?:PT|Portugal)$}i ],
197 [ 'PW', qr{^(?:PW|Palau)$}i ],
198 [ 'PY', qr{^(?:PY|Paraguay)$}i ],
199 [ 'QA', qr{^(?:QA|Qatar)$}i ],
200 [ 'RE', qr{^(?:RE|Réunion)$}i ],
201 [ 'RO', qr{^(?:RO|Romania)$}i ],
202 [ 'RS', qr{^(?:RS|Serbia)$}i ],
203 [ 'RU', qr{^(?:RU|Russian Federation)$}i ],
204 [ 'RW', qr{^(?:RW|Rwanda)$}i ],
205 [ 'SA', qr{^(?:SA|Saudi Arabia)$}i ],
206 [ 'SB', qr{^(?:SB|Solomon Islands)$}i ],
207 [ 'SC', qr{^(?:SC|Seychelles)$}i ],
208 [ 'SD', qr{^(?:SD|Sudan)$}i ],
209 [ 'SE', qr{^(?:SE|Sweden)$}i ],
210 [ 'SG', qr{^(?:SG|Singapore)$}i ],
211 [ 'SH', qr{^(?:SH|Saint Helena, Ascension and Tristan da Cunha)$}i ],
212 [ 'SI', qr{^(?:SI|Slovenia)$}i ],
213 [ 'SJ', qr{^(?:SJ|Svalbard and Jan Mayen)$}i ],
214 [ 'SK', qr{^(?:SK|Slovakia)$}i ],
215 [ 'SL', qr{^(?:SL|Sierra Leone)$}i ],
216 [ 'SM', qr{^(?:SM|San Marino)$}i ],
217 [ 'SN', qr{^(?:SN|Senegal)$}i ],
218 [ 'SO', qr{^(?:SO|Somalia)$}i ],
219 [ 'SR', qr{^(?:SR|Suriname)$}i ],
220 [ 'SS', qr{^(?:SS|South Sudan)$}i ],
221 [ 'ST', qr{^(?:ST|Sao Tome and Principe)$}i ],
222 [ 'SV', qr{^(?:SV|El Salvador)$}i ],
223 [ 'SX', qr{^(?:SX|Sint Maarten \(Dutch part\)|Sint Maarten)$}i ],
224 [ 'SY', qr{^(?:SY|Syrian Arab Republic)$}i ],
225 [ 'SZ', qr{^(?:SZ|Eswatini)$}i ],
226 [ 'TC', qr{^(?:TC|Turks and Caicos Islands)$}i ],
227 [ 'TD', qr{^(?:TD|Chad)$}i ],
228 [ 'TF', qr{^(?:TF|French Southern Territories)$}i ],
229 [ 'TG', qr{^(?:TG|Togo)$}i ],
230 [ 'TH', qr{^(?:TH|Thailand)$}i ],
231 [ 'TJ', qr{^(?:TJ|Tajikistan)$}i ],
232 [ 'TK', qr{^(?:TK|Tokelau)$}i ],
233 [ 'TL', qr{^(?:TL|Timor-Leste)$}i ],
234 [ 'TM', qr{^(?:TM|Turkmenistan)$}i ],
235 [ 'TN', qr{^(?:TN|Tunisia)$}i ],
236 [ 'TO', qr{^(?:TO|Tonga)$}i ],
237 [ 'TR', qr{^(?:TR|Turkey)$}i ],
238 [ 'TT', qr{^(?:TT|Trinidad and Tobago)$}i ],
239 [ 'TV', qr{^(?:TV|Tuvalu)$}i ],
240 [ 'TW', qr{^(?:TW|Taiwan, Province of China)$}i ],
241 [ 'TZ', qr{^(?:TZ|Tanzania, United Republic of)$}i ],
242 [ 'UA', qr{^(?:UA|Ukraine)$}i ],
243 [ 'UG', qr{^(?:UG|Uganda)$}i ],
244 [ 'UM', qr{^(?:UM|United States Minor Outlying Islands)$}i ],
245 [ 'US', qr{^(?:US|United States of America)$}i ],
246 [ 'UY', qr{^(?:UY|Uruguay)$}i ],
247 [ 'UZ', qr{^(?:UZ|Uzbekistan)$}i ],
248 [ 'VA', qr{^(?:VA|Holy See)$}i ],
249 [ 'VC', qr{^(?:VC|Saint Vincent and the Grenadines)$}i ],
250 [ 'VE', qr{^(?:VE|Venezuela \(Bolivian Republic of\)|Venezuela)$}i ],
251 [ 'VG', qr{^(?:VG|Virgin Islands \(British\))$}i ],
252 [ 'VI', qr{^(?:VI|Virgin Islands \(U\.?S\.?\))$}i ],
253 [ 'VN', qr{^(?:VN|Viet Nam)$}i ],
254 [ 'VU', qr{^(?:VU|Vanuatu)$}i ],
255 [ 'WF', qr{^(?:WF|Wallis and Futuna)$}i ],
256 [ 'WS', qr{^(?:WS|Samoa)$}i ],
257 [ 'YE', qr{^(?:YE|Yemen)$}i ],
258 [ 'YT', qr{^(?:YT|Mayotte)$}i ],
259 [ 'ZA', qr{^(?:ZA|South Africa)$}i ],
260 [ 'ZM', qr{^(?:ZM|Zambia)$}i ],
261 [ 'ZW', qr{^(?:ZW|Zimbabwe)$}i ],
264 sub map_name_to_alpha_2_code {
267 return undef if ($country // '') eq '';
269 my $code = first { $country =~ $_->[1] } @alpha_2_mappings;
270 return $code->[0] if $code;
273 $::lxdebug->message(LXDebug::WARN(), "ISO3166::map_name_to_alpha_2_code: no mapping found for '$country'");