共通デスクトップ環境 プログラマーズ・ガイド (国際化対応編)

XPG4 メッセージ例

次の例はカタログからメッセージを取り出す方法を示しており、3 つのパートがあります。1 番目のパートはメッセージ・ソース・ファイルを示し、2 番目のパートはカタログ・ファイルを生成するために使う方法を示します。3 番目のパートは、そのカタログを使用したプログラム例です。

メッセージ・ソース・ファイル

メッセージ・カタログは次のように指定します。

example.msg ファイル
$quote “  
$ every message catalog should have a beginning set number.
$set 1 This is the set 1 of messages  
1 “Hello world\n”  
2 “Good Morning\n”  
3 “example: 1000.220 Read permission is denied for the file  
%s.\n“  
$set 2  
1 “Howdy\n”
カタログ・ファイルの生成

このファイルは、メッセージ・カタログ example.cat を生成するために gencat ユーティリティに次のように入力されます。

gencat example example.msg
プログラム内のカタログへのアクセス
#include <locale.h>  
#include <nl_types.h>  
char *MF_EXAMPLE = "example.cat"   

main()  
{
        nl_catd catd;
        int error;

        (void)setlocale(LC_ALL, “”);

        catd = catopen(MF_EXAMPLE, 0);
            /* Get the message number 1 from the first set.*/

        printf( catgets(catd,1,1,“Hello world\n”) );
            /* Get the message number 1 from the second set.*/

        printf( catgets(catd, 2, 1,“Howdy\n”) );
            /* Display an error message.*/

        printf( catgets(catd, 1, 4,“example: 100.220
                Permission is denied to read the file %s.\n“) ,
                MF_EXAMPLE);
        catclose(catd);  
}