操作系统的语言本地化?

jma*_*erx 0 c localization internationalization

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

这些机制是如何实施的?

谢谢

Ada*_*iss 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;
}
Run Code Online (Sandbox Code Playgroud)