- 标签
- Lazarus
在之前的一篇文章中简单介绍了如何在 Lazarus 项目中启用多语言国际化。不过使用 DefaultTranslator
或 SetDefaultLang()
无法实现运行时动态切换 GUI 的语言。更准确地说,在程序中首次调用 LCLTranslator.SetDefaultLang()
,并将参数 ForceUpdate
设为 True
,是能够在运行时修改 GUI 语言的,但若再次调用便无效了。
查看 SetDefaultLang()
源代码:
pascalfunction SetDefaultLang(Lang: string; Dir: string = ''; LocaleFileName: string = ''; ForceUpdate: boolean = true): string;
{ Arguments:
Lang - language (e.g. 'ru', 'de', 'zh_CN'); empty argument is default language.
Dir - custom translation files subdirectory (e.g. 'mylng'); empty argument means searching only in predefined subdirectories.
LocaleFileName - custom translation file name; empty argument means that the name is the same as the one of executable.
ForceUpdate - true means forcing immediate interface update. Only should be set to false when the procedure is
called from unit Initialization section. User code normally should not specify it.
}
var
lcfn: string;
LocalTranslator: TUpdateTranslator;
i: integer;
begin
Result := '';
LocalTranslator := nil;
// search first po translation resources
try
lcfn := FindLocaleFileName('.po', Lang, Dir, LocaleFileName, Result);
if lcfn <> '' then
begin
Translations.TranslateResourceStrings(lcfn);
LocalTranslator := TPOTranslator.Create(lcfn);
end
else
begin
// try now with MO translation resources
lcfn := FindLocaleFileName('.mo', Lang, Dir, LocaleFileName, Result);
if lcfn <> '' then
begin
GetText.TranslateResourceStrings(UTF8ToSys(lcfn));
LocalTranslator := TDefaultTranslator.Create(lcfn);
end;
end;
except
Result := '';
lcfn := '';
end;
if lcfn<>'' then
TranslateLCLResourceStrings(Lang, lcfn);
if LocalTranslator<>nil then
begin
if Assigned(LRSTranslator) then
LRSTranslator.Free;
LRSTranslator := LocalTranslator;
// Do not update the translations when this function is called from within
// the unit initialization.
if ForceUpdate=true then
begin
for i := 0 to Screen.CustomFormCount-1 do
LocalTranslator.UpdateTranslation(Screen.CustomForms[i]);
for i := 0 to Screen.DataModuleCount-1 do
LocalTranslator.UpdateTranslation(Screen.DataModules[i]);
end;
end;
end;
该函数首先查找本地的 .po 或 .mo 翻译文件;然后,将程序的资源字符串翻译成本地语言;接下来,创建一个新的 TUpdateTranslator
对象替代全局对象。
……
Free Pascal 支持交叉编译,理论上是可以在 Windows 平台上编译 Linux 可执行文件的,但是官方没有提供相关文档,且不建议这么做。除了使用虚拟机外,在 Windows 平台上还可以使用 WSL 来交叉编译 Lazarus 项目。
……
Lazarus 是一个与 Delphi 兼容的跨平台 RAD 集成开发环境。和 Delphi 一样, Lazarus 可以拖放组件,快速开发 GUI 应用程序。它使用 Free Pascal 作为后端编译器。当前的 Lazarus 版本是 2.2.6 ,内置的 Free Pascal 编译器版本是 3.2.2 。
……