United States-English |
|
|
HP-UX Reference > Ssetlocale(3C)HP-UX 11i Version 3: February 2007 |
|
NAMEsetlocale(), getlocale() — set and get the locale of a program DESCRIPTIONThe setlocale() function sets, queries, or restores the aspect of a program's locale that is specified by the category argument. A program's locale refers to those areas of the program's Native Language Support (NLS) environment for which the following values of category have been defined:
All nl_langinfo(3C) items are affected by the setting of one of the categories listed above. See langinfo(5) to determine which categories affect each item. The value of the locale argument determines the action taken by setlocale(). locale is a pointer to a character string. Note that while setlocale() can be called concurrently from multiple threads, the locale data structures that get updated are not protected against reads by other threads. Hence it is up to the application developer to ensure proper synchronization when changing locales. Setting the Locale of a ProgramTo set the program's locale for category, setlocale() accepts one of the following values as the locale argument: locale name, C, POSIX, or "" (the empty string). The actions prescribed by these values are as follows:
Querying the Locale of a Programsetlocale() queries the current NLS environment pertaining to category, if the value of locale is NULL. The query operation does not change the environment. The purpose of performing a query is to save that aspect of the user's current NLS environment associated with category, in the value returned by setlocale(), such that it can be restored with a subsequent call to setlocale(). Restoring the Locale of a ProgramTo restore a category within the program locale, a setlocale() call is made with the same category argument and the return string of the previous setlocale() call given as the locale argument. getlocale() returns a pointer to a locale_data structure (see /usr/include/locale.h). The members of the locale_data structure contain information about the setting of each setlocale() category. type determines what information is contained in the locale_data structure. The only supported value of type is:
RETURN VALUEIf the pointer to a string is given for locale and the selection can be honored, the setlocale() function returns a pointer to the string associated with the specified category for the new locale. The maximum length of this string is LC_BUFSIZ bytes (see <locale.h>). If the selection cannot be honored, the setlocale() function returns a null pointer and the program's locale is not changed. A null pointer for locale causes setlocale() to return a string associated with the category for the program's current locale. The string returned by setlocale() is such that a subsequent call with that string as the locale argument and its associated category restores that part of the program's locale. ERRORSIf a language name given through the locale argument does not identify a valid language name, or if the language name is more than 256 characters, or if the language is not available on the system (see lang(5)), then a null pointer is returned and the program's locale is not changed. The same behavior occurs when the call: setlocale(LC_ALL, ""); is made and any category related environment variable in the user's environment identifies an invalid language name or a language that is not available on the system. If the category argument is not a defined category value, a null pointer is returned and the program's locale is not changed. setlocale() returns a string that reflects the current setting of that aspect of the NLS environment corresponding to the category argument. If this return string is used in a subsequent setlocale() call and the category arguments of the two calls do not match, the locale remains unchanged and a null pointer is returned. EXAMPLESTo set a program's entire locale based on the language requirements specified via the user's environment variables: setlocale(LC_ALL, ""); If in the example the user's environment variables were set as follows: LANG ="de_DE.iso88591" LC_COLLATE ="es_ES.iso88591" LC_MONETARY ="" LC_TIME ="en_US.iso88591" the LC_CTYPE, LC_MONETARY, and LC_NUMERIC category items would be set to correspond to the de_DE.iso88591 language definition, the LC_COLLATE category items would be set to correspond to the es_ES.iso88591 language definition for collation and the LC_TIME category items would be set corresponding to the en_US.iso88591 language definition. Using the same example, if the following call was made: struct locale_data *locale_info=getlocale(LOCALE_STATUS); the contents of *locale_info would be: locale_info->LC_ALL_D="de_DE.iso88591" locale_info->LC_COLLATE_D="es_ES.iso88591" locale_info->LC_CTYPE_D="de_DE.iso88591" locale_info->LC_MESSAGES_D="de_DE.iso88591" locale_info->LC_MONETARY_D="de_DE.iso88591" locale_info->LC_NUMERIC_D="de_DE.iso88591" locale_info->LC_TIME_D="en_US.iso88591" The next example shows the precedence of the LC_ALL environment variable: setlocale(LC_ALL, ""); with the following settings in the user's environment: LANG=de_DE.iso88591 LC_ALL=fr_FR.iso88591 All categories will be loaded with fr_FR.iso88591. Another example showing the precedence of the LC_ALL environment variable: setlocale(LC_CTYPE, ""); with the following settings in the user's environment: LANG=tr_TR.iso88599 LC_ALL=da_DK.iso88591 LC_CTYPE=ru_RU.iso88595 All categories will be loaded with da_DK.iso88591. Another example with the LC_ALL environment variable: setlocale(LC_TIME, "pl_PL.iso88592"); with the following settings in the user's environment: LANG=it_IT.iso88591 LC_ALL=nl_NL.iso88591 The LC_TIME category will be set to pl_PL.iso88592, but all other categories will be set to nl_NL.iso88591. To set the date/time formats to fr_FR.iso88591: setlocale(LC_TIME, "fr_FR.iso88591"); To set the collating sequence to the C locale: setlocale(LC_COLLATE, "C"); To set monetary handling to the value of the user's LC_MONETARY environment variable: setlocale(LC_MONETARY, ""); Note that if the LC_MONETARY environment variable is not set or is empty, the value of the user's LANG environment variable is used. To query a user's locale: char *ch = setlocale(LC_ALL, NULL); To restore the locale saved in the above example: setlocale(LC_ALL, ch); To query only the part of the user's locale pertaining to the LC_NUMERIC category: char *ch = setlocale(LC_NUMERIC, NULL); To restore the LC_NUMERIC category of the user's locale saved in the above example: setlocale(LC_NUMERIC, ch); WARNINGSThe format of the return string from setlocale() is implementation specific, is not standardized across vendor's platforms, and is subject to change in future releases. The return string is valid only for the life of the process invoking the setlocale() and should only be used for restoring a previously stored locale setting within the same process. Using getenv() as the locale argument is not recommended. An example of such incorrect usage is: setlocale(LC_ALL, getenv("LANG")); getenv() returns a character string which can be a language name, an empty string, or a null pointer; depending on the setting of the user's LANG environment variable. Each of these values as the locale argument define a specific action to be taken by setlocale(). Therefore the action taken by setlocale() depends upon the value returned from the getenv() call. To ensure that setlocale() sets the program's locale based upon the setting of the user's environment variables the following usage is recommended: setlocale(LC_ALL, ""); The value returned by setlocale() points to an area that is overwritten during the next call to setlocale(). Be sure to copy these values to another area if they are to be used after a subsequent setlocale() call. getlocale() is an HP proprietary interface, which will be obsoleted in a future release, and is not portable to other vendor's platforms. The structure returned through a call to getlocale() is overwritten during the next call to getlocale(). Be sure to save these values if they are to be used after a subsequent getlocale() call. It should be noted that the locale state is common to all threads within a process. getlocale_r() and setlocale_r() are obsolescent interfaces supported for compatibility with existing DCE applications. New multithreaded applications should use getlocale() and setlocale(). Any program that calls setlocale() before catopen() with the oflag parameter set to NL_CAT_LOCALE may behave differently in this release than in previous releases because of the addition of LC_MESSAGES to XPG4. In the past, catopen() was directed to the desired language by LANG. Now, catopen() with the oflag parameter set to NL_CAT_LOCALE, is controlled by LC_MESSAGES. setlocale() can modify the LC_MESSAGES category. For example, if the environment variables are set as follows: LC_MESSAGES="fr_FR.iso88591" and the following call to setlocale() is made: setlocale(LC_ALL, "de_DE.iso88591"); followed by a call to catopen(), then catopen() will open the message catalogs for de_DE.iso88591 rather than fr_FR.iso88591. If you use setlocale() and compile/link your application archive on PA-RISC systems, please note that setlocale() has a dependency on libdld.sl that will require a change to the compile/link command. Compile: cc -Wl,-a,archive -Wl,-E -Wl,+n -l:libdld.sl -o outfile source Or compile with CCOPTS and LDOPTS: export CCOPTS="-Wl,-a,archive options -Wl,-E -l:libdld.sl" export LDOPTS="options -E +n -l:libdld.sl" cc -o outfile source The option -Wl,-a,archive is positionally dependent and should occur at the beginning of the compile line. For optimum compatibility in future releases, avoid using archive libc with other shared libraries except for libdld.sl as needed above. SEE ALSOlocale(1), localedef(1M), conv(3C), ctype(3C), ecvt(3C), getdate(3C), multibyte(3C), nl_langinfo(3C), regcomp(3C), string(3C), perror(3C), strfmon(3C), strftime(3C), string(3C), strptime(3C), strtod(3C), wcsftime(3C), wcstring(3C), printf(3S), scanf(3S), vprintf(3S), wconv(3C), wctype(3C), wcstod(3C), wcstol(3C), environ(5), lang(5), langinfo(5), thread_safety(5). |
Printable version | ||
|