ILE C/C++ Programmer's Guide

Using Environment Variables to Set the Active Locale

A program's active locale is set either implicitly at program startup, as described above, or explicitly by a call to setlocale(). The setlocale() function takes two arguments: an integer representing the locale category whose values are needed for the active locale, and the name of the locale from which the values are to be taken. The name of the locale can be any of the following:

When the locale argument of setlocale() is specified as a null string (""), setlocale() sets the active locale according to the environment variables defined for the job in which the program is running. You can create environment variables that have the same names as the locale categories and specify the locale to be associated with each environment variable. The LANG environment variable is automatically created during job initiation when you specify a locale path name for the LOCALE parameter in your user profile or for the QLOCALE system value.

When a program calls setlocale(category, ""), the locale-related environment variables defined in the current job are checked to find the locale name or names to be used for the specified category. The locale name is chosen according to the first of the following conditions that applies:

  1. If the environment variable LC_ALL is defined and is not null, the value of LC_ALL is used for the specified category. If the specified category is LC_ALL, that value is applied to all categories.
  2. If the environment variable for the category is defined and is not null, then the value that is specified for the environment variable is used. For the LC_ALL category, if individual environment variables (for example, LC_CTYPE, LC_MONETARY, and so on) are defined and are not null, then their values are used for the categories that correspond to the environment variables. This could result in the locale information for each category that is retrieved from a different locale object.
  3. If the environment variable LANG is defined and is not null, the value of the LANG environment variable is used.
  4. If no non-null environment variable is present to supply a locale value, the default C locale is used.

If the locale specified for the environment variable is found to be invalid or non-existent, setlocale() returns NULL and the program's active locale remains unchanged.

For setlocale(LC_ALL, ""), if the locale names found identify valid locales on the system, setlocale() returns a string naming the locale associated with each locale category. Otherwise, setlocale() returns NULL, and the program's locale remains unchanged.


[ Top of Page | Previous Page | Next Page | Table of Contents ]