Commit | Line | Data |
47a334e9 |
1 | package Locale::Country; |
f768f60b |
2 | # Copyright (C) 2001 Canon Research Centre Europe (CRE). |
3 | # Copyright (C) 2002-2009 Neil Bowers |
4 | # Copyright (c) 2010-2010 Sullivan Beck |
5 | # This program is free software; you can redistribute it and/or modify it |
6 | # under the same terms as Perl itself. |
7 | |
47a334e9 |
8 | use strict; |
f768f60b |
9 | use warnings; |
47a334e9 |
10 | require 5.002; |
11 | |
47a334e9 |
12 | require Exporter; |
13 | use Carp; |
f768f60b |
14 | use Locale::Codes; |
47a334e9 |
15 | use Locale::Constants; |
f768f60b |
16 | use Locale::Codes::Country; |
47a334e9 |
17 | |
f768f60b |
18 | #======================================================================= |
19 | # Public Global Variables |
20 | #======================================================================= |
47a334e9 |
21 | |
47a334e9 |
22 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); |
f768f60b |
23 | |
24 | $VERSION='3.12'; |
47a334e9 |
25 | @ISA = qw(Exporter); |
f768f60b |
26 | @EXPORT = qw(code2country |
27 | country2code |
28 | all_country_codes |
29 | all_country_names |
30 | country_code2code |
31 | LOCALE_CODE_ALPHA_2 |
32 | LOCALE_CODE_ALPHA_3 |
33 | LOCALE_CODE_NUMERIC |
34 | LOCALE_CODE_FIPS |
35 | LOCALE_CODE_DOM |
36 | ); |
37 | |
38 | sub _code { |
39 | my($code,$codeset) = @_; |
40 | $code = "" if (! $code); |
41 | |
42 | $codeset = LOCALE_CODE_DEFAULT if (! defined($codeset) || $codeset eq ""); |
43 | |
44 | if ($codeset =~ /^\d+$/) { |
45 | if ($codeset == LOCALE_CODE_ALPHA_2) { |
46 | $codeset = "alpha2"; |
47 | } elsif ($codeset == LOCALE_CODE_ALPHA_3) { |
48 | $codeset = "alpha3"; |
49 | } elsif ($codeset == LOCALE_CODE_NUMERIC) { |
50 | $codeset = "num"; |
51 | } elsif ($codeset == LOCALE_CODE_FIPS) { |
52 | $codeset = "fips"; |
53 | } elsif ($codeset == LOCALE_CODE_DOM) { |
54 | $codeset = "dom"; |
55 | } else { |
56 | return (1); |
57 | } |
58 | } |
59 | |
60 | if ($codeset eq "alpha2" || |
61 | $codeset eq "alpha3") { |
62 | $code = lc($code); |
63 | } elsif ($codeset eq "num") { |
64 | if (defined($code) && $code ne "") { |
65 | return (1) unless ($code =~ /^\d+$/); |
66 | $code = sprintf("%.3d", $code); |
67 | } |
68 | } elsif ($codeset eq "fips" || |
69 | $codeset eq "dom") { |
70 | $code = uc($code); |
71 | } else { |
72 | return (1); |
73 | } |
74 | |
75 | return (0,$code,$codeset); |
76 | } |
47a334e9 |
77 | |
f768f60b |
78 | #======================================================================= |
79 | # |
80 | # code2country ( CODE [,CODESET] ) |
81 | # |
82 | #======================================================================= |
83 | |
84 | sub code2country { |
85 | my($err,$code,$codeset) = _code(@_); |
86 | return undef if ($err || |
87 | ! defined $code); |
47a334e9 |
88 | |
f768f60b |
89 | return Locale::Codes::_code2name("country",$code,$codeset); |
90 | } |
47a334e9 |
91 | |
92 | #======================================================================= |
6b14ceb7 |
93 | # |
f768f60b |
94 | # country2code ( COUNTRY [,CODESET] ) |
6b14ceb7 |
95 | # |
47a334e9 |
96 | #======================================================================= |
47a334e9 |
97 | |
f768f60b |
98 | sub country2code { |
99 | my($country,$codeset) = @_; |
100 | my($err,$tmp); |
101 | ($err,$tmp,$codeset) = _code("",$codeset); |
102 | return undef if ($err || |
103 | ! defined $country); |
104 | |
105 | return Locale::Codes::_name2code("country",$country,$codeset); |
106 | } |
6b14ceb7 |
107 | |
108 | #======================================================================= |
109 | # |
f768f60b |
110 | # country_code2code ( CODE,CODESET_IN,CODESET_OUT ) |
6b14ceb7 |
111 | # |
112 | #======================================================================= |
47a334e9 |
113 | |
f768f60b |
114 | sub country_code2code { |
115 | (@_ == 3) or croak "country_code2code() takes 3 arguments!"; |
116 | my($code,$inset,$outset) = @_; |
117 | my($err,$tmp); |
118 | ($err,$code,$inset) = _code($code,$inset); |
119 | return undef if ($err); |
120 | ($err,$tmp,$outset) = _code("",$outset); |
121 | return undef if ($err); |
122 | |
123 | return Locale::Codes::_code2code("country",$code,$inset,$outset); |
124 | } |
6b14ceb7 |
125 | |
126 | #======================================================================= |
127 | # |
f768f60b |
128 | # all_country_codes ( [CODESET] ) |
6b14ceb7 |
129 | # |
130 | #======================================================================= |
f768f60b |
131 | |
132 | sub all_country_codes { |
133 | my($codeset) = @_; |
134 | my($err,$tmp); |
135 | ($err,$tmp,$codeset) = _code("",$codeset); |
136 | return undef if ($err); |
137 | |
138 | return Locale::Codes::_all_codes("country",$codeset); |
47a334e9 |
139 | } |
140 | |
47a334e9 |
141 | |
142 | #======================================================================= |
6b14ceb7 |
143 | # |
f768f60b |
144 | # all_country_names ( [CODESET] ) |
6b14ceb7 |
145 | # |
146 | #======================================================================= |
47a334e9 |
147 | |
f768f60b |
148 | sub all_country_names { |
149 | my($codeset) = @_; |
150 | my($err,$tmp); |
151 | ($err,$tmp,$codeset) = _code("",$codeset); |
152 | return undef if ($err); |
47a334e9 |
153 | |
f768f60b |
154 | return Locale::Codes::_all_names("country",$codeset); |
155 | } |
6b14ceb7 |
156 | |
157 | #======================================================================= |
158 | # |
f768f60b |
159 | # rename_country ( CODE,NAME [,CODESET] ) |
6b14ceb7 |
160 | # |
161 | #======================================================================= |
47a334e9 |
162 | |
f768f60b |
163 | sub rename_country { |
164 | my($code,$new_name,@args) = @_; |
165 | my $nowarn = 0; |
166 | $nowarn = 1, pop(@args) if ($args[$#args] eq "nowarn"); |
167 | my $codeset = shift(@args); |
168 | my $err; |
169 | ($err,$code,$codeset) = _code($code,$codeset); |
47a334e9 |
170 | |
f768f60b |
171 | return Locale::Codes::_rename("country",$code,$new_name,$codeset,$nowarn); |
172 | } |
47a334e9 |
173 | |
6b14ceb7 |
174 | #======================================================================= |
175 | # |
f768f60b |
176 | # add_country ( CODE,NAME [,CODESET] ) |
6b14ceb7 |
177 | # |
f768f60b |
178 | #======================================================================= |
179 | |
180 | sub add_country { |
181 | my($code,$name,@args) = @_; |
182 | my $nowarn = 0; |
183 | $nowarn = 1, pop(@args) if ($args[$#args] eq "nowarn"); |
184 | my $codeset = shift(@args); |
185 | my $err; |
186 | ($err,$code,$codeset) = _code($code,$codeset); |
187 | |
188 | return Locale::Codes::_add_code("country",$code,$name,$codeset,$nowarn); |
189 | } |
190 | |
191 | #======================================================================= |
6b14ceb7 |
192 | # |
f768f60b |
193 | # delete_country ( CODE [,CODESET] ) |
6b14ceb7 |
194 | # |
195 | #======================================================================= |
47a334e9 |
196 | |
f768f60b |
197 | sub delete_country { |
198 | my($code,@args) = @_; |
199 | my $nowarn = 0; |
200 | $nowarn = 1, pop(@args) if ($args[$#args] eq "nowarn"); |
201 | my $codeset = shift(@args); |
202 | my $err; |
203 | ($err,$code,$codeset) = _code($code,$codeset); |
47a334e9 |
204 | |
f768f60b |
205 | return Locale::Codes::_delete_code("country",$code,$codeset,$nowarn); |
206 | } |
47a334e9 |
207 | |
f768f60b |
208 | #======================================================================= |
209 | # |
210 | # add_country_alias ( NAME,NEW_NAME ) |
211 | # |
212 | #======================================================================= |
47a334e9 |
213 | |
f768f60b |
214 | sub add_country_alias { |
215 | my($name,$new_name,$nowarn) = @_; |
216 | $nowarn = (defined($nowarn) && $nowarn eq "nowarn" ? 1 : 0); |
217 | |
218 | return Locale::Codes::_add_alias("country",$name,$new_name,$nowarn); |
47a334e9 |
219 | } |
220 | |
f768f60b |
221 | #======================================================================= |
222 | # |
223 | # delete_country_alias ( NAME ) |
224 | # |
225 | #======================================================================= |
226 | |
227 | sub delete_country_alias { |
228 | my($name,$nowarn) = @_; |
229 | $nowarn = (defined($nowarn) && $nowarn eq "nowarn" ? 1 : 0); |
917211f5 |
230 | |
f768f60b |
231 | return Locale::Codes::_delete_alias("country",$name,$nowarn); |
232 | } |
917211f5 |
233 | |
234 | #======================================================================= |
235 | # |
f768f60b |
236 | # rename_country_code ( CODE,NEW_CODE [,CODESET] ) |
917211f5 |
237 | # |
238 | #======================================================================= |
917211f5 |
239 | |
f768f60b |
240 | sub rename_country_code { |
241 | my($code,$new_code,@args) = @_; |
242 | my $nowarn = 0; |
243 | $nowarn = 1, pop(@args) if ($args[$#args] eq "nowarn"); |
244 | my $codeset = shift(@args); |
245 | my $err; |
246 | ($err,$code,$codeset) = _code($code,$codeset); |
247 | ($err,$new_code,$codeset) = _code($new_code,$codeset) if (! $err); |
248 | |
249 | return Locale::Codes::_rename_code("country",$code,$new_code,$codeset,$nowarn); |
250 | } |
917211f5 |
251 | |
252 | #======================================================================= |
253 | # |
f768f60b |
254 | # add_country_code_alias ( CODE,NEW_CODE [,CODESET] ) |
917211f5 |
255 | # |
256 | #======================================================================= |
917211f5 |
257 | |
f768f60b |
258 | sub add_country_code_alias { |
259 | my($code,$new_code,@args) = @_; |
260 | my $nowarn = 0; |
261 | $nowarn = 1, pop(@args) if ($args[$#args] eq "nowarn"); |
262 | my $codeset = shift(@args); |
263 | my $err; |
264 | ($err,$code,$codeset) = _code($code,$codeset); |
265 | ($err,$new_code,$codeset) = _code($new_code,$codeset) if (! $err); |
917211f5 |
266 | |
f768f60b |
267 | return Locale::Codes::_add_code_alias("country",$code,$new_code,$codeset,$nowarn); |
917211f5 |
268 | } |
269 | |
f768f60b |
270 | #======================================================================= |
271 | # |
272 | # delete_country_code_alias ( CODE [,CODESET] ) |
273 | # |
274 | #======================================================================= |
275 | |
276 | sub delete_country_code_alias { |
277 | my($code,@args) = @_; |
278 | my $nowarn = 0; |
279 | $nowarn = 1, pop(@args) if ($args[$#args] eq "nowarn"); |
280 | my $codeset = shift(@args); |
281 | my $err; |
282 | ($err,$code,$codeset) = _code($code,$codeset); |
283 | |
284 | return Locale::Codes::_delete_code_alias("country",$code,$codeset,$nowarn); |
285 | } |
47a334e9 |
286 | |
287 | #======================================================================= |
6b14ceb7 |
288 | # |
f768f60b |
289 | # Old function for backward compatibility |
6b14ceb7 |
290 | # |
47a334e9 |
291 | #======================================================================= |
f768f60b |
292 | |
293 | sub alias_code { |
294 | my($alias,$code,@args) = @_; |
295 | my $success = rename_country_code($code,$alias,@args); |
296 | return 0 if (! $success); |
297 | return $alias; |
47a334e9 |
298 | } |
299 | |
300 | 1; |
f768f60b |
301 | # Local Variables: |
302 | # mode: cperl |
303 | # indent-tabs-mode: nil |
304 | # cperl-indent-level: 3 |
305 | # cperl-continued-statement-offset: 2 |
306 | # cperl-continued-brace-offset: 0 |
307 | # cperl-brace-offset: 0 |
308 | # cperl-brace-imaginary-offset: 0 |
309 | # cperl-label-offset: -2 |
310 | # End: |