Einzelnen Beitrag anzeigen

Rollo62

Registriert seit: 15. Mär 2007
4.208 Beiträge
 
Delphi 12 Athens
 
#4

AW: Ländername für ISO-Code auslesen

  Alt 10. Jun 2025, 17:55
Ich habe das mal mit KI zusammenfassen lassen, nicht sicher ob es funktioniert,
sieht aber ganz sinnvoll aus, zeigt zumindest die relevanten Funktionen.

Delphi-Quellcode:
unit CountryUtils;

interface

uses
  System.SysUtils;

/// <summary>
/// Gibt den im aktuellen Systemsprache lokalisierten Namen für den ISO-Ländercode zurück.
/// Beispiel: 'DE' → 'Deutschland' (wenn Systemsprache Deutsch).
/// </summary>
/// <param name="ACountryCode">ISO 3166-1 Alpha-2 Ländercode (z.B. 'DE', 'US')</param>
function GetCountryName(const ACountryCode: string): string;

implementation

uses
  {$IF defined(ANDROID)}
  Androidapi.JNI.Java.Util, Androidapi.Helpers;
  {$ELSEIF defined(IOS)}
  iOSapi.Foundation, Macapi.ObjectiveC, iOSapi.Helpers;
  {$ELSEIF defined(MSWINDOWS)}
  Winapi.Windows;
  {$ELSEIF defined(MACOS)}
  iOSapi.Foundation, Macapi.ObjectiveC, iOSapi.Helpers;
  {$ENDIF}

{$IF defined(ANDROID)}
function GetCountryNameAndroid(const ACountryCode: string): string;
var
  SysLocale, CountryLocale: JLocale;
begin
  SysLocale := TJLocale.JavaClass.getDefault;
  CountryLocale := TJLocale.JavaClass.init(
    StringToJString(''),
    StringToJString(ACountryCode.ToUpper)
  );
  Result := JStringToString(CountryLocale.getDisplayCountry(SysLocale));
end;
{$ENDIF}

{$IF defined(IOS) or defined(MACOS)}
function GetCountryNameApple(const ACountryCode: string): string;
var
  Locale: NSLocale;
  Name: NSString;
begin
  Locale := TNSLocale.Wrap(TNSLocale.OCClass.currentLocale);
  Name := Locale.displayNameForKeyValue(
    NSLocaleCountryCode,
    StrToNSStr(ACountryCode.ToUpper)
  );
  Result := NSStrToStr(Name);
end;
{$ENDIF}

{$IF defined(MSWINDOWS)}
function GetCountryNameWindows(const ACountryCode: string): string;
var
  LocaleName: string;
  Buf: PChar;
  Len: Integer;
begin
  LocaleName := 'und-' + ACountryCode.ToUpper;
  // Puffergröße ermitteln
  Len := GetLocaleInfoEx(PChar(LocaleName),
                        LOCALE_SLOCALIZEDCOUNTRYNAME,
                        nil, 0);
  if Len > 0 then
  begin
    GetMem(Buf, Len * SizeOf(Char));
    try
      if GetLocaleInfoEx(PChar(LocaleName),
                         LOCALE_SLOCALIZEDCOUNTRYNAME,
                         Buf, Len) > 0 then
        Result := Buf
      else
        Result := '';
    finally
      FreeMem(Buf);
    end;
  end
  else
    Result := '';
end;
{$ENDIF}

function GetCountryName(const ACountryCode: string): string;
begin
  if ACountryCode.Trim.IsEmpty then
    Exit('');
  {$IF defined(ANDROID)}
    Result := GetCountryNameAndroid(ACountryCode);
  {$ELSEIF defined(IOS) or defined(MACOS)}
    Result := GetCountryNameApple(ACountryCode);
  {$ELSEIF defined(MSWINDOWS)}
    Result := GetCountryNameWindows(ACountryCode);
  {$ELSE}
    Result := '';
  {$ENDIF}
end;

end.
  Mit Zitat antworten Zitat