0

我一直想知道诸如 Windows 或 Mac OS X 之类的操作系统如何只需单击一次即可更改语言,突然间,所有消息框、按钮等都发生了变化。

这些机制是如何实现的?

谢谢

4

2 回答 2

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;
}
于 2012-05-19T13:55:40.613 回答
2

在类 UNIX 系统上,消息是目录并存储在文件中。以编程方式,C 提供了gettext()国际化和本地化功能以及用于获取文化信息的locale.h标头。

这是在此处拍摄的代码示例

#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

于 2012-05-19T14:46:00.333 回答