Don’t be a Stranger to the Locale

A major hurdle in computer science was getting disparate computers to speak with each other. Not verbally, of course, but by exchanging files in compatible file formats. After this task was ironed out, professionals in white lab coats realized the next step was massaging program output to match the user’s language and other local preferences.

For example, it’s common in the US to write a price as $1,240.99. It’s common in Europe to write the same price as €1.103,77 (though some countries may not use a dot after the thousands’ place). It’s the same value, but expressed by using different monetary symbols and separators for the thousands and decimal.

Computer scientists could have started a war over which presentation is correct, but they decided instead to follow-up the common file formats with locale settings. These are a slate of preferences for specific regions and languages. Programs that use these settings can easily modify output without requiring separate code for each area and language.

If you have a terminal window handy (in Windows 10, open Ubuntu bash as neither the command prompt nor Powershell offers this command), type locale to see various environment settings related to your location. Here’s the output I see:

$ locale
LANG="en_US.UTF-8"
LC_COLLATE="C"
LC_CTYPE="C"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL="C"

C is the C language locale, which is a common setting in Posix shells. You might also see something more specific, such as en_US.UTF-8, which is the code for American English using the UTF-8 character set.

Another way to see the locale settings for your computer system is to code the following C program:

#include <stdio.h>
#include <locale.h>

int main()
{
    char *old;

    old = setlocale(LC_ALL,"");
    printf("Old locale setting: %s\n",old);

    return(0);
}

This code uses the setlocale() function at Line 8. It’s prototyped in the locale.h header file, included at Line 2.

The setlocale() function has two arguments, an int representing a category and a string representing the new locale. In this code, the defined constant LC_ALL allows all locale settings to be modified at once. The empty string "" is specified to retain the native locale.

The function returns a pointer to a string that expresses the previous locale. Here’s sample output:

Old locale setting: C

Yes, the setlocale() function reads and resets the current locale unless you specify the empty string, as shown in Line 8. Or you can use the function to read the current locale, then use the returned string in a second setlocale() call to reset the things back.

Other locale functions allow your code to examine various items stored as part of the locale, such as the currency symbol or thousands separator. This topic is addressed in next week’s Lesson.

Leave a Reply