[Gammu]setlocale和bindtextdomain函数的用法

这两个函数都是linux实现i18n需要用到的。
其中setlocale用来设定locale,比如LC_ALL,LC_CTYPE等,一般用法是:setlocale(LC_ALL, "")
这用来设置LC_ALL,第二个参数是一个空字符串表示使用环境变量中定义的LC_ALL的值。

然后就是用bindtextdomain,比如:bindtextdomain("libgammu", LOCALE_PATH);
Linux i18n中,每个资源文件是.mo文件,这个文件是二进制的,用工具针对一个文本生成(作成二进制应该是考虑了性能)。所以,上面的代码 中,LOCALE_PATH指定的就是寻找mo文件的一个路径,一般的,如果调用了上面的代码,那么gettext library就会在这个地方寻找mo文件:
/usr/share/locale-langpack/<locale>/LC_MESSAGES/libgammu.mo

此 外,还有textdomain函数,比如:textdomain("gammu"); 这个函数的作用是设置当前需要使用的text domain(这些text domain之前都要使用bindtextdomain来设定好以便能让gettext library找到那个mo文件)。如果我们的程序用到了多个mo文件,那就需要bindtextdomain多次,然后用textdomain来指定当 前需要使用哪个。比如gammu中,gammu命令行程序使用的就是gammu这个text domain,libgammu这个库使用的就是libgammu.mo

附上gammu中使用这两个函数的代码:
Code: Select all
#ifdef GETTEXTLIBS_FOUND
void GSM_InitLocales(const char *path) {
   /* setlocale, locale is "" means all locale settings are
      depend on the environment setting. */
   setlocale(LC_ALL, "");
   if (path == NULL || strlen(path) == 0) {
#if defined(LOCALE_PATH)
      bindtextdomain("libgammu", LOCALE_PATH);
#else
      bindtextdomain("libgammu", ".");
#endif
   } else {
      /* bindtextdomain, libgammu is the catalog, path is the
         message file root path. E.g: if path is /usr/share/locale-langpack,
         then all messages represented by gettext will by found in:
         /usr/share/locale-langpack/<locale>/LC_MESSAGES/libgammu.mo
         Refer to manual of bindtextdomain for more details. */
      bindtextdomain("libgammu", path);
   }
}
#else
void GSM_InitLocales(const char UNUSED *path) {
   setlocale(LC_ALL, "");
}
#endif
posted @ 2011-03-26 10:00  super119  阅读(2854)  评论(0编辑  收藏  举报