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 name2code ( NAME [,CODESET] [,'retired'] )
159 $code = $obj->name2code($name [,$codeset] [,'retired']);
160
161 This function takes the name of an element (or any of it's aliases)
162 and returns the code that corresponds to it, if it exists. If NAME
163 could not be identified as the name of one of the elements, then
164 "undef" will be returned.
165
166 The name is not case sensitive. Also, any known variation of a name
167 may be passed in.
168
169 For example, even though the country name returned using 'alpha-2'
170 and 'alpha-3' country codes for Bolivia are different, either
171 country name may be passed in since for each code set (in addition
172 to the more common alias 'Bolivia'). So:
173
174 $obj->name2code('Bolivia, Plurinational State of','alpha-2');
175 => bo
176
177 $obj->name2code('Bolivia (Plurinational State of)','alpha-2');
178 => bo
179
180 $obj->name2code('Bolivia','alpha-2');
181 => bo
182
183 By default, only active names will be used, but if the string
184 'retired' is passed in as an argument, both active and retired
185 names will be examined.
186
187 code2code ( CODE [,CODESET] ,CODESET2 )
188 $code = $obj->code2code($code [,$codeset] ,$codeset2);
189
190 This function takes a code from one code set (CODESET or the
191 default code set), and returns the corresponding code from another
192 code set (CODESET2). CODE must exists in the code set specified by
193 CODESET and must have a corresponding code in the code set
194 specified by CODESET2 or "undef" will be returned.
195
196 $obj->code2code('fin','alpha-3','alpha-2');
197 => 'fi'
198
199 Note that this function does NOT support retired codes.
200
201 all_codes ( [CODESET] [,'retired'] )
202 @code = $obj->all_codes([$codeset] [,'retired']);
203
204 This returns a list of all code in the code set. The codes will be
205 sorted.
206
207 By default, only active codes will be returned, but if the string
208 'retired' is passed in as an argument, both active and retired
209 codes will be returned.
210
211 all_names ( [CODESET] [,'retired'] )
212 @name = $obj->all_names([$codeset] [,'retired']);
213
214 This method returns a list of all elements names for which there is
215 a corresponding code in the specified code set.
216
217 The names returned are exactly as they are specified in the
218 standard, and are sorted.
219
220 Since not all elements are listed in all code sets, the list of
221 elements may differ depending on the code set specified.
222
223 By default, only active names will be returned, but if the string
224 'retired' is passed in as an argument, both active and retired
225 names will be returned.
226
227 The following additional methods are available and can be used to
228 modify the code list data (and are therefore not generally useful).
229
230 rename_code ( CODE ,NEW_NAME [,CODESET] )
231 $flag = $obj->rename_code($code,$new_name [,$codeset]);
232
233 This method can be used to change the official name of an element.
234 At that point, the name returned by the "code2name" method would be
235 NEW_NAME instead of the name specified in the standard.
236
237 The original name will remain as an alias.
238
239 For example, the official country name for code 'gb' is 'United
240 Kingdom'. If you want to change that, you might call:
241
242 $obj->rename_code('gb', 'Great Britain');
243
244 This means that calling code2name('gb') will now return 'Great
245 Britain' instead of 'United Kingdom'.
246
247 If any error occurs, a warning is issued and 0 is returned. An
248 error occurs if CODE doesn't exist in the specified code set, or if
249 NEW_NAME is already in use but for a different element.
250
251 If the method succeeds, 1 is returned.
252
253 add_code ( CODE ,NAME [,CODESET] )
254 $flag = $obj->add_code($code,$name [,$codeset]);
255
256 This method is used to add a new code and name to the data.
257
258 Both CODE and NAME must be unused in the data set or an error
259 occurs (though NAME may be used in a different data set).
260
261 For example, to create the fictitious country named "Duchy of Grand
262 Fenwick" with codes "gf" and "fen", use the following:
263
264 $obj->add_code("fe","Duchy of Grand Fenwick",'alpha-2');
265 $obj->add_code("fen","Duchy of Grand Fenwick",'alpha-3');
266
267 The return value is 1 on success, 0 on an error.
268
269 delete_code ( CODE [,CODESET] )
270 $flag = $obj->delete_code($code [,$codeset]);
271
272 This method is used to delete a code from the data.
273
274 CODE must refer to an existing code in the code set.
275
276 The return value is 1 on success, 0 on an error.
277
278 add_alias ( NAME ,NEW_NAME )
279 $flag = $obj->add_alias($name,$new_name);
280
281 This method is used to add a new alias to the data. They do not
282 alter the return value of the "code2name" function.
283
284 NAME must be an existing element name, and NEW_NAME must be unused
285 or an error occurs.
286
287 The return value is 1 on success, 0 on an error.
288
289 delete_alias ( NAME )
290 $flag = $obj->delete_alias($name);
291
292 This method is used to delete an alias from the data. Once removed,
293 the element may not be referred to by NAME.
294
295 NAME must be one of a list of at least two names that may be used
296 to specify an element. If the element may only be referred to by a
297 single name, you'll need to use the "add_alias" method to add a new
298 alias first, or the "remove_code" method to remove the element
299 entirely.
300
301 If the alias is used as the name in any code set, one of the other
302 names will be used instead. Predicting exactly which one will be
303 used requires you to know the order in which the standards were
304 read, which is not reliable, so you may want to use the
305 "rename_code" method to force one of the alternate names to be
306 used.
307
308 The return value is 1 on success, 0 on an error.
309
310 replace_code ( CODE ,NEW_CODE [,CODESET] )
311 $flag = $obj->replace_code($code,$new_code [,$codeset]);
312
313 This method is used to change the official code for an element. At
314 that point, the code returned by the "name2code" method would be
315 NEW_CODE instead of the code specified in the standard.
316
317 NEW_CODE may either be a code that is not in use, or it may be an
318 alias for CODE (in which case, CODE becomes and alias and NEW_CODE
319 becomes the "real" code).
320
321 The original code is kept as an alias, so that the "code2name"
322 routines will work with either the code from the standard or the
323 new code.
324
325 However, the "all_codes" method will only return the codes which
326 are considered "real" (which means that the list of codes will now
327 contain NEW_CODE, but will not contain CODE).
328
329 add_code_alias ( CODE ,NEW_CODE [,CODESET] )
330 $flag = $obj->add_code_alias($code,$new_code [,$codeset]);
331
332 This method adds an alias for the code. At that point, NEW_CODE and
333 CODE will both work in the "code2name" method. However, the
334 "name2code" method will still return the original code.
335
336 delete_code_alias ( CODE [,CODESET] )
337 These routines delete an alias for the code.
338
339 These will only work if CODE is actually an alias. If it is the
340 "real" code, it will not be deleted. You will need to use the
341 "rename_code" method to switch the real code with one of the
342 aliases, and then delete the alias.
343
345 In addition the the primary OO module, the following modules are
346 included in the distribution for the traditional way of working with
347 code sets.
348
349 Each module will work with one specific type of code sets.
350
351 Locale::Codes::Country, Locale::Country
352 This includes support for country codes (such as those listed in
353 ISO-3166) to specify the country.
354
355 Because this module was originally distributed as Locale::Country,
356 it is also available under that name.
357
358 Locale::Codes::Language, Locale::Language
359 This includes support for language codes (such as those listed in
360 ISO-639) to specify the language.
361
362 Because this module was originally distributed as Locale::Language,
363 it is also available under that name.
364
365 Locale::Codes::Currency, Locale::Currency
366 This includes support for currency codes (such as those listed in
367 ISO-4217) to specify the currency.
368
369 Because this module was originally distributed as Locale::Currency,
370 it is also available under that name.
371
372 Locale::Codes::Script, Locale::Script
373 This includes support for script codes (such as those listed in
374 ISO-15924) to specify the script.
375
376 Because this module was originally distributed as Locale::Script,
377 it is also available under that name.
378
379 Locale::Codes::LangExt
380 This includes support for language extension codes (such as those
381 listed in the IANA language registry) to specify the language
382 extension.
383
384 Locale::Codes::LangVar
385 This includes support for language variation codes (such as those
386 listed in the IANA language registry) to specify the language
387 variation.
388
389 Locale::Codes::LangFam
390 This includes support for language family codes (such as those
391 listed in ISO 639-5) to specify families of languages.
392
393 In addition to the modules above, there are a number of support modules
394 included in the distribution. Any module not listed above falls into
395 that category.
396
397 These modules are not intended to be used by programmers. They contain
398 functions or data that are used by the modules listed above. No
399 support of any kind is offered for using these modules directly. They
400 may be modified at any time.
401
403 As of version 2.00, the modules supported common variants of names.
404
405 For example, Locale::Country supports variant names for countries, and
406 a few of the most common ones are included in the data. The country
407 code for "United States" is "us", so:
408
409 country2code('United States');
410 => "us"
411
412 Now the following will also return 'us':
413
414 country2code('United States of America');
415 country2code('USA');
416
417 Any number of common aliases may be included in the data, in addition
418 to the names that come directly from the standards. If you have a
419 common alias for a country, language, or any other of the types of
420 codes, let me know and I'll add it, with some restrictions.
421
422 For example, the country name "North Korea" never appeared in any of
423 the official sources (instead, it was "Korea, North" or "Korea,
424 Democratic People's Republic of". I would honor a request to add an
425 alias "North Korea" since that's a very common way to specify the
426 country (please don't request this... I've already added it).
427
428 On the other hand, a request to add Zaire as an alias for "Congo, The
429 Democratic Republic of" will not be honored. The country's official
430 name is no longer Zaire, so adding it as an alias violates the
431 standard. Zaire was kept as an alias in versions of this module prior
432 to 3.00, but it has been removed. Other aliases (if any) which no
433 longer appear in any standard (and which are not common variations of
434 the name in the standards) have also been removed.
435
437 Occasionally, a code is deprecated, but it may still be desirable to
438 have access to it.
439
440 Although there is no way to see every code that has ever existed and
441 been deprecated (since most codesets do not have that information
442 available), as of version 3.20, every code which has ever been included
443 in these modules can be referenced.
444
445 For more information, refer to the documentation on the code2name,
446 name2code, all_codes, and all_names methods above.
447
449 Locale::Codes::Types
450 The list of all code sets available for each type.
451
452 Locale::Codes::Changes
453 A history of changes made to this distribution.
454
456 Relationship between code sets
457 Because each code set uses a slightly different list of elements,
458 and they are not necessarily one-to-one, there may be some
459 confusion about the relationship between codes from different code
460 sets.
461
462 For example, ISO 3166 assigns one code to the country "United
463 States Minor Outlying Islands", but the IANA codes give different
464 codes to different islands (Baker Island, Howland Island, etc.).
465
466 This may cause some confusion... I've done the best that I could do
467 to minimize it.
468
469 Non-ASCII characters not supported
470 Currently all names must be all ASCII. I plan on relaxing that
471 limitation in the future.
472
474 If you find a bug in Locale::Codes, there are three ways to send it to
475 me. Any of them are fine, so use the method that is easiest for you.
476
477 Direct email
478 You are welcome to send it directly to me by email. The email
479 address to use is: sbeck@cpan.org.
480
481 CPAN Bug Tracking
482 You can submit it using the CPAN tracking too. This can be done at
483 the following URL:
484
485 <http://rt.cpan.org/Public/Dist/Display.html?Name=Locale-Codes>
486
487 GitHub
488 You can submit it as an issue on GitHub. This can be done at the
489 following URL:
490
491 <https://github.com/SBECK-github/Locale-Codes>
492
493 Please do not use other means to report bugs (such as forums for a
494 specific OS or Linux distribution) as it is impossible for me to keep
495 up with all of them.
496
497 When filing a bug report, please include the following information:
498
499 Locale::Codes version
500 Please include the version of Locale::Codes you are using. You can
501 get this by using the script:
502
503 use Locale::Codes;
504 print $Locale::Codes::VERSION,"\n";
505
506 If you want to report missing or incorrect codes, you must be running
507 the most recent version of Locale::Codes.
508
509 If you find any problems with the documentation (errors, typos, or
510 items that are not clear), please send them to me. I welcome any
511 suggestions that will allow me to improve the documentation.
512
514 Locale::Country and Locale::Language were originally written by Neil
515 Bowers at the Canon Research Centre Europe (CRE). They maintained the
516 distribution from 1997 to 2001.
517
518 Locale::Currency was originally written by Michael Hennecke and was
519 modified by Neil Bowers for inclusion in the distribution.
520
521 From 2001 to 2004, maintenance was continued by Neil Bowers. He
522 modified Locale::Currency for inclusion in the distribution. He also
523 added Locale::Script.
524
525 From 2004-2009, the module was unmaintained.
526
527 In 2010, maintenance was taken over by Sullivan Beck (sbeck@cpan.org)
528 with Neil Bower's permission. All problems or comments should be sent
529 to him using any of the methods listed above.
530
532 Copyright (c) 1997-2001 Canon Research Centre Europe (CRE).
533 Copyright (c) 2001 Michael Hennecke (Locale::Currency)
534 Copyright (c) 2001-2010 Neil Bowers
535 Copyright (c) 2010-2018 Sullivan Beck
536
537 This module is free software; you can redistribute it and/or modify it
538 under the same terms as Perl itself.
539
540
541
542perl v5.26.3 2018-03-01 Locale::Codes(3)