通过调用 Windows API (GetUserPreferredUILanguages()
),我会得到一个字符串列表,其中每个字符串都是使用 null 字符 #0
分隔的。 我需要将此转换为 Delphi 字符串列表。 我开始编写代码手动循环遍历列表,查找 #0
字符。
有更智能的方法吗?
GetUserPreferredUILanguages
返回的 PWideChar
示例:
('e','n','-','U','S',#0,'f','r','-','F','R',#0,#0,...)
根据我在文档中所读的内容,因为当我在我的电脑上调用该函数时,它只返回一种语言,即'en-US'#0#0。
这是我目前的代码:
procedure GetWinLanguages(aList: TStringList);
var lCount, lSize: ULong;
lChars: array of WideChar;
lIndex, lLastIndex: integer;
begin
lSize := 1000;
SetLength(lChars, lSize);
GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, @lCount, @lChars[0], @lSize);
// untested quick solution to convert from lChars to aList
lIndex := 0;
lLastIndex := -1;
while (lIndex<=lSize) do
begin
while (lIndex<lSize) and (lChars[lIndex]<>#0) do
inc(lIndex);
if (lIndex-lLastIndex)>1 then
begin
// here: copy range lLastIndex to lIndex, convert to string and add to aList
lLastIndex := lIndex;
inc(lIndex);
end else
Break;
end;
end;
PS. 我正在使用 Delphi Berlin 进行 FMX 项目,系统是 Windows 10。