Delphi 7 应用程序未在 Windows 10 上加载 STRINGTABLE 资源

问题描述 投票:0回答:1

离开 Delphi 开发已经有一段时间了。最近,一位老客户联系我,询问在 Windows 10(x86 和 x64)上使用旧版 Delphi 7 应用程序时遇到的问题。该应用程序通常运行良好,但国际化未加载正确的字符串。

这是基本场景。

  1. 从翻译电子表格生成名为“String.rc”的 ASCII 文件:
Language LANG_ENGLISH, SUBLANG_ENGLISH_UK
STRINGTABLE
BEGIN
    cszTestString "English is good"
END

Language LANG_DUTCH, SUBLANG_DUTCH
STRINGTABLE
BEGIN
    cszTestString "En Nederlands is lekker"
END
  1. 生成一个名为“String_IDs.pas”的单独文件,为
    cszTestString
    提供整数值:
unit String_IDs;
interface
const
    cszTestString = 1234;
  1. “Strings.rc”是使用 Windows 10 SDK

    rc.exe
    编译生成“Strings.res”。

  2. 通过在项目 .dpr 文件中包含以下内容,将 .pas 和资源文件编译到 Delphi 应用程序中:

    uses
        String_IDs in 'source\String_IDs.pas';

    {$R 'source\Strings.res'}
  1. 在代码中,使用以下命令将语言环境设置为英语或荷兰语:
    appLocale := (SUBLANG_ENGLISH_UK shl 10) or LANG_ENGLISH; // 2057
    if (isDutch) then
        appLocale := (SUBLANG_DUTCH shl 10) or LANG_DUTCH; // 1043
  1. 使用以下方法设置线程区域设置:
    SetThreadLocale(appLocale);
  1. 测试以三种不同方式加载字符串资源:
    // A: Using LoadStr
    stringA := LoadStr(cszTestString);

    // B: Using LoadString
    if (LoadString(0, cszTestString, szBuffer, Length(szBuffer)) > 0) then
        stringB := szBuffer;
    
    // C: Using LoadString in a different format
    try
    pstrBuffer := AllocMem(MAX_PATH);
    if (LoadString(0, cszTestString, pstrBuffer, MAX_PATH) > 0) then
        stringC := StrPas(pstrBuffer);
    finally
        FreeMem(pstrBuffer, MAX_PATH);
    end;
  1. 这是在 Windows XP (x86) VM 中测试的。一切都很完美。用英语来说,这三个都是“英语很好”。在荷兰语中,都是“En Nederlands is lekker”。

  2. 但我在 Windows 10(x86 和 x64)虚拟机中进行了测试,文本始终是“English is good”。已尝试更改兼容性设置但没有成功。

是否有其他一些 Windows API 可在 Windows 10 上使用,或者是否有针对此旧应用程序的特殊兼容性设置?

delphi localization delphi-7
1个回答
0
投票

正如我在 Windows Vista 和更新版本的评论中提到的,当本地化资源存储在同一资源文件中时,您应该使用 SetThreadUILanguage 而不是 SetThreadLocale 来处理本地化资源,前提是它们标有特定的语言标识符。

还基于 SetThreadLocale 和 SetThreadUILanguage for Localization on Windows XP and Vista 文章,不建议使用 SetThreadUILanguage,因为它在 Windows XP 上可能不会达到预期的效果。您应该在 Windows XP 上继续使用 SetThreadLocale

为了了解如何检查应用程序执行的 Windows 版本,我建议您阅读

获取 Windows 版本?

© www.soinside.com 2019 - 2024. All rights reserved.