Next: , Previous: , Up: Preparing Program Sources   [Contents][Index]


4.2 Triggering gettext Operations

The initialization of locale data should be done with more or less the same code in every program, as demonstrated below:

int
main (int argc, char *argv[])
{
  …
  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);
  …
}

PACKAGE and LOCALEDIR should be provided either by config.h or by the Makefile. For now consult the gettext or hello sources for more information.

The use of LC_ALL might not be appropriate for you. LC_ALL includes all locale categories and especially LC_CTYPE. This latter category is responsible for determining character classes with the isalnum etc. functions from ctype.h which could especially for programs, which process some kind of input language, be wrong. For example this would mean that a source code using the ç (c-cedilla character) is runnable in France but not in the U.S.

Some systems also have problems with parsing numbers using the scanf functions if an other but the LC_ALL locale category is used. The standards say that additional formats but the one known in the "C" locale might be recognized. But some systems seem to reject numbers in the "C" locale format. In some situation, it might also be a problem with the notation itself which makes it impossible to recognize whether the number is in the "C" locale or the local format. This can happen if thousands separator characters are used. Some locales define this character according to the national conventions to '.' which is the same character used in the "C" locale to denote the decimal point.

So it is sometimes necessary to replace the LC_ALL line in the code above by a sequence of setlocale lines

{
  …
  setlocale (LC_CTYPE, "");
  setlocale (LC_MESSAGES, "");
  …
}

On all POSIX conformant systems the locale categories LC_CTYPE, LC_MESSAGES, LC_COLLATE, LC_MONETARY, LC_NUMERIC, and LC_TIME are available. On some systems which are only ISO C compliant, LC_MESSAGES is missing, but a substitute for it is defined in GNU gettext’s <libintl.h> and in GNU gnulib’s <locale.h>.

Note that changing the LC_CTYPE also affects the functions declared in the <ctype.h> standard header and some functions declared in the <string.h> and <stdlib.h> standard headers. If this is not desirable in your application (for example in a compiler’s parser), you can use a set of substitute functions which hardwire the C locale, such as found in the modules ‘c-ctype’, ‘c-strcase’, ‘c-strcasestr’, ‘c-strtod’, ‘c-strtold’ in the GNU gnulib source distribution.

It is also possible to switch the locale forth and back between the environment dependent locale and the C locale, but this approach is normally avoided because a setlocale call is expensive, because it is tedious to determine the places where a locale switch is needed in a large program’s source, and because switching a locale is not multithread-safe.


Next: Preparing Translatable Strings, Previous: Importing the gettext declaration, Up: Preparing Program Sources   [Contents][Index]