Cwd.pm docs
[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),
32or with the two functions which return lists of all language codes or
33all language names.
34
35=cut
36
37#-----------------------------------------------------------------------
38
39require Exporter;
40
41#-----------------------------------------------------------------------
42# Public Global Variables
43#-----------------------------------------------------------------------
44use vars qw($VERSION @ISA @EXPORT);
45$VERSION = sprintf("%d.%02d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/);
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
216=item Locale::Currency
217
218ISO three letter codes for identification of currencies and funds (ISO 4217).
219
220=item ISO 639:1988 (E/F)
221
222Code for the representation of names of languages.
223
224=item http://lcweb.loc.gov/standards/iso639-2/langhome.html
225
226Home page for ISO 639-2
227
228=back
229
230
231=head1 AUTHOR
232
233Neil Bowers E<lt>neilb@cre.canon.co.ukE<gt>
234
235=head1 COPYRIGHT
236
237Copyright (c) 1997-2001 Canon Research Centre Europe (CRE).
238
239This module is free software; you can redistribute it and/or
240modify it under the same terms as Perl itself.
241
242=cut
243
244#-----------------------------------------------------------------------
245
246#=======================================================================
247# initialisation code - stuff the DATA into the CODES hash
248#=======================================================================
249{
250 my $code;
251 my $language;
252
253
254 while (<DATA>)
255 {
256 next unless /\S/;
257 chop;
258 ($code, $language) = split(/:/, $_, 2);
259 $CODES{$code} = $language;
260 $LANGUAGES{"\L$language"} = $code;
261 }
262}
263
2641;
265
266__DATA__
267aa:Afar
268ab:Abkhazian
269ae:Avestan
270af:Afrikaans
271am:Amharic
272ar:Arabic
273as:Assamese
274ay:Aymara
275az:Azerbaijani
276
277ba:Bashkir
278be:Belarusian
279bg:Bulgarian
280bh:Bihari
281bi:Bislama
282bn:Bengali
283bo:Tibetan
284br:Breton
285bs:Bosnian
286
287ca:Catalan
288ce:Chechen
289ch:Chamorro
290co:Corsican
291cs:Czech
292cu:Church Slavic
293cv:Chuvash
294cy:Welsh
295
296da:Danish
297de:German
298dz:Dzongkha
299
300el:Greek
301en:English
302eo:Esperanto
303es:Spanish
304et:Estonian
305eu:Basque
306
307fa:Persian
308fi:Finnish
309fj:Fijian
310fo:Faeroese
311fr:French
312fy:Frisian
313
314ga:Irish
315gd:Gaelic (Scots)
316gl:Gallegan
317gn:Guarani
318gu:Gujarati
319gv:Manx
320
321ha:Hausa
322he:Hebrew
323hi:Hindi
324ho:Hiri Motu
325hr:Croatian
326hu:Hungarian
327hy:Armenian
328hz:Herero
329
330ia:Interlingua
331id:Indonesian
332ie:Interlingue
333ik:Inupiaq
334is:Icelandic
335it:Italian
336iu:Inuktitut
337
338ja:Japanese
339jw:Javanese
340
341ka:Georgian
342ki:Kikuyu
343kj:Kuanyama
344kk:Kazakh
345kl:Kalaallisut
346km:Khmer
347kn:Kannada
348ko:Korean
349ks:Kashmiri
350ku:Kurdish
351kv:Komi
352kw:Cornish
353ky:Kirghiz
354
355la:Latin
356lb:Letzeburgesch
357ln:Lingala
358lo:Lao
359lt:Lithuanian
360lv:Latvian
361
362mg:Malagasy
363mh:Marshall
364mi:Maori
365mk:Macedonian
366ml:Malayalam
367mn:Mongolian
368mo:Moldavian
369mr:Marathi
370ms:Malay
371mt:Maltese
372my:Burmese
373
374na:Nauru
375nb:Norwegian Bokmål
376nd:Ndebele, North
377ne:Nepali
378ng:Ndonga
379nl:Dutch
380nn:Norwegian Nynorsk
381no:Norwegian
382nr:Ndebele, South
383nv:Navajo
384ny:Chichewa; Nyanja
385
386oc:Occitan (post 1500)
387om:Oromo
388or:Oriya
389os:Ossetian; Ossetic
390
391pa:Panjabi
392pi:Pali
393pl:Polish
394ps:Pushto
395pt:Portuguese
396
397qu:Quechua
398
399rm:Rhaeto-Romance
400rn:Rundi
401ro:Romanian
402ru:Russian
403rw:Kinyarwanda
404
405sa:Sanskrit
406sc:Sardinian
407sd:Sindhi
408se:Sami
409sg:Sango
410si:Sinhalese
411sk:Slovak
412sl:Slovenian
413sm:Samoan
414sn:Shona
415so:Somali
416sq:Albanian
417sr:Serbian
418ss:Swati
419st:Sotho
420su:Sundanese
421sv:Swedish
422sw:Swahili
423
424ta:Tamil
425te:Telugu
426tg:Tajik
427th:Thai
428ti:Tigrinya
429tk:Turkmen
430tl:Tagalog
431tn:Tswana
432to:Tonga
433tr:Turkish
434ts:Tsonga
435tt:Tatar
436tw:Twi
437
438ug:Uighur
439uk:Ukrainian
440ur:Urdu
441uz:Uzbek
442
443vi:Vietnamese
444vo:Volapük
445
446wo:Wolof
447
448xh:Xhosa
449
450yi:Yiddish
451yo:Yoruba
452
453za:Zhuang
454zh:Chinese
455zu:Zulu