SiComponents Home Page SiComponents Forums
Here you will be able to get help and share your experience
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Retrieve the string index

 
Post new topic   Reply to topic    SiComponents Forums Forum Index -> TsiLang Components Suite
View previous topic :: View next topic  
Author Message
Ioan



Joined: 14 Mar 2006
Posts: 22
Location: Canada

PostPosted: Tue Apr 04, 2006 8:43 pm    Post subject: Retrieve the string index Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
isiticov
Site Admin


Joined: 21 Nov 2002
Posts: 2097

PostPosted: Wed Apr 05, 2006 3:33 am    Post subject: Reply with quote

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:
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:
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.
Back to top
View user's profile Send private message
Ioan



Joined: 14 Mar 2006
Posts: 22
Location: Canada

PostPosted: Wed Apr 05, 2006 2:18 pm    Post subject: Reply with quote

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:

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:

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
Back to top
View user's profile Send private message Visit poster's website
isiticov
Site Admin


Joined: 21 Nov 2002
Posts: 2097

PostPosted: Wed Apr 05, 2006 3:34 pm    Post subject: Reply with quote

For the case 1. you need to make a call:
Code:
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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    SiComponents Forums Forum Index -> TsiLang Components Suite All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by p h p B B  © 2001, 2005 p h p B B  Group