从资源DLL字符串表设置Unicode字幕 [英] Setting Unicode Caption from Resource DLL String Table

查看:190
本文介绍了从资源DLL字符串表设置Unicode字幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的UI在Unicode字符显示不正确时遇到问题。我有一个资源仅DLL包含用于UI本地化的字符串表。我在Delphi XE3中创建一个DLL,只有一个只有DLL的项目(在DPR文件中只有 {$ R'lang.res''lang.rc'} ,并给出我 lang.dll )。我已经验证我的lang.rc文件是UTF-8格式与Windows CRLF换行符。当我从DLL加载字符串时,Unicode字符在接口上混乱。以下是一些细节。

I'm having trouble with Unicode characters being displayed incorrectly on my UI. I have a resource-only DLL containing a string table used for UI localization. I create the DLL in Delphi XE3 with a DLL-only project (just has {$R 'lang.res' 'lang.rc'} in the DPR file, and gives me lang.dll). I've verified that my lang.rc file is in UTF-8 format with Windows CRLF line breaks. When I load the strings from the DLL, Unicode characters are jumbled on the interface. Here are some details.

字符串表中的代码段:

STRINGTABLE
{
59,"180˚"
60,"90˚ CW"
61,"90˚ CCW"
}

以下是使用Unicode字符说明问题的代码片段:

Here are code snippets that illustrate the problem I'm having with Unicode characters:

// explicitly assigning the degrees character shows 180˚ properly
ImageMenu180Action.Caption := '180˚'; 

// getting the resource from the DLL shows some weird two-character string for the degrees character
ImageMenu90CWAction.Caption := TLangHelper.LoadStr(IDS_ImageMenuRotationCW90);

// OutputDebugString shows the degrees character in the debugger output correctly
OutputDebugString(PChar('IDS_ImageMenuRotationCW90: '+TLangHelper.LoadStr(IDS_ImageMenuRotationCW90)));

这是我从用于从资源DLL加载字符串的Delphi函数:

Here is my Delphi function used for loading strings from the resource DLL:

class function TLangHelper.LoadStr(ResourceId: Integer):String;
var
   Buff: String;
   L: Integer;
begin
  Result := '';
  if LangDllHandle = 0 then begin
    LangDllHandle := LoadLibrary(LANGUAGE_DLL_LOCATION);
    if LangDllHandle = 0 then begin
      ShowMessage('Error loading language localization resources.');
    end;
  end;
  if LangDllHandle <> 0 then begin
    L := 1024;
    SetLength(Buff, L+1);
    LoadString(LangDllHandle, ResourceId, PChar(Buff), L);
    Result := String(PChar(Buff));
  end;
end;

任何建议?

关于

对于汉字,我不得不在.rc文件中的字符串定义中添加前一个L,以便DLL编译识别它们作为Unicode。例如(英文,繁体中文,简体中文,法文):

For Chinese characters, I had to add a preceding L to the string definitions in the .rc file so that the DLL compilation recognized them as Unicode. For example (English, Chinese Traditional, Chinese Simplified, French):

STRINGTABLE
{
35,"Status Bar"
1035,L"狀態欄"
2035,L"状态栏"
3035,"Barre d'état"
}


推荐答案

我发现 2002年的引用,表明您需要告知资源编译器如何编辑 .rc 文件。对于UTF-8,这是代码页65001,所以你可以运行这个:

I found a reference from 2002 indicating that you need to tell the resource compiler how the .rc file is encoded. For UTF-8, that's code page 65001, so you'd run this:


brcc32 -c65001 lang.rc

然后,当然,你可以从$中删除'lang.rc' c $ c> $ R 指令,因为您不再希望IDE调用资源编译器本身。

Then, of course, you'd remove the 'lang.rc' part from the $R directive in your code because you no longer want the IDE to invoke the resource compiler itself.

如果您的Delphi版本是最新的,那么你可以保留完整的 $ R 指令,而是在 -c65001 选项项目选项的href =http://docwiki.embarcadero.com/RADStudio/en/Resource_Compiler_Options =noreferrer>资源编译器配置。

If your Delphi version is recent enough, then you can keep the full $R directive and instead set the -c65001 option in the resource-compiler configuration of your project options.

只要查看文件,就很难知道文件的编码。可以有很多有效的猜测。 -c 选项已被记录,但文档没有提及什么时候需要使用它,或者当 运行资源编译器时,IDE使用什么。 IDE可能只是使用默认值,与brcc32.exe相同,这是系统的默认ANSI代码页。

It's hard to know the encoding of a file just by looking at it. There can be many valid guesses. The -c option is documented, but the documentation doesn't mention when you'd need to use it, or what the IDE uses when it runs the resource compiler. The IDE probably just uses the default, the same as brcc32.exe, which is the system's default ANSI code page.

这篇关于从资源DLL字符串表设置Unicode字幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆