Delphi TStringList search method cannot find item

I am writing my own class to manage translation of android / ios application and I created this code. I'll explain the code below, but it's pretty easy and short.

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 in the list. }
 { if it 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.

      

The code used above is used like this:

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;

      

The class TLocalization

does all the work. As you can see, I create a variable a

, then add the language to the class (this is managed internally with a dictionary / string list).

I can access the languages ​​that I have added using a property Locale[Index: string]

that returns TLanguage

, the class I am using to specify one lang. In the end, using the localization method, I get the translation.


Oddly enough, I always get the error 'Locale not found'

. Any ideas? Using the debugger, I found the following:

enter image description here

FLocaleList

has elements, but I have checked this and I am guessing that I am doing something wrong on line 71 (where I am using the Find function). Perhaps I am wrong about the Index?

+3


source to share


1 answer


Your code logic is reversed. Find()

returns True if it finds a match, otherwise it returns False. You access Items[]

if Find()

returns False and throws an exception if it returns True. You need to remove not

in the instructions if

:

function TLocalization.getLang(Index: string): TLanguage;
var
  i: integer;
begin
  { I search the locale id (for example 'it') if it in the list. }
  { if it 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;

      

But more importantly, the documentation says: Find()



Note: Use only Find

with sorted lists
. For unsorted lists, use instead IndexOf

.

Your list is not sorted as the Sorted

default property is false. So use IndexOf()

instead:

function TLocalization.getLang(Index: string): TLanguage;
var
  i: integer;
begin
  { I search the locale id (for example 'it') if it in the list. }
  { if it 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;

      

+9


source







All Articles