delphi – 将ISO 3166-1 alpha-2国家/地区代码转换为本地化的国家/地区名称

前端之家收集整理的这篇文章主要介绍了delphi – 将ISO 3166-1 alpha-2国家/地区代码转换为本地化的国家/地区名称前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法在不使用外部资源的情况下将两个字母的国家代码转换为可读的国家代码

例如DE – >德国,AD – >安道尔

如果我可以选择目标语言或使用系统语言会很好,因为我想用德语.

解决方法

正如@Uwe在评论中提到的,您可以使用 EnumSystemGeoIDGetGeoInfo功能.原则是,使用 EnumSystemGeoID函数,您将枚举地理位置标识符,并通过 GetGeoInfo函数查询枚举标识符的ISO 2字母国家/地区代码(信息类型 GEO_ISO2)是否等于您感兴趣的一个.如果是这样,那么您可以使用相同的函数(友好名称(信息类型 GEO_FRIENDLYNAME)或官方名称(信息类型 GEO_OFFICIALNAME))查询此标识符,返回结果并停止枚举.

下面是一个示例代码,可能会这样做(遗憾的是,枚举函数不支持传递自定义数据,因此我使用了全局记录变量来传递值):

  1. type
  2. TEnumData = record
  3. GeoCode: string;
  4. GeoName: string;
  5. Success: Boolean;
  6. end;
  7.  
  8. GEOID = type LONG;
  9. GEOTYPE = type DWORD;
  10. GEOCLASS = type DWORD;
  11.  
  12. SYSGEOTYPE = (
  13. GEO_NATION = $0001,GEO_LATITUDE = $0002,GEO_LONGITUDE = $0003,GEO_ISO2 = $0004,GEO_ISO3 = $0005,GEO_RFC1766 = $0006,GEO_LCID = $0007,GEO_FRIENDLYNAME= $0008,GEO_OFFICIALNAME= $0009,GEO_TIMEZONES = $000A,GEO_OFFICIALLANGUAGES = $000B,GEO_ISO_UN_NUMBER = $000C,GEO_PARENT = $000D
  14. );
  15.  
  16. SYSGEOCLASS = (
  17. GEOCLASS_NATION = 16,GEOCLASS_REGION = 14,GEOCLASS_ALL = 0
  18. );
  19.  
  20. GEO_ENUMPROC = function(GeoId: GEOID): BOOL; stdcall;
  21.  
  22. function EnumSystemGeoID(GeoClass: GEOCLASS;
  23. ParentGeoId: GEOID; lpGeoEnumProc: GEO_ENUMPROC): BOOL; stdcall;
  24. external kernel32 name 'EnumSystemGeoID';
  25. function GetGeoInfo(Location: GEOID; GeoType: GEOTYPE;
  26. lpGeoData: LPTSTR; cchData: Integer; LangId: LANGID): Integer; stdcall;
  27. external kernel32 name {$IFDEF UNICODE}'GetGeoInfoW'{$ELSE}'GetGeoInfoA'{$ENDIF};
  28.  
  29. implementation
  30.  
  31. var
  32. // I have used this global variable due to a lack of user data parameter for the callback function
  33. EnumData: TEnumData;
  34.  
  35. function TryGetGeoInfo(GeoId: GEOID; GeoType: GEOTYPE; out Value: string): Boolean;
  36. var
  37. Buffer: string;
  38. BufferLen: Integer;
  39. begin
  40. Result := False;
  41. BufferLen := GetGeoInfo(GeoId,GeoType,LPTSTR(Buffer),0);
  42. if BufferLen <> 0 then
  43. begin
  44. SetLength(Buffer,BufferLen);
  45. Result := GetGeoInfo(GeoId,BufferLen,0) <> 0;
  46. if Result then
  47. Value := Trim(Buffer);
  48. end;
  49. end;
  50.  
  51. function EnumGeoInfoProc(GeoId: GEOID): BOOL; stdcall;
  52. var
  53. S: string;
  54. begin
  55. Result := TryGetGeoInfo(GeoId,GEOTYPE(GEO_ISO2),S);
  56. if Result and (S = EnumData.GeoCode) then
  57. begin
  58. // stop the enumeration since we've found the country by its ISO code
  59. Result := False;
  60. // return the success flag and try to return the friendly name of the country to the
  61. // EnumData.GeoName record field; you can optionally query the GEO_OFFICIALNAME
  62. EnumData.Success := TryGetGeoInfo(GeoId,GEOTYPE(GEO_FRIENDLYNAME),EnumData.GeoName);
  63. end;
  64. end;
  65.  
  66. function TryGetCountryNameByISO2(const Code: string; out Name: string): Boolean;
  67. begin
  68. // here is the brainless part using global record variable (because the function used
  69. // here with its callback does not support passing user data); no,you cannot tune it
  70. // up by making the callback function nested
  71. EnumData.GeoCode := Code;
  72. EnumData.Success := False;
  73.  
  74. if not EnumSystemGeoID(GEOCLASS(GEOCLASS_NATION),EnumGeoInfoProc) then
  75. RaiseLastOSError;
  76.  
  77. Result := EnumData.Success;
  78. if Result then
  79. Name := EnumData.GeoName;
  80. end;

可能的用法

  1. var
  2. S: string;
  3. begin
  4. if TryGetCountryNameByISO2('DE',S) then
  5. ShowMessage(S);
  6. end;

猜你在找的Delphi相关文章