字符串文字的本地化

发布于 2024-08-10 14:36:48 字数 216 浏览 9 评论 0 原文

我需要本地化来自编译器的错误消息。目前,所有错误消息都以英文字符串形式散布在整个源代码中。我们想将这些错误消息翻译成德语。解决这个问题的最佳方法是什么?保留字符串文字不变,并将 char* 映射到错误报告例程内的另一种语言,或者用描述性宏替换英语文字,例如ERR_UNDEFINED_LABEL_NAME 并在编译时将此映射到正确的字符串?

在类似的项目中如何处理这样的事情?

I need to localize error messages from a compiler. As it stands, all error messages are spread throughout the source code as string literals in English. We want to translate these error messages into German. What would be the best way to approach this? Leave the string literals as-is, and map the char* to another language inside the error reporting routine, or replace the english literals with a descriptive macro, eg. ERR_UNDEFINED_LABEL_NAME and have this map to the correct string at compile time?

How is such a thing approached in similar projects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

洛阳烟雨空心柳 2024-08-17 14:36:48

在 Windows 上,通常通过将字符串替换为整数常量,然后使用 LoadString 或类似方法从 DLL 或 EXE 中的资源获取它们。然后您就可以拥有多种语言的 DLL 和一个 EXE。

在 Unixy 系统上,我认为最典型的方法是 gettext。最终结果类似,但不是定义整数常量,而是将英文字符串文字包装在宏中,它会应用一些魔法将其转换为本地化字符串。

On Windows, typically this is done by replacing the string with integer constants, and then using LoadString or similar to get them from a resource in a DLL or EXE. Then you can have multiple language DLLs and a single EXE.

On Unixy systems I believe the most typical approach is gettext. The end result is similar, but instead of defining integer constants, you wrap your English string literals in a macro, and it will apply some magic to turn that into a localized string.

停顿的约定 2024-08-17 14:36:48

最灵活的方法是编译器从消息目录中读取翻译后的消息,并根据区域设置选择语言。这需要更改编译器以使用类似的工具
gettext

The most flexible way would be for the compiler to read the translated messages from message catalogs, with the choice of language being made according to the locale. This would require changing the compiler to use some tool like
gettext.

困倦 2024-08-17 14:36:48

只是一个快速的想法......

你可以让你的错误报告程序超载吗?假设您正在使用

printf("MESSAGE")

您可以以 "MESSAGE" 为输入的方式重载它,并将其哈希为德语的相应消息。

这行得通吗?

Just a quick thought...

Could you overload your error reporting routine? Say you are using

printf("MESSAGE")

You could overload it in a way that "MESSAGE" is the input, and you hash it to the corresponding message in German.

Could this work?

小女人ら 2024-08-17 14:36:48

您可以使用我的 CMsg() 和 CFMsg() 围绕 LoadString( ) API。它们使您可以更轻松地加载和格式化从资源中提取的字符串。

当然,appTranslator 是您翻译资源的最佳朋友;-)

免责声明:我appTranslator 的作者。

You could use my CMsg() and CFMsg() wrappers around the LoadString() API. They make your life easier to load and format the strings pulled out of the resources.

And of course, appTranslator is your best friend to translate your resources ;-)

Disclaimer: I'm the author of appTranslator.

凤舞天涯 2024-08-17 14:36:48

在 Windows 上,您可以使用资源编译器和 WinAPI 加载函数来获取本地化的字符串和其他资源。 FindResource()及其专门的衍生产品,例如 LoadString< /a>() 将根据用户当前的区域设置自动加载特定于语言的资源。 FindResourceEx()甚至允许您手动指定要检索的资源的语言版本。

为了在程序中启用此功能,您必须首先更改程序以编译资源文件 (.rc) 中的字符串,并使用 LoadString() 在运行时获取字符串,而不是使用文字字符串。然后,在资源文件中设置 STRINGTABLE,使用 语言 修饰符。然后,根据 MSDN 上描述的搜索顺序加载多语言资源:多个-语言资源

注意:如果您没有理由需要单个可执行文件,或者正在执行诸如在应用程序中使用用户选择的语言之类的操作,那么它可以让您更好地控制并减少在单独的语言中编译每种语言的混乱。 dll 并动态加载它们,而不是拥有一个大的单个资源文件并尝试动态切换区域设置。

下面是一个多语言 StringTable 资源文件的示例(即:strings.rc):

#define IDS_HELLOSTR     361

STRINGTABLE
  LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_CAN
  BEGIN
     IDS_HELLO, "Hello!"
  END

STRINGTABLE
   LANG_FRENCH, SUBLANG_NEUTRAL
   BEGIN
      IDS_HELLO, "Bonjour!"
   END

On Windows you can use the resource compiler and the WinAPI load functions to have localized strings and other resources. FindResource() and its specialized derivatives like LoadString() will automatically load language specific resources according to the user's current locale. FindResourceEx() even allows you to manually specify the language version of the resource you wish to retrieve.

In order to enable this in your program you must first change your program to compile the strings in an resource file(.rc) and use LoadString() to fetch the strings at runtime instead of using a literal string. Within the resource file you then setup multiple language versions of the STRINGTABLEs you use, with the LANGUAGE modifier. The multi-lingual resources are then loaded based on the search order described here on MSDN: Multiple-Language Resources

Note: If you have no reason to need a single executable, or are doing something like using a user selected language from within your app, it gives you more control and less confusion to compile each language in a seperate dll and load them dynamically rather than have a large single resource file and trying to dynamically switch locales.

Here is an example of a multiple language StringTable resource file (ie:strings.rc):

#define IDS_HELLOSTR     361

STRINGTABLE
  LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_CAN
  BEGIN
     IDS_HELLO, "Hello!"
  END

STRINGTABLE
   LANG_FRENCH, SUBLANG_NEUTRAL
   BEGIN
      IDS_HELLO, "Bonjour!"
   END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文