在 Oracle Solaris 中进行应用程序国际化和本地化

退出打印视图

更新时间: 2014 年 7 月
 
 

关于国际化

Internationalization 一词通常缩写为 i18n,因为字母 in 之间有 18 个字母。尽管国际化一词有多种不同的定义,但本书中指的是使程序通用而灵活,以便满足全球市场的要求。

国际化的一部分是使产品可本地化,以便翻译应用程序的用户界面 (user interface, UI) 时可最大程度减少对应用程序的源代码的更改。

例如,假设某 C 程序中的一条消息文本的编码方式如下所示:

    /* This is not internationalized code */
    printf("This message needs internationalization.");

可按以下方式编写借助于 gettext 命令外化到消息目录中的消息:

    /* This is internationalized code */
    printf("%s", gettext("This message is now internationalized."));

国际化的另一个重要组成部分是允许在不更改源代码的情况下处理属于不同语言环境的数据。

例如,假设您必须对一个字符串数组进行排序。如未进行国际化,则源代码将如下所示:

    /* This is not internationalized code, since strcmp() compares byte
       values, producing an invalid sort order */ 

    if (strcmp((const char *)string[q], (const char *)string[p]) > 0) {
        temp = string[q];
        string[q] = string[p];
        string[p] = temp;
    }

如果要处理的字符仅来自英语语言环境,则此排序方法是可行的。但是,要使代码可处理来自不同语言环境的字符,您必须使用区分语言环境的函数。在使用区分语言环境的函数时,排序方法的编写方式如下所示:

    /* This is internationalized code, since strcoll() uses locale
       information to determine sort order */ 

    if (strcoll((const char *)string[q], (const char *)string[p]) > 0) {
        temp = string[q];
        string[q] = string[p];
        string[p] = temp;
    }