Delphi TStringList查找方法无法找到项目

Raf*_*ssi 3 delphi firemonkey

我正在编写自己的类来管理android/ios app的翻译,我已经制作了这段代码.我将解释下面的代码,但它非常容易和简短.

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.
Run Code Online (Sandbox Code Playgroud)

上面的代码使用如下:

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;
Run Code Online (Sandbox Code Playgroud)

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

我可以使用Locale[Index: string]返回a 的属性访问我添加的语言TLanguage,该类用于表示单个lang.最后使用localize方法我得到了我的翻译.


奇怪的是,我总是得到错误'Locale not found'.任何的想法?使用调试器我发现了这个:

在此输入图像描述

FLocaleList有项目,但我已经测试了这一点,我想我做错了什么上线71(在这里我使用查找功能).我错误地错过了索引吗?

Rem*_*eau 9

您的代码逻辑是向后的. Find()如果找到匹配则返回True,否则返回False.Items[]如果Find()返回False 则访问,如果返回True则引发异常.您需要notif声明中删除:

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;
Run Code Online (Sandbox Code Playgroud)

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

注意:  仅用于  Find排序列表.对于未排序的列表,请改用该  IndexOf 方法.

您的列表未排序,因为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;
Run Code Online (Sandbox Code Playgroud)

  • 在这种情况下,我根本不会使用`TStringList`.如果出现问题,太容易让`FLanguagesList`和`FLocaleList`不同步.我要么1)将一个`LocaleID`成员添加到`TLanguage`然后循环通过`FLanguagesList`寻找那个ID,或者2)将`FLanguagesList`改为`TDictionary <string,TLanguage>`而不是`TObjectList` (3认同)