Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Function efficiency


This question is answered.


Permlink Replies: 1 - Last Post: Jan 19, 2017 10:34 AM Last Post By: Remy Lebeau (Te...
Fred Smith

Posts: 81
Registered: 12/4/15
Function efficiency  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 19, 2017 9:54 AM
Hi,
Working on a function which translates strings into different languages, how can I improve it ?
I have about 30 strings to translate into 5 languages.
How can I make this function more efficient ?
Thanks

function TranslateMyString(OriginalStr: string;  LangID: wideString): string;
begin
  result := OriginalStr; // Return original if no translation is available
 
  if OriginalStr ='Good morning' then
  begin
    if  LangID = '1036' then Result := 'Bonjour' 	  // French
    else if LangID= '1053' then Result := 'God morgon'  // Sweedish
  end
  else if OriginalStr = 'Good night' then
  begin
    if  LangID ='1036' then Result := 'Bonne nuit'       // French
    else if LangID='1053' then Result := 'Godnatt'     // Sweedish
  end;
end;
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Function efficiency
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 19, 2017 10:34 AM   in response to: Fred Smith in response to: Fred Smith
Fred wrote:

Working on a function which translates strings into different
languages, how can I improve it ?

Which Delphi version are you using? If you are using Delphi 2009 or later,
you really should use (Unicode)String instead of WideString for the LangID
parameter. That alone will improve performance a bit, as UnicodeString memory
management is vastly better over WideString's memory management. Or even
better, change the LangID parameter to an Integer instead, and then you can
use 'case' statements instead of 'if' statements.

I have about 30 strings to translate into 5 languages.
How can I make this function more efficient ?

Is there a reason why you are translating the strings in code, instead of
storing the original strings in resource strings and then using a separate
localization tool to translate them?

http://docwiki.embarcadero.com/RADStudio/en/Localization

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02