Build multi-language support into your Linux application with catgets

Putting It All Together

Listing 2 shows how to use catgets in a sample C program. The program first needs to open the catalog file using catopen(). For every string the program needs to print, I first need to retrieve the string from the catalog using catgets(). When the program is done using the catalog, a call to the catclose() function closes it.

Listing 2

A Sample Program


Now when you compile and run the program, you will see the Klingon text nuqneH instead of the default text Hello.

$ gcc -o hello hello.o
$ ./hello

Extending the Program

In this article, I have described a simple example with a hard-coded path to the catalog file. To make this example more flexible, and to support multiple spoken languages, you can omit the path to the message catalog in the catopen() function call and allow the program to look for a catalog file in some location defined by the system (Listing 3).

Listing 3

Omitting the Path


With this change, when you compile and run the new program, you can let the NLSPATH environment variable determine where catopen() will find the message catalog file. The NLSPATH variable uses certain flags to stand in for other values, such as %N for "the file itself." Let's say you set the NLSPATH variable as NLSPATH=/path/to/messages/%N. When you run the program, it will look for the message file as /path/to/messages/

The Author

Jim Hall is an open source software advocate and developer, best known for usability testing in Gnome and as the founder and project coordinator of FreeDOS. At work, Jim is CEO of Hallmentum, an IT executive consulting company that provides hands-on IT Leadership training, workshops, and coaching.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Palm Responds to App Catalog Critics

    Palm has announced that it will simplify its developer program for the Linux-based Web OS behind its new Palm Pre smartphone. The device maker is thereby responding to hefty criticism about its App Catalog.

  • Decision Making Scripts

    The Bash shell uses different criteria to make decisions. Learn how to teach your shell scripts to make the right choice.

  • Hypermail

    Hypermail converts email messages to HTML and allows you to group your messages in tidy archives.

  • Beagle

    To find files, music, messages, and photos in a single search, try this desktop tool with the power of an Internet search engine.

  • QR Code Generators

    With the right tools, you can create your own QR code squares with information you want to share, for example, on a business card, in a letter, or on your website.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More