1Locale::Codes(3) User Contributed Perl Documentation Locale::Codes(3)
2
3
4
6 Locale::Codes - a distribution of modules to handle locale codes
7
9 Locale-Codes is a distribution containing a set of modules designed to
10 work with sets of codes which uniquely identify something. For
11 example, there are codes associated with different countries, different
12 currencies, different languages, etc. These sets of codes are
13 typically maintained in some standard.
14
15 This distribution provides a way to work with these lists of codes.
16 Because the data from the various standards is not available in any
17 sort of consistent API, access to the lists is not available in any
18 direct fashion. To compensate for this, the list of codes is stored
19 internally within this distribution, and the distribution is updated on
20 a regular basis to include all known codes at that point in time. This
21 does mean that it is necessary to keep this distribution up-to-date to
22 keep up with the various changes that are made in the various
23 standards.
24
25 Traditionally, a module has been created to work with each type of code
26 sets. So, there is a module for working with country lists, one for
27 currency lists, etc. Since version 3.00, all of these individual
28 modules were written as wrappers around a central module (which was not
29 intended to be used directly) which did all of the real work.
30
31 Starting with version 3.50, the central module was reworked slightly to
32 provide an object-oriented interface. All of the modules for working
33 with individual types of code sets were reworked to use the improved OO
34 module, so the traditional interfaces still work as they always have.
35 As a result, you are free to use the traditional functional (non-OO)
36 interfaces, or to use the OO interface and bypass the wrapper modules
37 entirely.
38
39 Both methods will be supported in the future, so use the one that is
40 best suited to your needs.
41
42 Within each type, any number of code sets are allowed. For example,
43 sets of country codes are maintained in several different locations
44 including the ISO-3166 standard, the IANA, and by the United Nations.
45 The lists of countries are similar, but not identical. Multiple code
46 sets are supported, though trying to convert from one code set to
47 another will not always work since the list of countries is not one-to-
48 one.
49
50 All data in all of these modules comes directly from the original
51 standards (or as close to direct as possible), so it should be up-to-
52 date at the time of release.
53
54 I plan on releasing a new version several times a year to incorporate
55 any changes made in the standards. However, I don't always know about
56 changes that occur, so if any of the standards change, and you want a
57 new release sooner, just email me and I'll get one out.
58
60 use Locale::Codes;
61 or
62 use Locale::Codes ':constants';
63
64 $obj = new Locale::Codes 'country';
65
67 The following methods are available.
68
69 In all methods, when specifying a code set, the name (as a string) is
70 always available.
71
72 Traditionally, you could also use a perl constant to specify the code
73 set. In order to do so with the OO interface, you have to import the
74 constants. To do that, load the module with:
75
76 use Locale::Codes ':constants';
77
78 new ( [TYPE [,CODESET]] )
79 $obj = new Locale::Codes;
80 $obj = new Locale::Codes 'country';
81 $obj = new Locale::Codes 'country','alpha-3';
82 $obj = new Locale::Codes 'country',LOCALE_COUNTRY_ALPHA_3;
83
84 This creates a new object that can access the data. If no type is
85 specified (in the first argument), you must use the type method
86 described below. No operations will work unless the type is
87 specified.
88
89 The second argument is the default code set to use. This is
90 optional, as each type has a default code set. The default code
91 set can be set using the codeset method below.
92
93 The last example is only available if the constants were imported
94 when the module was loaded.
95
96 show_errors ( FLAG )
97 $obj->show_errors(1);
98 $obj->show_errors(0);
99
100 By default, error messages will be produced when bad data is passed
101 to any method. By passing in '0', these will be turned off so that
102 all failures will be silent.
103
104 type ( TYPE )
105 $obj->type($type)
106
107 This will set the type of codes that will be worked with. $type
108 may be any of the recognized types of code sets, including:
109
110 country
111 language
112 currency
113 script
114 etc.
115
116 The list of valid types, and the code sets supported in each, are
117 described in the Locale::Codes::Types document.
118
119 This method can be called any number of times to toggle between
120 different types of code sets.
121
122 codeset ( CODESET )
123 $obj->codeset($codeset);
124
125 This sets the default code set to use. The list of code sets
126 available for each type are described in the Locale::Codes::Types
127 document.
128
129 In all other methods below, when an optional CODESET argument is
130 omitted, it will default to this value.
131
132 code2name ( CODE [,CODESET] [,'retired'] )
133 $name = $obj->code2name($code [,$codeset] [,'retired']);
134
135 This functions take a code and returns a string which contains the
136 name of the element identified. If the code is not a valid code in
137 the CODESET specified then "undef" will be returned.
138
139 The name of the element is the name as specified in the standard,
140 and as a result, different variations of an element name may be
141 returned for different values of CODESET.
142
143 For example, the alpha-2 country code set defines the two-letter
144 code "bo" to be "Bolivia, Plurinational State of", whereas the
145 alpha-3 code set defines the code 'bol' to be the country "Bolivia
146 (Plurinational State of)". So:
147
148 $obj->code2name('bo','alpha-2');
149 => 'Bolivia, Plurinational State of'
150
151 $obj->code2name('bol','alpha-3');
152 => 'Bolivia (Plurinational State of)'
153
154 By default, only active codes will be used, but if the string
155 'retired' is passed in as an argument, both active and retired
156 codes will be examined.
157
158 code2names ( CODE [,CODESET] )
159 @name = $obj->code2names($code [,$codeset]);
160
161 This functions take a code and returns a list of all names and
162 aliases associated with that code.
163
164 Only active codes may be used and only active names and aliases
165 will be returned.
166
167 name2code ( NAME [,CODESET] [,'retired'] )
168 $code = $obj->name2code($name [,$codeset] [,'retired']);
169
170 This function takes the name of an element (or any of it's aliases)
171 and returns the code that corresponds to it, if it exists. If NAME
172 could not be identified as the name of one of the elements, then
173 "undef" will be returned.
174
175 The name is not case sensitive. Also, any known variation of a name
176 may be passed in.
177
178 For example, even though the country name returned using 'alpha-2'
179 and 'alpha-3' country codes for Bolivia are different, either
180 country name may be passed in since for each code set (in addition
181 to the more common alias 'Bolivia'). So:
182
183 $obj->name2code('Bolivia, Plurinational State of','alpha-2');
184 => bo
185
186 $obj->name2code('Bolivia (Plurinational State of)','alpha-2');
187 => bo
188
189 $obj->name2code('Bolivia','alpha-2');
190 => bo
191
192 By default, only active names will be used, but if the string
193 'retired' is passed in as an argument, both active and retired
194 names will be examined.
195
196 code2code ( CODE [,CODESET] ,CODESET2 )
197 $code = $obj->code2code($code [,$codeset] ,$codeset2);
198
199 This function takes a code from one code set (CODESET or the
200 default code set), and returns the corresponding code from another
201 code set (CODESET2). CODE must exists in the code set specified by
202 CODESET and must have a corresponding code in the code set
203 specified by CODESET2 or "undef" will be returned.
204
205 $obj->code2code('fin','alpha-3','alpha-2');
206 => 'fi'
207
208 Note that this function does NOT support retired codes.
209
210 all_codes ( [CODESET] [,'retired'] )
211 @code = $obj->all_codes([$codeset] [,'retired']);
212
213 This returns a list of all code in the code set. The codes will be
214 sorted.
215
216 By default, only active codes will be returned, but if the string
217 'retired' is passed in as an argument, both active and retired
218 codes will be returned.
219
220 all_names ( [CODESET] [,'retired'] )
221 @name = $obj->all_names([$codeset] [,'retired']);
222
223 This method returns a list of all elements names for which there is
224 a corresponding code in the specified code set.
225
226 The names returned are exactly as they are specified in the
227 standard, and are sorted.
228
229 Since not all elements are listed in all code sets, the list of
230 elements may differ depending on the code set specified.
231
232 By default, only active names will be returned, but if the string
233 'retired' is passed in as an argument, both active and retired
234 names will be returned.
235
236 The following additional methods are available and can be used to
237 modify the code list data (and are therefore not generally useful).
238
239 rename_code ( CODE ,NEW_NAME [,CODESET] )
240 $flag = $obj->rename_code($code,$new_name [,$codeset]);
241
242 This method can be used to change the official name of an element.
243 At that point, the name returned by the "code2name" method would be
244 NEW_NAME instead of the name specified in the standard.
245
246 The original name will remain as an alias.
247
248 For example, the official country name for code 'gb' is 'United
249 Kingdom'. If you want to change that, you might call:
250
251 $obj->rename_code('gb', 'Great Britain');
252
253 This means that calling code2name('gb') will now return 'Great
254 Britain' instead of 'United Kingdom'.
255
256 If any error occurs, a warning is issued and 0 is returned. An
257 error occurs if CODE doesn't exist in the specified code set, or if
258 NEW_NAME is already in use but for a different element.
259
260 If the method succeeds, 1 is returned.
261
262 add_code ( CODE ,NAME [,CODESET] )
263 $flag = $obj->add_code($code,$name [,$codeset]);
264
265 This method is used to add a new code and name to the data.
266
267 Both CODE and NAME must be unused in the data set or an error
268 occurs (though NAME may be used in a different data set).
269
270 For example, to create the fictitious country named "Duchy of Grand
271 Fenwick" with codes "gf" and "fen", use the following:
272
273 $obj->add_code("fe","Duchy of Grand Fenwick",'alpha-2');
274 $obj->add_code("fen","Duchy of Grand Fenwick",'alpha-3');
275
276 The return value is 1 on success, 0 on an error.
277
278 delete_code ( CODE [,CODESET] )
279 $flag = $obj->delete_code($code [,$codeset]);
280
281 This method is used to delete a code from the data.
282
283 CODE must refer to an existing code in the code set.
284
285 The return value is 1 on success, 0 on an error.
286
287 add_alias ( NAME ,NEW_NAME )
288 $flag = $obj->add_alias($name,$new_name);
289
290 This method is used to add a new alias to the data. They do not
291 alter the return value of the "code2name" function.
292
293 NAME must be an existing element name, and NEW_NAME must be unused
294 or an error occurs.
295
296 The return value is 1 on success, 0 on an error.
297
298 delete_alias ( NAME )
299 $flag = $obj->delete_alias($name);
300
301 This method is used to delete an alias from the data. Once removed,
302 the element may not be referred to by NAME.
303
304 NAME must be one of a list of at least two names that may be used
305 to specify an element. If the element may only be referred to by a
306 single name, you'll need to use the "add_alias" method to add a new
307 alias first, or the "remove_code" method to remove the element
308 entirely.
309
310 If the alias is used as the name in any code set, one of the other
311 names will be used instead. Predicting exactly which one will be
312 used requires you to know the order in which the standards were
313 read, which is not reliable, so you may want to use the
314 "rename_code" method to force one of the alternate names to be
315 used.
316
317 The return value is 1 on success, 0 on an error.
318
319 replace_code ( CODE ,NEW_CODE [,CODESET] )
320 $flag = $obj->replace_code($code,$new_code [,$codeset]);
321
322 This method is used to change the official code for an element. At
323 that point, the code returned by the "name2code" method would be
324 NEW_CODE instead of the code specified in the standard.
325
326 NEW_CODE may either be a code that is not in use, or it may be an
327 alias for CODE (in which case, CODE becomes and alias and NEW_CODE
328 becomes the "real" code).
329
330 The original code is kept as an alias, so that the "code2name"
331 routines will work with either the code from the standard or the
332 new code.
333
334 However, the "all_codes" method will only return the codes which
335 are considered "real" (which means that the list of codes will now
336 contain NEW_CODE, but will not contain CODE).
337
338 add_code_alias ( CODE ,NEW_CODE [,CODESET] )
339 $flag = $obj->add_code_alias($code,$new_code [,$codeset]);
340
341 This method adds an alias for the code. At that point, NEW_CODE and
342 CODE will both work in the "code2name" method. However, the
343 "name2code" method will still return the original code.
344
345 delete_code_alias ( CODE [,CODESET] )
346 These routines delete an alias for the code.
347
348 These will only work if CODE is actually an alias. If it is the
349 "real" code, it will not be deleted. You will need to use the
350 "rename_code" method to switch the real code with one of the
351 aliases, and then delete the alias.
352
354 In addition the the primary OO module, the following modules are
355 included in the distribution for the traditional way of working with
356 code sets.
357
358 Each module will work with one specific type of code sets.
359
360 Locale::Codes::Country, Locale::Country
361 This includes support for country codes (such as those listed in
362 ISO-3166) to specify the country.
363
364 Because this module was originally distributed as Locale::Country,
365 it is also available under that name.
366
367 Locale::Codes::Language, Locale::Language
368 This includes support for language codes (such as those listed in
369 ISO-639) to specify the language.
370
371 Because this module was originally distributed as Locale::Language,
372 it is also available under that name.
373
374 Locale::Codes::Currency, Locale::Currency
375 This includes support for currency codes (such as those listed in
376 ISO-4217) to specify the currency.
377
378 Because this module was originally distributed as Locale::Currency,
379 it is also available under that name.
380
381 Locale::Codes::Script, Locale::Script
382 This includes support for script codes (such as those listed in
383 ISO-15924) to specify the script.
384
385 Because this module was originally distributed as Locale::Script,
386 it is also available under that name.
387
388 Locale::Codes::LangExt
389 This includes support for language extension codes (such as those
390 listed in the IANA language registry) to specify the language
391 extension.
392
393 Locale::Codes::LangVar
394 This includes support for language variation codes (such as those
395 listed in the IANA language registry) to specify the language
396 variation.
397
398 Locale::Codes::LangFam
399 This includes support for language family codes (such as those
400 listed in ISO 639-5) to specify families of languages.
401
402 In addition to the modules above, there are a number of support modules
403 included in the distribution. Any module not listed above falls into
404 that category.
405
406 These modules are not intended to be used by programmers. They contain
407 functions or data that are used by the modules listed above. No
408 support of any kind is offered for using these modules directly. They
409 may be modified at any time.
410
412 As of version 2.00, the modules supported common variants of names.
413
414 For example, Locale::Country supports variant names for countries, and
415 a few of the most common ones are included in the data. The country
416 code for "United States" is "us", so:
417
418 country2code('United States');
419 => "us"
420
421 Now the following will also return 'us':
422
423 country2code('United States of America');
424 country2code('USA');
425
426 Any number of common aliases may be included in the data, in addition
427 to the names that come directly from the standards. If you have a
428 common alias for a country, language, or any other of the types of
429 codes, let me know and I'll add it, with some restrictions.
430
431 For example, the country name "North Korea" never appeared in any of
432 the official sources (instead, it was "Korea, North" or "Korea,
433 Democratic People's Republic of". I would honor a request to add an
434 alias "North Korea" since that's a very common way to specify the
435 country (please don't request this... I've already added it).
436
437 On the other hand, a request to add Zaire as an alias for "Congo, The
438 Democratic Republic of" will not be honored. The country's official
439 name is no longer Zaire, so adding it as an alias violates the
440 standard. Zaire was kept as an alias in versions of this module prior
441 to 3.00, but it has been removed. Other aliases (if any) which no
442 longer appear in any standard (and which are not common variations of
443 the name in the standards) have also been removed.
444
446 Occasionally, a code is deprecated, but it may still be desirable to
447 have access to it.
448
449 Although there is no way to see every code that has ever existed and
450 been deprecated (since most codesets do not have that information
451 available), as of version 3.20, every code which has ever been included
452 in these modules can be referenced.
453
454 For more information, refer to the documentation on the code2name,
455 name2code, all_codes, and all_names methods above.
456
458 Locale::Codes::Types
459 The list of all code sets available for each type.
460
461 Locale::Codes::Changes
462 A history of changes made to this distribution.
463
465 Relationship between code sets
466 Because each code set uses a slightly different list of elements,
467 and they are not necessarily one-to-one, there may be some
468 confusion about the relationship between codes from different code
469 sets.
470
471 For example, ISO 3166 assigns one code to the country "United
472 States Minor Outlying Islands", but the IANA codes give different
473 codes to different islands (Baker Island, Howland Island, etc.).
474
475 This may cause some confusion... I've done the best that I could do
476 to minimize it.
477
478 Non-ASCII characters not supported
479 Currently all names must be all ASCII. I plan on relaxing that
480 limitation in the future.
481
483 If you find a bug in Locale::Codes, there are three ways to send it to
484 me. In order of preference, they are:
485
486 GitHub
487 You can submit it as an issue on GitHub. This can be done at the
488 following URL:
489
490 <https://github.com/SBECK-github/Locale-Codes>
491
492 This is the preferred method. Please submit problems requests as
493 GitHub issues if at all possible.
494
495 Direct email
496 You are welcome to send it directly to me by email. The email
497 address to use is: sbeck@cpan.org.
498
499 Please note that because cpan.org addresses are published, they are
500 used by a lot of spammers and phishers. Please include the name of
501 the perl module in the subject line of ALL messages sent to my
502 cpan.org address or it will likely be missed.
503
504 CPAN Bug Tracking
505 You can submit it using the CPAN tracking tool. This can be done
506 at the following URL:
507
508 <http://rt.cpan.org/Public/Dist/Display.html?Name=Locale-Codes>
509
510 There was discussion of halting this service a while back (though
511 it continues to function), so only use this as a last resort.
512
513 Please do not use other means to report bugs (such as forums for a
514 specific OS or Linux distribution) as it is impossible for me to keep
515 up with all of them. These are the current methods that are guaranteed
516 to notify me.
517
518 When filing a bug report, please include the following information:
519
520 Locale::Codes version
521 Please include the version of Locale::Codes you are using. You can
522 get this by using the script:
523
524 use Locale::Codes;
525 print $Locale::Codes::VERSION,"\n";
526
527 If you want to report missing or incorrect codes, you must be running
528 the most recent version of Locale::Codes.
529
530 If you find any problems with the documentation (errors, typos, or
531 items that are not clear), please send them to me. I welcome any
532 suggestions that will allow me to improve the documentation.
533
535 Locale::Country and Locale::Language were originally written by Neil
536 Bowers at the Canon Research Centre Europe (CRE). They maintained the
537 distribution from 1997 to 2001.
538
539 Locale::Currency was originally written by Michael Hennecke and was
540 modified by Neil Bowers for inclusion in the distribution.
541
542 From 2001 to 2004, maintenance was continued by Neil Bowers. He
543 modified Locale::Currency for inclusion in the distribution. He also
544 added Locale::Script.
545
546 From 2004-2009, the module was unmaintained.
547
548 In 2010, maintenance was taken over by Sullivan Beck (sbeck@cpan.org)
549 with Neil Bower's permission. All problems or comments should be sent
550 to him using any of the methods listed above.
551
553 Copyright (c) 1997-2001 Canon Research Centre Europe (CRE).
554 Copyright (c) 2001 Michael Hennecke (Locale::Currency)
555 Copyright (c) 2001-2010 Neil Bowers
556 Copyright (c) 2010-2023 Sullivan Beck
557
558 This module is free software; you can redistribute it and/or modify it
559 under the same terms as Perl itself.
560
561
562
563perl v5.36.1 2023-06-08 Locale::Codes(3)