我试图显示附加到Resourcestring的常量数组的内容;但它无法正常运行(Showmessage应该显示“ Primavera”),但是在完成应用程序时会收到空白消息和异常。例如,示例代码在Lazurus中运行良好。我想念一些东西...
unit U_Translate;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
end;
Resourcestring
RS1 = 'Primavera'; RS2 = 'Verano'; RS3 = 'Otoño'; RS4 = 'Invierno';
Const
CEstacion: Array [1..4] of ^String = (@RS1,@RS2,@RS3,@RS4);
var Form1: TForm1;
implementation
$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
ShowMessage (CEstacion[1]^);
end;
end.
最佳答案
如果在const数组中使用资源字符串,则在单元初始化期间将初始化数组的值。那时,它们是从二进制资源加载的。它们为空的原因仅意味着您拥有一些翻译资源,而您尚未翻译那些资源字符串。
以这种方式使用资源字符串还意味着您不能只在运行时更改语言并更改值(除非您显式地重新初始化数组,但这需要大量显式的低级代码-请参见System._InitResStrings
)
另一种方法是像这样使用PResStringRec数组:
const
CEstacion: array [1..4] of PResStringRec = (@RS1,@RS2,@RS3,@RS4);
然后像这样调用它(当使用资源字符串时,编译器通常会为您插入对LoadResString的调用)
ShowMessage(LoadResString(CEstacion[1]));
作为语言扩展,我们需要的是能够声明
array of resourcestring
,编译器会将其翻译为PResStringRec
数组,并插入LoadResString
调用,就像普通资源字符串一样。
关于delphi - 使用指针和资源字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50562012/