summaryrefslogtreecommitdiff
path: root/pod/perli18n.pod
blob: 891f95ef48bf50ba6ca87304387e1554086ccd4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
=head1 NAME

perl18n - Perl i18n (internalization)

=head1 DESCRIPTION

Perl supports the language-specific notions of data like
"is this a letter" and "which letter comes first". These
are very important issues especially for languages other
than English -- but also for English: it would be very
naïve indeed to think that C<A-Za-z> defines all the letters.

Perl understands the language-specific data via the standardized
(ISO C, XPG4, POSIX 1.c) method called "the locale system".
The locale system is controlled per application using one
function call and several environment variables.

=head1 USING LOCALES

If your operating system supports the locale system and you have
installed the locale system and you have set your locale environment
variables correctly (please see below) before running Perl, Perl will
understand your data correctly according to your locale settings.

In runtime you can switch locales using the POSIX::setlocale().

	# setlocale is the function call
	# LC_CTYPE will be explained later

	use POSIX qw(setlocale LC_CTYPE);

	# query and save the old locale.
	$old_locale = setlocale(LC_CTYPE);

	setlocale(LC_CTYPE, "fr_CA.ISO8859-1");
	# for LC_CTYPE now in locale "French, Canada, codeset ISO 8859-1"

	setlocale(LC_CTYPE, "");
	# for LC_CTYPE now in locale what the LC_ALL / LC_CTYPE / LANG define.
	# see below for documentation about the LC_ALL / LC_CTYPE / LANG.

	# restore the old locale
	setlocale(LC_CTYPE, $old_locale);

The first argument of C<setlocale()> is called B<the category> and the
second argument B<the locale>. The category tells in what aspect of data
processing we want to apply language-specific rules, the locale tells
in what language-country/territory-codeset - but read on for the naming
of the locales: not all systems name locales as in the example.

For further information about the categories, please consult your
L<setlocale(3)> manual. For the locales available in your system, also
consult the L<setlocale(3)> manual and see whether it leads you to the
list of the available locales (search for the C<SEE ALSO> section). If
that fails, try out in command line the following commands:

=over 12

=item locale -a

=item nlsinfo

=item ls /usr/lib/nls/loc

=item ls /usr/lib/locale

=item ls /usr/lib/nls

=back

and see whether they list something resembling these

	en_US.ISO8859-1		de_DE.ISO8859-1		ru_RU.ISO8859-5
	en_US			de_DE			ru_RU
	en			de			ru
	english			german			russian
	english.iso88591	german.iso88591		russian.iso88595

Sadly enough even if the calling interface has been standardized
the names of the locales are not. The naming usually is
language-country/territory-codeset but the latter parts may
not be present. Two special locales are worth special mention:

	"C"

and
	"POSIX"

Currently and effectively these are the same locale: the difference is
mainly that the first one is defined by the C standard and the second
one is defined by the POSIX standard. What they mean and define is the
B<default locale> in which every program does start in.  The language
is (American) English and the character codeset C<ASCII>.
B<NOTE>: not all systems have the C<"POSIX"> locale (not all systems
are POSIX): use the C<"C"> locale when you need the default locale.

=head2 Category LC_CTYPE: CHARACTER TYPES

Starting from Perl version 5.002 perl has obeyed the C<LC_CTYPE>
environment variable which controls application's notions on
which characters are alphabetic characters. This affects in
Perl the regular expression metanotation

	\w

which stands for alphanumeric characters, that is, alphabetic and
numeric characters (please consult L<perlre> for more information
about regular expressions). Thanks to the C<LC_CTYPE>, depending on
your locale settings, characters like C<Æ>, C<É>, C<ß>, C<ø>, can be
understood as C<\w> characters.

=head2 Category LC_COLLATE: COLLATION

Starting from Perl version 5.003_06 perl has obeyed the B<LC_COLLATE>
environment variable which controls application's notions on the
collation (ordering) of the characters. C<B> does in most Latin
alphabets follow the C<A> but where do the C<Á> and C<Ä> belong?

Here is a code snippet that will tell you what are the alphanumeric
characters in the current locale, in the locale order:

	perl -le 'print sort grep /\w/, map { chr() } 0..255'

As noted above, this will work only for Perl versions 5.003_06 and up.

B<NOTE>: in the pre-5.003_06 Perl releases the per-locale collation
was possible using the C<I18N::Collate> library module. This is now
mildly obsolete and to be avoided. The C<LC_COLLATE> functionality is
integrated into the Perl core language and one can use scalar data
completely normally -- there is no need to juggle with the scalar
references of C<I18N::Collate>.

=head1 ENVIRONMENT

=over 12

=item PERL_BADLANG

A string that controls whether Perl warns in its startup about failed
locale settings. This can happen if the locale support in the
operating system is lacking (broken) is some way. If this string has
an integer value differing from zero, Perl will not complain.
B<NOTE>: this is just hiding the warning message: the message tells
about some problem in your system's locale support and you should
investigate what the problem is.

=back

The following environment variables are not specific to Perl: they are
part of the standardized (ISO C, XPG4, POSIX 1.c) setlocale method to
control an application's opinion on data.

=over 12

=item LC_ALL

C<LC_ALL> is the "override-all" locale environment variable. If it is
set, it overrides all the rest of the locale environment variables.

=item LC_CTYPE

C<LC_ALL> controls the classification of characters, see above.

If this is unset and the C<LC_ALL> is set, the C<LC_ALL> is used as
the C<LC_CTYPE>. If both this and the C<LC_ALL> are unset but the C<LANG>
is set, the C<LANG> is used as the C<LC_CTYPE>.
If none of these three is set, the default locale C<"C">
is used as the C<LC_CTYPE>.

=item LC_COLLATE

C<LC_ALL> controls the collation of characters, see above.

If this is unset and the C<LC_ALL> is set, the C<LC_ALL> is used as
the C<LC_CTYPE>. If both this and the C<LC_ALL> are unset but the
C<LANG> is set, the C<LANG> is used as the C<LC_COLLATE>.
If none of these three is set, the default locale C<"C">
is used as the C<LC_COLLATE>.

=item LANG

LC_ALL is the "catch-all" locale environment variable. If it is set,
it is used as the last resort if neither of the C<LC_ALL> and the
category-specific C<LC_...> are set.

=back

There are further locale-controlling environment variables
(C<LC_MESSAGES, LC_MONETARY, LC_NUMERIC, LC_TIME>) but Perl
B<does not> currently obey them.