xref: /netbsd-src/lib/libc/locale/setlocale.3 (revision 4afdaab7489ebf554bdc81b916ffdb9a5035b9cb)
1*4afdaab7Snia.\"	$NetBSD: setlocale.3,v 1.23 2022/12/31 14:35:56 nia Exp $
2691ce5a6Skleink.\"
3371c2f6cSperry.\" Copyright (c) 1993
4371c2f6cSperry.\"	The Regents of the University of California.  All rights reserved.
5371c2f6cSperry.\"
6371c2f6cSperry.\" This code is derived from software contributed to Berkeley by
7371c2f6cSperry.\" Donn Seeley at BSDI.
8371c2f6cSperry.\"
9371c2f6cSperry.\" Redistribution and use in source and binary forms, with or without
10371c2f6cSperry.\" modification, are permitted provided that the following conditions
11371c2f6cSperry.\" are met:
12371c2f6cSperry.\" 1. Redistributions of source code must retain the above copyright
13371c2f6cSperry.\"    notice, this list of conditions and the following disclaimer.
14371c2f6cSperry.\" 2. Redistributions in binary form must reproduce the above copyright
15371c2f6cSperry.\"    notice, this list of conditions and the following disclaimer in the
16371c2f6cSperry.\"    documentation and/or other materials provided with the distribution.
17eb7c1594Sagc.\" 3. Neither the name of the University nor the names of its contributors
18371c2f6cSperry.\"    may be used to endorse or promote products derived from this software
19371c2f6cSperry.\"    without specific prior written permission.
20371c2f6cSperry.\"
21371c2f6cSperry.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22371c2f6cSperry.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23371c2f6cSperry.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24371c2f6cSperry.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25371c2f6cSperry.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26371c2f6cSperry.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27371c2f6cSperry.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28371c2f6cSperry.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29371c2f6cSperry.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30371c2f6cSperry.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31371c2f6cSperry.\" SUCH DAMAGE.
32371c2f6cSperry.\"
33371c2f6cSperry.\"	@(#)setlocale.3	8.1 (Berkeley) 6/9/93
34371c2f6cSperry.\"
35*4afdaab7Snia.Dd December 31, 2022
36371c2f6cSperry.Dt SETLOCALE 3
3738c96853Sgarbled.Os
38371c2f6cSperry.Sh NAME
39371c2f6cSperry.Nm setlocale ,
40371c2f6cSperry.Nm localeconv
41371c2f6cSperry.Nd natural language formatting for C
42312aca53Sperry.Sh LIBRARY
43312aca53Sperry.Lb libc
44371c2f6cSperry.Sh SYNOPSIS
45472351e1Swiz.In locale.h
46371c2f6cSperry.Ft char *
47371c2f6cSperry.Fn setlocale "int category" "const char *locale"
48371c2f6cSperry.Ft struct lconv *
49371c2f6cSperry.Fn localeconv "void"
50371c2f6cSperry.Sh DESCRIPTION
51371c2f6cSperryThe
52371c2f6cSperry.Fn setlocale
53371c2f6cSperryfunction sets the C library's notion
54371c2f6cSperryof natural language formatting style
55371c2f6cSperryfor particular sets of routines.
56371c2f6cSperryEach such style is called a
57371c2f6cSperry.Sq locale
58371c2f6cSperryand is invoked using an appropriate name passed as a C string.
59371c2f6cSperryThe
60371c2f6cSperry.Fn localeconv
61371c2f6cSperryroutine returns the current locale's parameters
62371c2f6cSperryfor formatting numbers.
63371c2f6cSperry.Pp
64371c2f6cSperryThe
65371c2f6cSperry.Fn setlocale
66371c2f6cSperryfunction recognizes several categories of routines.
67371c2f6cSperryThese are the categories and the sets of routines they select:
68371c2f6cSperry.Bl -tag -width LC_MONETARY
69371c2f6cSperry.It Dv LC_ALL
70371c2f6cSperrySet the entire locale generically.
71371c2f6cSperry.It Dv LC_COLLATE
72371c2f6cSperrySet a locale for string collation routines.
73371c2f6cSperryThis controls alphabetic ordering in
74371c2f6cSperry.Fn strcoll
75371c2f6cSperryand
76371c2f6cSperry.Fn strxfrm .
77371c2f6cSperry.It Dv LC_CTYPE
78371c2f6cSperrySet a locale for the
79691ce5a6Skleink.Xr ctype 3
80371c2f6cSperryfunctions.
81371c2f6cSperryThis controls recognition of upper and lower case,
82371c2f6cSperryalphabetic or non-alphabetic characters,
833225ede1Swizand so on.
840c949973Skleink.It Dv LC_MESSAGES
850c949973SkleinkSet a locale for message catalogs.
860c949973SkleinkThis controls the selection of message catalogs by the
870c949973Skleink.Xr catgets 3
880c949973Skleinkand
890c949973Skleink.Xr gettext 3
900c949973Skleinkfamilies of functions.
91371c2f6cSperry.It Dv LC_MONETARY
92371c2f6cSperrySet a locale for formatting monetary values;
93371c2f6cSperrythis affects the
94371c2f6cSperry.Fn localeconv
95371c2f6cSperryfunction.
96371c2f6cSperry.It Dv LC_NUMERIC
97371c2f6cSperrySet a locale for formatting numbers.
98371c2f6cSperryThis controls the formatting of decimal points
99371c2f6cSperryin input and output of floating point numbers
100371c2f6cSperryin functions such as
101371c2f6cSperry.Fn printf
102371c2f6cSperryand
103371c2f6cSperry.Fn scanf ,
104371c2f6cSperryas well as values returned by
105371c2f6cSperry.Fn localeconv .
106371c2f6cSperry.It Dv LC_TIME
107371c2f6cSperrySet a locale for formatting dates and times using the
108371c2f6cSperry.Fn strftime
109371c2f6cSperryfunction.
110371c2f6cSperry.El
111371c2f6cSperry.Pp
112371c2f6cSperryOnly three locales are defined by default,
113371c2f6cSperrythe empty string
114371c2f6cSperry.Li "\&""\|""
115371c2f6cSperrywhich denotes the native environment, and the
116371c2f6cSperry.Li "\&""C""
117371c2f6cSperryand
11816289209Sfair.Li "\&""POSIX""
119371c2f6cSperrylocales, which denote the C language environment.
120371c2f6cSperryA
121371c2f6cSperry.Fa locale
122371c2f6cSperryargument of
123371c2f6cSperry.Dv NULL
124371c2f6cSperrycauses
125371c2f6cSperry.Fn setlocale
126371c2f6cSperryto return the current locale.
127371c2f6cSperryBy default, C programs start in the
128371c2f6cSperry.Li "\&""C""
129371c2f6cSperrylocale.
1309cb61837SgmcgarryThe format of the locale string is described in
1319cb61837Sgmcgarry.Xr nls 7 .
1329cb61837Sgmcgarry.Pp
133371c2f6cSperryThe only function in the library that sets the locale is
134371c2f6cSperry.Fn setlocale ;
135371c2f6cSperrythe locale is never changed as a side effect of some other routine.
136371c2f6cSperry.Pp
137d4ec441aSwizChanging the setting of
138d4ec441aSwiz.Dv LC_MESSAGES
139d4ec441aSwizhas no effect on catalogs that have already been opened by
1409cb61837Sgmcgarry.Xr catopen 3 .
1419cb61837Sgmcgarry.Pp
142371c2f6cSperryThe
143371c2f6cSperry.Fn localeconv
144371c2f6cSperryfunction returns a pointer to a structure
145371c2f6cSperrywhich provides parameters for formatting numbers,
146371c2f6cSperryespecially currency values:
147371c2f6cSperry.Bd -literal -offset indent
148371c2f6cSperrystruct lconv {
149371c2f6cSperry	char	*decimal_point;
150371c2f6cSperry	char	*thousands_sep;
151371c2f6cSperry	char	*grouping;
152371c2f6cSperry	char	*int_curr_symbol;
153371c2f6cSperry	char	*currency_symbol;
154371c2f6cSperry	char	*mon_decimal_point;
155371c2f6cSperry	char	*mon_thousands_sep;
156371c2f6cSperry	char	*mon_grouping;
157371c2f6cSperry	char	*positive_sign;
158371c2f6cSperry	char	*negative_sign;
159371c2f6cSperry	char	int_frac_digits;
160371c2f6cSperry	char	frac_digits;
161371c2f6cSperry	char	p_cs_precedes;
162371c2f6cSperry	char	p_sep_by_space;
163371c2f6cSperry	char	n_cs_precedes;
164371c2f6cSperry	char	n_sep_by_space;
165371c2f6cSperry	char	p_sign_posn;
166371c2f6cSperry	char	n_sign_posn;
16720a43da1Skleink	char	int_p_cs_precedes;
16820a43da1Skleink	char	int_n_cs_precedes;
16920a43da1Skleink	char	int_p_sep_by_space;
17020a43da1Skleink	char	int_n_sep_by_space;
17120a43da1Skleink	char	int_p_sign_posn;
17220a43da1Skleink	char	int_n_sign_posn;
173371c2f6cSperry};
174371c2f6cSperry.Ed
175371c2f6cSperry.Pp
176371c2f6cSperryThe individual fields have the following meanings:
17720a43da1Skleink.Bl -tag -width int_p_sep_by_space
178371c2f6cSperry.It Fa decimal_point
17920a43da1SkleinkThe decimal point character, except for monetary values.
180371c2f6cSperry.It Fa thousands_sep
181371c2f6cSperryThe separator between groups of digits
18220a43da1Skleinkbefore the decimal point, except for monetary values.
183371c2f6cSperry.It Fa grouping
18420a43da1SkleinkThe sizes of the groups of digits, except for monetary values.
185371c2f6cSperryThis is a pointer to a vector of integers, each of size
186371c2f6cSperry.Va char ,
187371c2f6cSperryrepresenting group size from low order digit groups
188371c2f6cSperryto high order (right to left).
189371c2f6cSperryThe list may be terminated with 0 or
190371c2f6cSperry.Dv CHAR_MAX .
191371c2f6cSperryIf the list is terminated with 0,
192371c2f6cSperrythe last group size before the 0 is repeated to account for all the digits.
193371c2f6cSperryIf the list is terminated with
194371c2f6cSperry.Dv CHAR_MAX ,
195371c2f6cSperryno more grouping is performed.
196371c2f6cSperry.It Fa int_curr_symbol
19720a43da1SkleinkThe standardized (ISO 4217:1995) international currency symbol.
198371c2f6cSperry.It Fa currency_symbol
199371c2f6cSperryThe local currency symbol.
200371c2f6cSperry.It Fa mon_decimal_point
20120a43da1SkleinkThe decimal point character for monetary values.
202371c2f6cSperry.It Fa mon_thousands_sep
20320a43da1SkleinkThe separator for digit groups in monetary values.
204371c2f6cSperry.It Fa mon_grouping
205371c2f6cSperryLike
206371c2f6cSperry.Fa grouping
20720a43da1Skleinkbut for monetary values.
208371c2f6cSperry.It Fa positive_sign
20920a43da1SkleinkThe character used to denote nonnegative monetary values,
210371c2f6cSperryusually the empty string.
211371c2f6cSperry.It Fa negative_sign
21220a43da1SkleinkThe character used to denote negative monetary values,
213371c2f6cSperryusually a minus sign.
214371c2f6cSperry.It Fa int_frac_digits
215371c2f6cSperryThe number of digits after the decimal point
21620a43da1Skleinkin an internationally formatted monetary value.
217371c2f6cSperry.It Fa frac_digits
218371c2f6cSperryThe number of digits after the decimal point
21920a43da1Skleinkin an locally formatted monetary value.
220371c2f6cSperry.It Fa p_cs_precedes
22120a43da1Skleink1 if the currency symbol precedes the monetary value
222371c2f6cSperryfor nonnegative values, 0 if it follows.
223371c2f6cSperry.It Fa p_sep_by_space
224371c2f6cSperry1 if a space is inserted between the currency symbol
22520a43da1Skleinkand the monetary value for nonnegative values, 0 otherwise.
226371c2f6cSperry.It Fa n_cs_precedes
227371c2f6cSperryLike
228371c2f6cSperry.Fa p_cs_precedes
229371c2f6cSperrybut for negative values.
230371c2f6cSperry.It Fa n_sep_by_space
231371c2f6cSperryLike
232371c2f6cSperry.Fa p_sep_by_space
233371c2f6cSperrybut for negative values.
234371c2f6cSperry.It Fa p_sign_posn
235371c2f6cSperryThe location of the
236371c2f6cSperry.Fa positive_sign
237371c2f6cSperrywith respect to a nonnegative quantity and the
23820a43da1Skleink.Fa currency_symbol .
23920a43da1Skleink.It Fa n_sign_posn
24020a43da1SkleinkLike
24120a43da1Skleink.Fa p_sign_posn
24220a43da1Skleinkbut for negative currency values.
24320a43da1Skleink.It Fa int_p_cs_precedes
24420a43da1Skleink1 if the currency symbol precedes the internationally
24520a43da1Skleinkformatted monetary value for nonnegative values, 0 if it follows.
24620a43da1Skleink.It Fa int_n_cs_precedes
24720a43da1SkleinkLike
24820a43da1Skleink.Fa int_p_cs_precedes
24920a43da1Skleinkbut for negative values.
25020a43da1Skleink.It Fa int_p_sep_by_space
25120a43da1Skleink1 if a space is inserted between the currency symbol
25220a43da1Skleinkand the internationally formatted monetary value for
25320a43da1Skleinknonnegative values, 0 otherwise.
25420a43da1Skleink.It Fa int_n_sep_by_space
25520a43da1SkleinkLike
25620a43da1Skleink.Fa int_p_sep_by_space
25720a43da1Skleinkbut for negative values.
25820a43da1Skleink.It Fa int_p_sign_posn
25920a43da1SkleinkThe location of the
26020a43da1Skleink.Fa positive_sign
26120a43da1Skleinkwith respect to a nonnegative quantity and the
262371c2f6cSperry.Fa currency_symbol ,
26320a43da1Skleinkfor internationally formatted nonnegative monetary values.
26420a43da1Skleink.It Fa int_n_sign_posn
26520a43da1SkleinkLike
26620a43da1Skleink.Fa int_p_sign_posn
26720a43da1Skleinkbut for negative values.
26820a43da1Skleink.El
26920a43da1Skleink.Pp
27020a43da1SkleinkThe positional parameters in
27120a43da1Skleink.Fa p_sign_posn ,
27220a43da1Skleink.Fa n_sign_posn ,
27320a43da1Skleink.Fa int_p_sign_posn
27420a43da1Skleinkand
27520a43da1Skleink.Fa int_n_sign_posn
27620a43da1Skleinkare encoded as follows:
277371c2f6cSperry.Bl -tag -width 3n -compact
278371c2f6cSperry.It Li 0
279371c2f6cSperryParentheses around the entire string.
280371c2f6cSperry.It Li 1
281371c2f6cSperryBefore the string.
282371c2f6cSperry.It Li 2
283371c2f6cSperryAfter the string.
284371c2f6cSperry.It Li 3
285371c2f6cSperryJust before
286371c2f6cSperry.Fa currency_symbol .
287371c2f6cSperry.It Li 4
288371c2f6cSperryJust after
289371c2f6cSperry.Fa currency_symbol .
290371c2f6cSperry.El
291371c2f6cSperry.Pp
292371c2f6cSperryUnless mentioned above,
293371c2f6cSperryan empty string as a value for a field
294371c2f6cSperryindicates a zero length result or
295371c2f6cSperrya value that is not in the current locale.
296371c2f6cSperryA
297371c2f6cSperry.Dv CHAR_MAX
298371c2f6cSperryresult similarly denotes an unavailable value.
29906f97730Suwe.Sh RETURN VALUES
300371c2f6cSperryThe
301371c2f6cSperry.Fn setlocale
302371c2f6cSperryfunction returns
303371c2f6cSperry.Dv NULL
304371c2f6cSperryand fails to change the locale
305371c2f6cSperryif the given combination of
306371c2f6cSperry.Fa category
307371c2f6cSperryand
308371c2f6cSperry.Fa locale
309371c2f6cSperrymakes no sense.
310371c2f6cSperryThe
311371c2f6cSperry.Fn localeconv
312371c2f6cSperryfunction returns a pointer to a static object
313371c2f6cSperrywhich may be altered by later calls to
314371c2f6cSperry.Fn setlocale
315371c2f6cSperryor
316371c2f6cSperry.Fn localeconv .
317*4afdaab7Snia.Pp
318*4afdaab7SniaCurrently,
319*4afdaab7Snia.Fn setlocale
320*4afdaab7Sniareturns
321*4afdaab7Snia.Dv NULL
322*4afdaab7Sniaand fails to change the locale when
323*4afdaab7Snia.Dv LC_COLLATE
324*4afdaab7Sniais modified independently of other values.
3259cb61837Sgmcgarry.Sh EXAMPLES
3269cb61837SgmcgarryThe following code illustrates how a program can initialize the
3279cb61837Sgmcgarryinternational environment for one language, while selectively
3289cb61837Sgmcgarrymodifying the program's locale such that regular expressions and
3299cb61837Sgmcgarrystring operations can be applied to text recorded in a different
3309cb61837Sgmcgarrylanguage:
3319cb61837Sgmcgarry.Bd -literal
3329cb61837Sgmcgarry	setlocale(LC_ALL, "de");
333*4afdaab7Snia	setlocale(LC_NUMERIC, "C");
3349cb61837Sgmcgarry.Ed
3359cb61837Sgmcgarry.Pp
3369cb61837SgmcgarryWhen a process is started, its current locale is set to the C or POSIX
337d4ec441aSwizlocale.
338d4ec441aSwizAn internationalized program that depends on locale data not defined in
339d4ec441aSwizthe C or POSIX locale must invoke the setlocale subroutine in the
340d4ec441aSwizfollowing manner before using any of the locale-specific information:
3419cb61837Sgmcgarry.Bd -literal
3429cb61837Sgmcgarry	setlocale(LC_ALL, "");
3439cb61837Sgmcgarry.Ed
344e036c8bdSgutteridge.Sh FILES
345e036c8bdSgutteridgeThe use of multibyte locales requires shared libraries located in
346e036c8bdSgutteridge.Pa /usr/lib/i18n .
347691ce5a6Skleink.\" .Bl -tag -width /usr/share/locale/locale/category -compact	XXX
348691ce5a6Skleink.\" .It Pa $PATH_LOCALE/\fIlocale\fP/\fIcategory\fP		XXX
349691ce5a6Skleink.\" .It Pa /usr/share/locale/\fIlocale\fP/\fIcategory\fP	XXX
350691ce5a6Skleink.\" locale file for the locale \fIlocale\fP			XXX
351691ce5a6Skleink.\" and the category \fIcategory\fP.				XXX
352691ce5a6Skleink.\" .El
35365f6a76cSkleink.Sh SEE ALSO
3549cb61837Sgmcgarry.Xr catopen 3 ,
3550c949973Skleink.Xr gettext 3 ,
3569cb61837Sgmcgarry.Xr nl_langinfo 3 ,
3579cb61837Sgmcgarry.Xr nls 7
358691ce5a6Skleink.\" .Xr strcoll 3 ,						XXX
359691ce5a6Skleink.\" .Xr strxfrm 3						XXX
360371c2f6cSperry.Sh STANDARDS
361371c2f6cSperryThe
362371c2f6cSperry.Fn setlocale
363371c2f6cSperryand
364371c2f6cSperry.Fn localeconv
365371c2f6cSperryfunctions conform to
36626426e6fSjdolecek.St -ansiC
36726426e6fSjdolecekand
368dca9ccd3Swiz.St -isoC-90 .
36920a43da1Skleink.Pp
37020a43da1SkleinkThe
37120a43da1Skleink.Fa int_p_cs_precedes ,
37220a43da1Skleink.Fa int_n_cs_precedes ,
37320a43da1Skleink.Fa int_p_sep_by_space ,
37420a43da1Skleink.Fa int_n_sep_by_space ,
37520a43da1Skleink.Fa int_p_sign_posn
37620a43da1Skleinkand
37720a43da1Skleink.Fa int_n_sign_posn
37820a43da1Skleinkmembers of
37920a43da1Skleink.Ft struct lconv
38020a43da1Skleinkwere introduced in
381dca9ccd3Swiz.St -isoC-99 .
382371c2f6cSperry.Sh HISTORY
383371c2f6cSperryThe
384371c2f6cSperry.Fn setlocale
385371c2f6cSperryand
386371c2f6cSperry.Fn localeconv
387691ce5a6Skleinkfunctions first appeared in
388691ce5a6Skleink.Bx 4.4 .
389371c2f6cSperry.Sh BUGS
390371c2f6cSperryIn spite of the gnarly currency support in
391371c2f6cSperry.Fn localeconv ,
392371c2f6cSperrythe standards don't include any functions
393371c2f6cSperryfor generalized currency formatting.
394371c2f6cSperry.Pp
395371c2f6cSperry.Dv LC_COLLATE
396*4afdaab7Sniais unimplemented (but does not make sense for many languages).
397371c2f6cSperryUse of
398371c2f6cSperry.Dv LC_MONETARY
399371c2f6cSperrycould lead to misleading results until we have a real time currency
400371c2f6cSperryconversion function.
401371c2f6cSperry.Dv LC_NUMERIC
402371c2f6cSperryand
403371c2f6cSperry.Dv LC_TIME
404371c2f6cSperryare personal choices and should not be wrapped up with the other categories.
40567eaaa4eSyamt.Pp
40667eaaa4eSyamtMultibyte locales aren't supported for static binaries.
407