C localization functions: Difference between revisions

Content deleted Content added
Rescuing 2 sources and tagging 0 as dead.) #IABot (v2.0.9.5
 
(48 intermediate revisions by 30 users not shown)
Line 1:
{{cleanupUse dmy dates|date=SeptemberDecember 20112020}}
{{C Standard Library}}
In [[computing]], '''C localization functions''' are a group of functions in the [[C (programming language)|C programming language]] implementing basic localization routines.<ref name="c99">{{cite book | url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf | title=ISO/IEC 9899:1999 specification | at=p. 204, § 7.11 ''Localization'' }}</ref><ref name="c_primer">{{cite book|title=C primer plus | first=Stephen | last=Prata | year=2004 | publisher=Sams Publishing | isbn=0-672-32696-5 | at=Appendix B, Section V: The Standard ANSI C Library with C99 Additions}}</ref> The functions are used in multilingual programs to adapt to the specific locale. In particular, the way of displaying of numbers and currency can be modified. These settings affect the behaviour of [[C file input/output | input/output functions]] in the C Standard Library.
 
In [[computing]], '''C localization functions''' are a group of functions in the [[C (programming language)|C programming language]] implementing basic localization routines.<ref name="c99">{{cite book | url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf | title=ISO/IEC 9899:1999 specification | at=p. 204, § 7.11 ''Localization'' }}</ref><ref name="c_primer">{{cite book|title=C primer plus | first=Stephen | last=Prata | year=2004 | publisher=Sams Publishing | isbn=0-672-32696-5 | at=Appendix B, Section V: The Standard ANSI C Library with C99 Additions}}</ref> The functions are used in multilingual programs to adapt to the specific locale. In particular, the way of displaying of numbers and currency can be modified. These settings affect the behaviour of [[C file input/output | input/output functions]] in the C Standard Library.<ref>{{Cite web|url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf|title=ISO/IEC 9899:201x|date=12 April 2011|page=181|archive-url=https://web.archive.org/web/20180329042731/http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf|archive-date=29 March 2018|url-status=}}</ref>
==Criticism==
 
{{expand section}}
C standard localization functions are criticized because the localization state is stored globally. This means that in a given program all operations involving a locale can use only one locale at a time. As a result, it is very difficult to implement programs that use more than one locale.<ref>{{cite web | title=The Standard C Locale and the Standard C++ Locales | url=http://www.math.hkbu.edu.hk/parallel/pgi/doc/pgC++_lib/stdlibug/sta_9169.htm | publisher=Rogue Wave Software, Inc. | year=1996}}</ref>
 
==Overview of functions==
 
C localization functions and types are defined in {{mono|locale.h}} ({{mono|clocale}} header in C++).<ref name=www.utas.edu.au>{{cite web|title=locale.h|url=http://www.utas.edu.au/infosys/info/documentation/C/CStdLib.html#locale.h|work=utas.edu.au|publisher=infosys|accessdate=14 September 2011|archive-date=4 June 2012|archive-url=https://web.archive.org/web/20120604201614/http://www.utas.edu.au/infosys/info/documentation/C/CStdLib.html#locale.h|url-status=dead}}</ref><ref>{{Cite web|url=https://github.com/openbsd/src/blob/master/include/locale.h|title=openbsd/src|website=GitHub|language=en|access-date=2018-04-09}}</ref>
C localization functions and types are defined in <tt>locale.h</tt> (<tt>clocale</tt> header in C++).
 
{| class="wikitable" style="font-size:0.85em;"
'''struct lconv'''
! Function
*explain formatting monetary and other numeric values.
! Description
<code>'''char'''* decimal point;</code>
|-
*decimal point for non-monetary values
! style="font-family:monospace" | {{anchor|setlocale}}[http://en.cppreference.com/w/c/locale/setlocale setlocale]
<code> '''char'''* grouping;</code>
| sets and gets the current C locale
*size pf digit groups for non-monetary values
|-
<code>'''struct''' lconv* localeconv('''void''');</code><br />
! style="font-family:monospace" | {{anchor|localeconv}}[http://en.cppreference.com/w/c/locale/localeconv localeconv]
<code>'''char'''* setlocale('''int''', '''const char'''*);</code>
| returns numeric and monetary formatting details of the current locale
<code>'''char'''thousand sep;</code>
|-
*separator for non-monetary values
|}
<code>'''char'''* currency symbol;</code>
*currency symbol
<code>'''char'''* int curr symbol;</code>
*international currency symbol
<code>'''char'''*mon-decimal point</code>
*decimal point for monetary values
<code>'''char'''* mon grouping; </code>
*sizes of digit groups for monetary values
<code>'''char'''* mon thousand sep;</code>
*separator for digit groups for monetary values
<ref name=www.utas.edu.au>{{cite web|title=local.h|url=http://www.utas.edu.au/infosys/info/documentation/C/CStdLib.html#locale.h|work=utas.edu.au|publisher=infosys|accessdate=14 September 2011}}</ref>
 
===Example=Criticism==
 
<source lang="cpp">
C standard localization functions are criticized because the localization state is stored globally. This means that in a given program all operations involving a locale can use only one locale at a time. As a result, it is very difficult to implement programs that use more than one locale.<ref>{{cite web | title=The Standard C Locale and the Standard C++ Locales | url=http://www.math.hkbu.edu.hk/parallel/pgi/doc/pgC++_lib/stdlibug/sta_9169.htm | publisher=Rogue Wave Software, Inc. | year=1996 | access-date=10 November 2011 | archive-date=19 February 2020 | archive-url=https://web.archive.org/web/20200219173855/http://www.math.hkbu.edu.hk/parallel/pgi/doc/pgC++_lib/stdlibug/sta_9169.htm | url-status=dead }}</ref>
 
The functions alter the behavior of printf/scanf/strtod which are often used to write saved data to a file or to other programs. The result is that a saved file in one locale will not be readable in another locale, or not be readable ''at all'' due to assumptions such as "numbers end at comma characters". Most large-scale software forces the locale to "C" (or another fixed value) to work around these problems.
 
==Example==
<syntaxhighlight lang="c">
#include <stdio.h>
#include <stdlib.h>
Line 41 ⟶ 33:
 
int main(void)
{
{ /* Locale is set to "C" before this. This call sets it
to the "current locale" by reading environment variables: */
setlocale(LC_ALL, "");
Line 50 ⟶ 43:
currentlocale->currency_symbol);
 
return EXIT_SUCCESS;
}
</syntaxhighlight>
</source>
 
==See also==
*[[Locale (computer software)]]
 
==References==
{{Reflist}}
 
[[Category:C Standardstandard Librarylibrary]]
 
[[Category:C Standard Library]]
 
[[ru:Locale.h]]
[[uk:Locale.h]]