Retrieve the string index

All announcements, questions and issues related to the TsiLang Components Suite.
Post Reply
Ioan
Posts: 22
Joined: Tue Mar 14, 2006 8:32 pm
Location: Canada
Contact:

Retrieve the string index

Post by Ioan »

I have a weird question:

Is there any way to retrieve programmatically the identifier of a string (IDS_XXX) given one language string?

On other words, I have the English value of a string and I need to retrieve from the strings list the IDS_XXX index, so I can get the other language value.

Example:

IDS_70 is in English "word" and in French "mot". I read from my database the English value ("word") and I need to find the equivalent "mot".

Thanks,
Ioan
isiticov
Site Admin
Posts: 2383
Joined: Thu Nov 21, 2002 3:17 pm

Post by isiticov »

Hello,

There are 2 ways for this but both require a little hack.
1. First way will return the translation based on your word to translate:

Code: Select all

type
   TAccessLang = class(TsiCustomLang)
   end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  TAccessLang(siLang1).FLangStringsColl.SortID := 1; // assuming English is the first
  ShowMessage(TAccessLang(siLang1).FLangStringsColl.GetTranslationByLang('word', 2)); //assuming French is second
end;
Please note: you must not to use this way in case you use resourcestrings translation!
2. The second way is a bit slower but will work in most cases:

Code: Select all

type
   TAccessLang = class(TsiCustomLang)
   end;

function GetTranslationByLanguage(const AsiLang: TsiCustomLang; const BaseLang, ReturnLang: Integer; const Value: string): string;
var
   I: Integer;
begin
  Result := '';
  for I := 0 to TAccessLang(AsiLang).FStringsColl.Count - 1 do // iterating throught the translations
    if TAccessLang(AsiLang).FStringsColl[I].Values[BaseLang] = Value then
    begin
      Result := TAccessLang(AsiLang).FStringsColl[I].Values[ReturnLang];
      Exit;
    end;
end;
Hope this helps.
Best regards,
Igor Siticov.
Ioan
Posts: 22
Joined: Tue Mar 14, 2006 8:32 pm
Location: Canada
Contact:

Post by Ioan »

I tried both cases (I don't use resource strings) and the results are:

I'm using a siLangLinked component. The original string (in Language 1) is sItem.

1. It always returns the original string (for Language 1). I like this way but I don't have time to look for the fix.

Code: Select all

if MainForm.siLangDispatcher1.ActiveLanguage <> 1 then begin
  TAccessLang(siLangLinked1).FLangStringsColl.SortID := 1; 
  sItem := TAccessLang(siLangLinked1).FLangStringsColl.GetTranslationByLang(sItem, MainForm.siLangDispatcher1.ActiveLanguage); 
end;
2. It works OK, but with a small adjustment: I had to use BaseLang-1 and ReturnLang-1 in the function to get the right result:

Code: Select all

function GetTranslationByLanguage(const AsiLang: TsiCustomLang; const BaseLang, ReturnLang: Integer; const Value: string): string;
var
   I: Integer;
begin
  Result := '';
  for I := 0 to TAccessLang(AsiLang).FStringsColl.Count - 1 do // iterating throught the translations
    if TAccessLang(AsiLang).FStringsColl[I].Values[BaseLang-1] = Value then
    begin
      Result := TAccessLang(AsiLang).FStringsColl[I].Values[ReturnLang-1];
      Exit;
    end;
end;
................

if MainForm.siLangDispatcher1.ActiveLanguage <> 1 then begin
  sItem := GetTranslationByLanguage(siLangLinked1, 1, MainForm.siLangDispatcher1.ActiveLanguage, sItem);
end;
So, if is a quick fix for the first solution, I will use it, but for now I will go with the second one.

Thanks,
Ioan
isiticov
Site Admin
Posts: 2383
Joined: Thu Nov 21, 2002 3:17 pm

Post by isiticov »

For the case 1. you need to make a call:

Code: Select all

TAccessLang(siLang1).LoadStringsColl(TAccessLang(siLangLinked1).FLangStringsColl, 1);
after form is loaded. This will load internal strings collection for first language.

For the case 2. your fix is correct.
Best regards,
Igor Siticov.
Post Reply