离开 Delphi 开发已经有一段时间了。最近,一位老客户联系我,询问在 Windows 10(x86 和 x64)上使用旧版 Delphi 7 应用程序时遇到的问题。该应用程序通常运行良好,但国际化未加载正确的字符串。
这是基本场景。
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
cszTestString
提供整数值:unit String_IDs;
interface
const
cszTestString = 1234;
“Strings.rc”是使用 Windows 10 SDK
rc.exe
编译生成“Strings.res”。
通过在项目 .dpr 文件中包含以下内容,将 .pas 和资源文件编译到 Delphi 应用程序中:
uses
String_IDs in 'source\String_IDs.pas';
{$R 'source\Strings.res'}
appLocale := (SUBLANG_ENGLISH_UK shl 10) or LANG_ENGLISH; // 2057
if (isDutch) then
appLocale := (SUBLANG_DUTCH shl 10) or LANG_DUTCH; // 1043
SetThreadLocale(appLocale);
// 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;
这是在 Windows XP (x86) VM 中测试的。一切都很完美。用英语来说,这三个都是“英语很好”。在荷兰语中,都是“En Nederlands is lekker”。
但我在 Windows 10(x86 和 x64)虚拟机中进行了测试,文本始终是“English is good”。已尝试更改兼容性设置但没有成功。
是否有其他一些 Windows API 可在 Windows 10 上使用,或者是否有针对此旧应用程序的特殊兼容性设置?
正如我在 Windows Vista 和更新版本的评论中提到的,当本地化资源存储在同一资源文件中时,您应该使用 SetThreadUILanguage 而不是 SetThreadLocale 来处理本地化资源,前提是它们标有特定的语言标识符。
还基于 SetThreadLocale 和 SetThreadUILanguage for Localization on Windows XP and Vista 文章,不建议使用 SetThreadUILanguage,因为它在 Windows XP 上可能不会达到预期的效果。您应该在 Windows XP 上继续使用 SetThreadLocale
为了了解如何检查应用程序执行的 Windows 版本,我建议您阅读