操作系统的语言本地化?

时间:2012-05-19 13:41:14

标签: c localization internationalization

我一直想知道Windows或Mac OS X等操作系统如何只需一次点击即可更改语言,所有消息框,按钮等都会发生变化。

如何实施这些机制?

由于

2 个答案:

答案 0 :(得分:5)

国际化的关键是避免硬编码用户将看到的任何文本。相反,调用一个检查语言环境的函数并适当地选择文本。

一个人为的例子:

// A "database" of the word "hello" in various languages.
struct _hello {
  char *language;
  char *word;
} hello[] = {
  { "English", "Hello" },
  { "French", "Bon jour" },
  { "Spanish", "Buenos dias" },
  { "Japanese", "Konnichiwa" },
  { null, null }
};

// Print, e.g. "Hello, Milo!"
void printHello(char *name) {
  printf("%s, %s!\n", say_hello(), name);
}

// Choose the word for "hello" in the appropriate language,
// as set by the (fictitious) environment variable LOCALE
char *say_hello() {
  // Search until we run out of languages.
  for (struct _hello *h = hello; h->language != null; ++h) {
    // Found the language, so return the corresponding word.
    if (strcmp(h->language, getenv(LOCALE)) == 0) {
      return h->word;
    }
  }
  // No language match, so default to the first one.
  return hello->word;
}

答案 1 :(得分:2)

在类UNIX系统上,消息为catalogs and stored in files。 以编程方式,C为国际化和本地化提供gettext()函数,为获取文化信息提供locale.h标题。

以下是here

的代码示例
#include <libintl.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
 setlocale( LC_ALL, "" );
 bindtextdomain( "hello", "/usr/share/locale" );
 textdomain( "hello" );
 printf( gettext( "Hello, world!\n" ) );
 exit(0);
}

在MS-Windows上,它使用MUI(Multilingual User Interface)。以C编程方式,您可以使用LoadString()函数。查看how to do