Upgrade to Locale::Codes 2.00.
[p5sagit/p5-mst-13.2.git] / lib / Locale / Language.pm
CommitLineData
47a334e9 1#-----------------------------------------------------------------------
2
3=head1 NAME
4
5Locale::Language - ISO two letter codes for language identification (ISO 639)
6
7=head1 SYNOPSIS
8
9 use Locale::Language;
88c28ceb 10
47a334e9 11 $lang = code2language('en'); # $lang gets 'English'
12 $code = language2code('French'); # $code gets 'fr'
88c28ceb 13
47a334e9 14 @codes = all_language_codes();
15 @names = all_language_names();
16
17=cut
18
19#-----------------------------------------------------------------------
20
21package Locale::Language;
22use strict;
23require 5.002;
24
25#-----------------------------------------------------------------------
26
27=head1 DESCRIPTION
28
29The C<Locale::Language> module provides access to the ISO two-letter
30codes for identifying languages, as defined in ISO 639. You can either
31access the codes via the L<conversion routines> (described below),
6b6e008c 32or via the two functions which return lists of all language codes or
47a334e9 33all language names.
34
35=cut
36
37#-----------------------------------------------------------------------
38
39require Exporter;
40
41#-----------------------------------------------------------------------
42# Public Global Variables
43#-----------------------------------------------------------------------
44use vars qw($VERSION @ISA @EXPORT);
6b6e008c 45$VERSION = sprintf("%d.%02d", q$Revision: 2.0 $ =~ /(\d+)\.(\d+)/);
47a334e9 46@ISA = qw(Exporter);
47@EXPORT = qw(&code2language &language2code
48 &all_language_codes &all_language_names );
49
50#-----------------------------------------------------------------------
51# Private Global Variables
52#-----------------------------------------------------------------------
53my %CODES = ();
54my %LANGUAGES = ();
55
56
57#=======================================================================
58
59=head1 CONVERSION ROUTINES
60
61There are two conversion routines: C<code2language()> and C<language2code()>.
62
63=over 8
64
65=item code2language()
66
67This function takes a two letter language code and returns a string
68which contains the name of the language identified. If the code is
69not a valid language code, as defined by ISO 639, then C<undef>
70will be returned.
71
72 $lang = code2language($code);
73
74=item language2code()
75
76This function takes a language name and returns the corresponding
77two letter language code, if such exists.
78If the argument could not be identified as a language name,
79then C<undef> will be returned.
80
81 $code = language2code('French');
82
83The case of the language name is not important.
84See the section L<KNOWN BUGS AND LIMITATIONS> below.
85
86=back
87
88=cut
89
90#=======================================================================
91sub code2language
92{
93 my $code = shift;
94
95
96 return undef unless defined $code;
97 $code = lc($code);
98 if (exists $CODES{$code})
99 {
100 return $CODES{$code};
101 }
102 else
103 {
104 #---------------------------------------------------------------
105 # no such language code!
106 #---------------------------------------------------------------
107 return undef;
108 }
109}
110
111sub language2code
112{
113 my $lang = shift;
114
115
116 return undef unless defined $lang;
117 $lang = lc($lang);
118 if (exists $LANGUAGES{$lang})
119 {
120 return $LANGUAGES{$lang};
121 }
122 else
123 {
124 #---------------------------------------------------------------
125 # no such language!
126 #---------------------------------------------------------------
127 return undef;
128 }
129}
130
131#=======================================================================
132
133=head1 QUERY ROUTINES
134
135There are two function which can be used to obtain a list of all
136language codes, or all language names:
137
138=over 8
139
140=item C<all_language_codes()>
141
142Returns a list of all two-letter language codes.
143The codes are guaranteed to be all lower-case,
144and not in any particular order.
145
146=item C<all_language_names()>
147
148Returns a list of all language names for which there is a corresponding
149two-letter language code. The names are capitalised, and not returned
150in any particular order.
151
152=back
153
154=cut
155
156#=======================================================================
157sub all_language_codes
158{
159 return keys %CODES;
160}
161
162sub all_language_names
163{
164 return values %CODES;
165}
166
167#-----------------------------------------------------------------------
168
169=head1 EXAMPLES
170
171The following example illustrates use of the C<code2language()> function.
172The user is prompted for a language code, and then told the corresponding
173language name:
174
175 $| = 1; # turn off buffering
88c28ceb 176
47a334e9 177 print "Enter language code: ";
178 chop($code = <STDIN>);
179 $lang = code2language($code);
180 if (defined $lang)
181 {
182 print "$code = $lang\n";
183 }
184 else
185 {
186 print "'$code' is not a valid language code!\n";
187 }
188
189=head1 KNOWN BUGS AND LIMITATIONS
190
191=over 4
192
193=item *
194
195In the current implementation, all data is read in when the
196module is loaded, and then held in memory.
197A lazy implementation would be more memory friendly.
198
199=item *
200
201Currently just supports the two letter language codes -
202there are also three-letter codes, and numbers.
203Would these be of any use to anyone?
204
205=back
206
207=head1 SEE ALSO
208
209=over 4
210
211=item Locale::Country
212
213ISO codes for identification of country (ISO 3166).
214Supports 2-letter, 3-letter, and numeric country codes.
215
6b6e008c 216=item Locale::Script
217
218ISO codes for identification of written scripts (ISO 15924).
219
47a334e9 220=item Locale::Currency
221
222ISO three letter codes for identification of currencies and funds (ISO 4217).
223
224=item ISO 639:1988 (E/F)
225
226Code for the representation of names of languages.
227
228=item http://lcweb.loc.gov/standards/iso639-2/langhome.html
229
6b6e008c 230Home page for ISO 639-2.
47a334e9 231
232=back
233
234
235=head1 AUTHOR
236
6b6e008c 237Neil Bowers E<lt>neil@bowers.comE<gt>
47a334e9 238
239=head1 COPYRIGHT
240
6b6e008c 241Copyright (C) 2002, Neil Bowers.
242
47a334e9 243Copyright (c) 1997-2001 Canon Research Centre Europe (CRE).
244
245This module is free software; you can redistribute it and/or
246modify it under the same terms as Perl itself.
247
248=cut
249
250#-----------------------------------------------------------------------
251
252#=======================================================================
253# initialisation code - stuff the DATA into the CODES hash
254#=======================================================================
255{
256 my $code;
257 my $language;
258
259
260 while (<DATA>)
261 {
6b6e008c 262 next unless /\S/;
47a334e9 263 chop;
264 ($code, $language) = split(/:/, $_, 2);
265 $CODES{$code} = $language;
266 $LANGUAGES{"\L$language"} = $code;
267 }
268}
269
2701;
271
272__DATA__
273aa:Afar
274ab:Abkhazian
275ae:Avestan
276af:Afrikaans
277am:Amharic
278ar:Arabic
279as:Assamese
280ay:Aymara
281az:Azerbaijani
282
283ba:Bashkir
284be:Belarusian
285bg:Bulgarian
286bh:Bihari
287bi:Bislama
288bn:Bengali
289bo:Tibetan
290br:Breton
291bs:Bosnian
292
293ca:Catalan
294ce:Chechen
295ch:Chamorro
296co:Corsican
297cs:Czech
298cu:Church Slavic
299cv:Chuvash
300cy:Welsh
301
302da:Danish
303de:German
304dz:Dzongkha
305
306el:Greek
307en:English
308eo:Esperanto
309es:Spanish
310et:Estonian
311eu:Basque
312
313fa:Persian
314fi:Finnish
315fj:Fijian
316fo:Faeroese
317fr:French
318fy:Frisian
319
320ga:Irish
321gd:Gaelic (Scots)
322gl:Gallegan
323gn:Guarani
324gu:Gujarati
325gv:Manx
326
327ha:Hausa
328he:Hebrew
329hi:Hindi
330ho:Hiri Motu
331hr:Croatian
332hu:Hungarian
333hy:Armenian
334hz:Herero
335
336ia:Interlingua
337id:Indonesian
338ie:Interlingue
339ik:Inupiaq
340is:Icelandic
341it:Italian
342iu:Inuktitut
343
344ja:Japanese
345jw:Javanese
346
347ka:Georgian
348ki:Kikuyu
349kj:Kuanyama
350kk:Kazakh
351kl:Kalaallisut
352km:Khmer
353kn:Kannada
354ko:Korean
355ks:Kashmiri
356ku:Kurdish
357kv:Komi
358kw:Cornish
359ky:Kirghiz
360
361la:Latin
362lb:Letzeburgesch
363ln:Lingala
364lo:Lao
365lt:Lithuanian
366lv:Latvian
367
368mg:Malagasy
369mh:Marshall
370mi:Maori
371mk:Macedonian
372ml:Malayalam
373mn:Mongolian
374mo:Moldavian
375mr:Marathi
376ms:Malay
377mt:Maltese
378my:Burmese
379
380na:Nauru
381nb:Norwegian Bokmål
382nd:Ndebele, North
383ne:Nepali
384ng:Ndonga
385nl:Dutch
386nn:Norwegian Nynorsk
387no:Norwegian
388nr:Ndebele, South
389nv:Navajo
390ny:Chichewa; Nyanja
391
392oc:Occitan (post 1500)
393om:Oromo
394or:Oriya
395os:Ossetian; Ossetic
396
397pa:Panjabi
398pi:Pali
399pl:Polish
400ps:Pushto
401pt:Portuguese
402
403qu:Quechua
404
405rm:Rhaeto-Romance
406rn:Rundi
407ro:Romanian
408ru:Russian
409rw:Kinyarwanda
410
411sa:Sanskrit
412sc:Sardinian
413sd:Sindhi
414se:Sami
415sg:Sango
416si:Sinhalese
417sk:Slovak
418sl:Slovenian
419sm:Samoan
420sn:Shona
421so:Somali
422sq:Albanian
423sr:Serbian
424ss:Swati
425st:Sotho
426su:Sundanese
427sv:Swedish
428sw:Swahili
429
430ta:Tamil
431te:Telugu
432tg:Tajik
433th:Thai
434ti:Tigrinya
435tk:Turkmen
436tl:Tagalog
437tn:Tswana
438to:Tonga
439tr:Turkish
440ts:Tsonga
441tt:Tatar
442tw:Twi
443
444ug:Uighur
445uk:Ukrainian
446ur:Urdu
447uz:Uzbek
448
449vi:Vietnamese
450vo:Volapük
451
452wo:Wolof
453
454xh:Xhosa
455
456yi:Yiddish
457yo:Yoruba
458
459za:Zhuang
460zh:Chinese
461zu:Zulu