実行時にすべての文字列をTStringListに入れて、アプリケーションに言語翻訳を実装しました。
procedure PopulateStringList;
begin
EnglishStringList.Append('CAN_T_FIND_FILE=It is not possible to find the file');
EnglishStringList.Append('DUMMY=Just a dummy record');
// total of 2000 record appended in the same way
EnglishStringList.Sorted := True; // Updated comment: this is USELESS!
end;
次に、次を使用して翻訳を取得します。
function GetTranslation(ResStr:String):String;
var
iIndex : Integer;
begin
iIndex := -1;
iIndex := EnglishStringList.IndexOfName(ResStr);
if iIndex >= 0 then
Result := EnglishStringList.ValueFromIndex[iIndex] else
Result := ResStr + ' (Translation N/A)';
end;
とにかく、このアプローチでは、レコードを見つけるのに約30マイクロ秒かかりますが、同じ結果を達成するためのより良い方法はありますか?
更新:将来の参考のために、提案されているようにTDictionaryを使用する新しい実装をここに記述します(Delphi 2009以降で動作します):
procedure PopulateStringList;
begin
EnglishDictionary := TDictionary<String, String>.Create;
EnglishDictionary.Add('CAN_T_FIND_FILE','It is not possible to find the file');
EnglishDictionary.Add('DUMMY','Just a dummy record');
// total of 2000 record appended in the same way
end;
function GetTranslation(ResStr:String):String;
var
ValueFound: Boolean;
begin
ValueFound:= EnglishDictionary.TryGetValue(ResStr, Result);
if not ValueFound then Result := Result + '(Trans N/A)';
end;
新しいGetTranslation関数は、最初のバージョンよりも1000倍高速に実行されます(私の2000サンプルレコードで)。