Displaying messages with an application program
This topic will discuss the items that must be included in your application programming.
- The CatalogFile_msg.h definition file created by the mkcatdefs or runcat command if you used symbolic identifiers in the message source file, or the limits.h and nl_types.h files if you did not use symbolic identifiers
- A call to initialize the locale environment
- A call to open a catalog
- A call to read a message
- A call to display a message
- A call to close the catalog
The following subroutines provide the services necessary for displaying program messages with the message facility:
- setlocale
- Sets the locale. Specify the LC_ALL environment variable in the call to the setlocale subroutine for the preferred message catalog language.
- catopen
- Opens a specified message catalog and returns a catalog descriptor, which you use to retrieve messages from the catalog.
- catgets
- Retrieves a message from a catalog after a successful call to the catopen subroutine.
- printf
- Converts, formats, and writes to the stdout (standard output) stream.
- catclose
- Closes a specified message catalog.
The following C program, hello
, illustrates opening the
hello.cat
catalog with the catopen subroutine, retrieving
messages from the catalog with the catgets subroutine, displaying the messages with the printf
subroutine, and closing the catalog with the catclose subroutine.
/* program: hello */
#include <nl_types.h>
#include <locale.h>
nl_catd catd;
main()
{
/* initialize the locale */
setlocale (LC_ALL, "");
/* open the catalog */
catd=catopen("hello.cat",NL_CAT_LOCALE);
printf(catgets(catd,1,1,"Hello World!"));
catclose(catd); /* close the catalog */
exit(0);
}
In the previous example, the catopen subroutine refers
to the hello.cat message catalog only by file name. Therefore,
you must make sure that the NLSPATH environment variable is
set correctly. If the message catalog is successfully opened by the catopen subroutine,
the catgets subroutine returns a pointer to the specified
message in the hello.cat
catalog. If the message
catalog is not found or the message does not exist in the catalog,
the catgets subroutine returns the Hello World!
default
string.