Locale (Units: A Unit Conversion Program and Scientific Calculator)

### 16.1 Locale ¶

A locale is a subset of a user’s environment that indicates the user’s language and country, and some attendant preferences, such as the formatting of dates. The `units` program attempts to determine the locale from the POSIX `setlocale` function; if this cannot be done, `units` examines the environment variables `LC_CTYPE` and `LANG`. On POSIX systems, a locale is of the form language`_`country, where language is the two-character code from ISO 639-1 and country is the two-character code from ISO 3166-1; language is lower case and country is upper case. For example, the POSIX locale for the United Kingdom is `en_GB`.

On systems running Microsoft Windows, the value returned by `setlocale` is different from that on POSIX systems; `units` attempts to map the Windows value to a POSIX value by means of a table in the file `locale_map.txt` in the same directory as the other data files. The file includes entries for many combinations of language and country, and can be extended to include other combinations. The `locale_map.txt` file comprises two tab-separated columns; each entry is of the form

    Windows-locale   POSIX-locale

where POSIX-locale is as described above, and Windows-locale typically spells out both the language and country. For example, the entry for the United States is

    English_United States   en_US

You can force `units` to run in a desired locale by using the `-l` option.

In order to create unit definitions for a particular locale you begin a block of definitions in a unit datafile with ‘`!locale`’ followed by a locale name. The ‘`!`’ must be the first character on the line. The `units` program reads the following definitions only if the current locale matches. You end the block of localized units with ‘`!endlocale`’. Here is an example, which defines the British gallon.

    !locale en_GB
    gallon       4.54609 liter
    !endlocale