gpt4 book ai didi

Delphi TStringList Find 方法找不到项目

转载 作者:行者123 更新时间:2023-12-03 15:42:56 29 4
gpt4 key购买 nike

我正在编写自己的类来管理 android/ios 应用程序的翻译,并且我已经生成了此代码。我将解释下面的代码,但它非常简单且简短。

unit Localization;

interface

uses
System.Classes, System.SysUtils, Generics.Collections, Vcl.Dialogs;

//this class represents a single language (Italian, French, German...)
type
TLanguage = class
private
FTranslationList: TDictionary<string, string>;
function localize(const aWordId: string): string;
public
constructor Create;
destructor Destroy; override;
//methods
procedure addWord(const aIndex, aWord: string);
procedure removeWord(const aIndex: string);
end;

//this is a "container", it gathers all the languages in one place
type
TLocalization = class
private
FLanguagesList: TObjectList<TLanguage>;
FLocaleList: TStringList;
function getLang(Index: string): TLanguage;
public
constructor Create;
destructor Destroy; override;
//methods
function localize(const aLocaleId: string; const aIndex: string): string;
procedure addLanguage(const localeId: string);
//property to manage the languages
property Locale[Index: string]: TLanguage read getLang;
property langCount: integer read getCount;
end;

implementation

{ TLocalization }

{ add a new language to the class. }
{the localeId is a symbol like 'it' that represents the Italian language}
procedure TLocalization.addLanguage(const localeId: string);
begin
//add the language to the languages container
FLanguagesList.Add(TLanguage.Create);
//add the representation of the language.
FLocaleList.Add(localeId);
end;

constructor TLocalization.Create;
begin
FLanguagesList := TObjectList<TLanguage>.Create;
FLocaleList := TStringList.Create;
end;

destructor TLocalization.Destroy;
begin
FLanguagesList.Free;
FLocaleList.Free;
inherited;
end;

//ERROR HERE
function TLocalization.getLang(Index: string): TLanguage;
var i: integer;
begin

{ I search the locale id (for example 'it') if it's in the list. }
{ if it's in the list, I return the respective TLanguage object}
if not( FLocaleList.Find(Index, i) ) then
Result := FLanguagesList.Items[i]
else
raise Exception.Create('Locale not found');

end;

function TLocalization.localize(const aLocaleId, aIndex: string): string;
var k: integer;
begin

k := 0;

if not( FLocaleList.Find(aLocaleId, k) ) then
raise Exception.Create('Locale not found.');

//ho trovato il locale, adesso basta cercare la parola
Result := FLanguagesList.Items[k].localize(aIndex);

end;

{ TLanguage }

procedure TLanguage.addWord(const aIndex, aWord: string);
begin
FTranslationList.Add(aIndex, aWord);
end;

constructor TLanguage.Create;
begin
FTranslationList := TDictionary<string, string>.Create;
end;

destructor TLanguage.Destroy;
begin
FTranslationList.Free;
inherited;
end;

function TLanguage.localize(const aWordId: string): string;
begin

try
Result := FTranslationList.Items[aWordId];
except
Result := 'Not found.';
end;

end;

procedure TLanguage.removeWord(const aIndex: string);
begin
FTranslationList.Remove(aIndex);
end;

end.

上面的代码使用如下:

var a: TLocalization;
begin
a := TLocalization.Create;

a.addLanguage('it');
a.addLanguage('cse');
a.Locale['it'].addWord('test', 'Ciao mondo!');
a.Locale['cse'].addWord('test', 'fadfa ea!');

ButtonSomething.Text := a.localize('it', test);

end;

TLocalization 类完成所有工作。正如您所看到的,我创建了变量 a,然后向类添加一种语言(这是使用字典/字符串列表在内部管理的)。

我可以使用 Locale[Index: string] 属性访问我添加的语言,该属性返回一个 TLanguage,这是一个我用来指示单个语言的类。最后通过 localize 方法我得到了翻译。

<小时/>

奇怪的是,我总是收到错误“未找到语言环境”。任何想法?使用调试器我发现了这一点:

enter image description here

FLocaleList 有项目,但我已经对此进行了测试,我猜想我在第 71 行(我使用 Find 函数的地方)做错了什么。我是否可能错误地传递了索引?

最佳答案

你的代码逻辑是倒退的。 Find() 如果找到匹配项,则返回 True,否则返回 False。如果 Find() 返回 False,则您正在访问 Items[],如果返回 True,则引发异常。您需要删除 if 语句中的 not:

function TLocalization.getLang(Index: string): TLanguage;
var
i: integer;
begin
{ I search the locale id (for example 'it') if it's in the list. }
{ if it's in the list, I return the respective TLanguage object}
if FLocaleList.Find(Index, i) then // <-- HERE
Result := FLanguagesList.Items[i]
else
raise Exception.Create('Locale not found');
end;

但是,更重要的是,Find() documentation说:

Note: Only use Find with sorted lists. For unsorted lists, use the IndexOf method instead.

您的列表未排序,如 Sorted属性默认为 false。因此,请使用 IndexOf() 来代替:

function TLocalization.getLang(Index: string): TLanguage;
var
i: integer;
begin
{ I search the locale id (for example 'it') if it's in the list. }
{ if it's in the list, I return the respective TLanguage object}
i := FLocaleList.IndexOf(Index);
if i <> -1 then
Result := FLanguagesList.Items[i]
else
raise Exception.Create('Locale not found');
end;

关于Delphi TStringList Find 方法找不到项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44400854/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com