Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Library: Windows API / MS.NET Framework API (https://www.delphipraxis.net/20-library-windows-api-ms-net-framework-api/)
-   -   Delphi LoadString endlich richtig gut. (https://www.delphipraxis.net/99255-loadstring-endlich-richtig-gut.html)

Dezipaitor 9. Sep 2007 15:05


LoadString endlich richtig gut.
 
Der folgende Quelltext ersetzt die API-Funktion LoadString.
Der Vorteil meiner Funktion liegt darin, dass man vorher keinen Speicher
reservieren muss, und dass String bzw WideString direkt verwendet werden kann.
Zusätzlich kann man die Sprache selbst bestimmen, um so auch andere Sprachversionen
über Resourcen darzustellen.

Für die LanguageId muss man in der Resource unterschiedliche Stringtabellen für
die jeweilige gewünschte Sprache erstellen und die Texte entsprechend übersetzen.

Die Verwendung sieht dann so aus.
Delphi-Quellcode:
var S : TString;
  S := LoadLocalizedString(50005, LANG_NEUTRAL, SUBLANG_NEUTRAL); //neutral
  S := LoadLocalizedString(50005, LANG_ENGLISH, SUBLANG_NEUTRAL); //englisch
Um die Benutzersprache zu ermitteln, verwendet man GetUserDefaultUILanguage.
Delphi-Quellcode:
S := LoadLocalizedString(50005, PRIMARYLANGID(GetUserDefaultUILanguage), SUBLANGID(GetUserDefaultUILanguage));
Die Originalquelle kommt von hier. Ich hab sie übersetzt
und für Delphi angepasst.

Ich verwende JEDI Api Lib für die API-Funktionen.

Delphi-Quellcode:


{$IFDEF UNICODE}
type
  TString = WideString;
  TPChar = PWideChar;
  TChar  = WideChar;
{$ELSE}
type
  TString = AnsiString;
  TPChar = PChar;
  TChar  = Char;
{$ENDIF UNICODE}

interface

{@Name loads a string from a resource using a language id.
@param Index defines the string index to be loaded.
@param(PrimaryLanguageId defines the primary language id.
use PRIMARYLANGID(GetUserDefaultUILanguage), SUBLANGID(GetUserDefaultUILanguage)
to get user language.)
@param(SubLanguageId defines the sub language id.)
@param Instance defines the location of the resource. Can be null to use current module.
@return Returns the resource string.
@raises EOSError if the resource could not be located.
}
function LoadLocalizedString(const Index : Cardinal;
  const PrimaryLanguageId, SubLanguageId : Word;
  Instance : HInst = 0) : TString;

implementation

function LoadLocalizedString(const Index : Cardinal; const PrimaryLanguageId, SubLanguageId : Word;
 Instance : HInst = 0) : TString;
var pS : PWideChar;
    Rsrc : HRSRC;
    res : HGLOBAL;
    S : TString;
    Len, i : Cardinal;
begin
  result := '';
  pS := nil;
  S := '#'+IntToStr((Index div 16)+1);
  if Instance = 0 then
    Instance := GetModuleHandle(nil);


  Rsrc := {$IFDEF UNICODE}FindResourceExW{$ELSE}FindResourceExA{$ENDIF}
    (Instance, TPChar(RT_STRING), TPChar(S), MAKELANGID(PrimaryLanguageId,SubLanguageId));
  if (Rsrc <> 0) then
  begin
    res := LoadResource(Instance, Rsrc);
    if (res <> 0) then
    begin
      pS := LockResource(res);
      if (pS <> nil) then
      begin
        i := 0;
        while i < (Index and 15) do
        begin
          Len := Integer(pS^); //get string length
          Inc(pS); //skip string length
          Inc(pS, Len); //skip string
          Inc(i);
        end;
        UnlockResource(Cardinal(pS));
      end;
      FreeResource(res);
    end;
  end
  else
    RaiseLastOSError;

  if (pS <> nil) then
  begin
    Len := Integer(pS^); //get string length
    Inc(pS);  //skip string length
    result := TString(pS);
    SetLength(result, Len);
  end;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 10:58 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz