从资源DLL字符串表设置Unicode标题

时间:2014-11-11 20:10:56

标签: delphi dll unicode localization

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

字符串表中的一个片段:

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

以下是代码片段,说明了我使用Unicode字符时遇到的问题:

// 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)));

这是我的Delphi函数,用于从资源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。例如(英文,繁体中文,简体中文,法文):

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

1 个答案:

答案 0 :(得分:6)

我发现a reference from 2002表示您需要告诉资源编译器 .rc 文件是如何编码的。对于UTF-8,那是代码页65001,所以你要运行它:

brcc32 -c65001 lang.rc

然后,当然,您要从代码中的'lang.rc'指令中删除$R部分,因为您不再希望IDE调用资源编译器本身。

如果您的Delphi版本足够新,那么您可以保留完整的$R指令,而是在项目选项的resource-compiler configuration中设置-c65001选项。

仅通过查看就很难知道文件的编码。可以有许多有效的猜测。 -c选项已记录在案,但the documentation未提及何时需要使用它,或者当运行资源编译器时IDE使用的内容。 IDE可能只使用默认值,与brcc32.exe相同,后者是系统的默认ANSI代码页。