Delphi:通过LocaleID获取国家/地区代码

时间:2013-10-14 21:44:54

标签: delphi

我需要一种通过区域设置ID获取“en”或“uk”或“jp”等国家/地区代码的方法:

  • 英语 - 美国ID为1033 - > “en”(或“en-US”)
  • 日语id为1041 - > “JP”

像:

function GetCountryFromLoc(lcid: Integer): string;

我使用Delphi 7。

3 个答案:

答案 0 :(得分:6)

从Windows vista开始,您可以使用LCIDToLocaleName功能。

试试这个样本

{$APPTYPE CONSOLE}

uses
  Windows,
  SysUtils;

const
  LOCALE_NAME_MAX_LENGTH = 85;

function LCIDToLocaleName(Locale: LCID; lpName: LPWSTR; cchName: Integer;
  dwFlags: DWORD): Integer; stdcall;external kernel32 name 'LCIDToLocaleName';

var
   strNameBuffer : array [0..LOCALE_NAME_MAX_LENGTH-1] of WideChar;
begin
    if (LCIDToLocaleName(1033, strNameBuffer, LOCALE_NAME_MAX_LENGTH, 0) = 0) then
      RaiseLastOSError
    else
      Writeln(strNameBuffer);

    if (LCIDToLocaleName(1041, strNameBuffer, LOCALE_NAME_MAX_LENGTH, 0) = 0) then
      RaiseLastOSError
    else
      Writeln(strNameBuffer);

  Readln;
end.

在Windows Vista之前的系统上,您可以使用DownlevelLCIDToLocaleName功能。

这将返回

en-US
ja-JP

答案 1 :(得分:1)

检查GetLocaleInfo

  

受支持的最低客户端Windows 2000 Professional

function _GetCountryFlag(): string;
var
  Buffer : PChar;
  Size : integer;
begin
  Size := GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVCTRYNAME, nil, 0);
  GetMem(Buffer, Size);
  try
    GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVCTRYNAME, Buffer, Size);
    Result := Copy(Buffer,0,2); { return US / use  Result := Buffer to return USA }
  finally
    FreeMem(Buffer);
  end;
end;

参数LCType可以采用以下值(如Windows.pas中所示):

  LOCALE_NOUSEROVERRIDE       { do not use user overrides }
  LOCALE_USE_CP_ACP           { use the system ACP }
  LOCALE_ILANGUAGE            { language id }
  LOCALE_SLANGUAGE            { localized name of language }
  LOCALE_SENGLANGUAGE         { English name of language
  LOCALE_SABBREVLANGNAME      { abbreviated language name }
  LOCALE_SNATIVELANGNAME      { native name of language }
  LOCALE_ICOUNTRY             { country code }
  LOCALE_SCOUNTRY             { localized name of country }
  LOCALE_SENGCOUNTRY          { English name of country }
  LOCALE_SABBREVCTRYNAME      { abbreviated country name }
  LOCALE_SNATIVECTRYNAME      { native name of country }
  LOCALE_IDEFAULTLANGUAGE     { default language id }
  LOCALE_IDEFAULTCOUNTRY      { default country code }
  LOCALE_IDEFAULTCODEPAGE     { default oem code page }
  LOCALE_IDEFAULTANSICODEPAGE { default ansi code page }
  LOCALE_IDEFAULTMACCODEPAGE  { default mac code page }
  LOCALE_SLIST                { list item separator }
  LOCALE_IMEASURE             { 0 = metric, 1 = US }
  LOCALE_SDECIMAL             { decimal separator }
  LOCALE_STHOUSAND            { thousand separator }
  LOCALE_SGROUPING            { digit grouping }
  LOCALE_IDIGITS              { number of fractional digits }
  LOCALE_ILZERO               { leading zeros for decimal }
  LOCALE_INEGNUMBER           { negative number mode }
  LOCALE_SNATIVEDIGITS        { native ascii 0-9 }
  LOCALE_SCURRENCY            { local monetary symbol }
  LOCALE_SINTLSYMBOL          { intl monetary symbol }
  LOCALE_SMONDECIMALSEP       { monetary decimal separator }
  LOCALE_SMONTHOUSANDSEP      { monetary thousand separator }
  LOCALE_SMONGROUPING         { monetary grouping }
  LOCALE_ICURRDIGITS          { # local monetary digits }
  LOCALE_IINTLCURRDIGITS      { # intl monetary digits }
  LOCALE_ICURRENCY            { positive currency mode }
  LOCALE_INEGCURR             { negative currency mode }
  LOCALE_SDATE                { date separator }
  LOCALE_STIME                { time separator }
  LOCALE_SSHORTDATE           { short date format string }
  LOCALE_SLONGDATE            { long date format string }
  LOCALE_STIMEFORMAT          { time format string }
  LOCALE_IDATE                { short date format ordering }
  LOCALE_ILDATE               { long date format ordering }
  LOCALE_ITIME                { time format specifier }
  LOCALE_ITIMEMARKPOSN        { time marker position }
  LOCALE_ICENTURY             { century format specifier (short date) }
  LOCALE_ITLZERO              { leading zeros in time field }
  LOCALE_IDAYLZERO            { leading zeros in day field (short date) }
  LOCALE_IMONLZERO            { leading zeros in month field (short date) }
  LOCALE_S1159                { AM designator }
  LOCALE_S2359                { PM designator }
  LOCALE_ICALENDARTYPE        { type of calendar specifier }
  LOCALE_IOPTIONALCALENDAR    { additional calendar types specifier }
  LOCALE_IFIRSTDAYOFWEEK      { first day of week specifier }
  LOCALE_IFIRSTWEEKOFYEAR     { first week of year specifier }
  LOCALE_SDAYNAME1            { long name for Monday }
  LOCALE_SDAYNAME2            { long name for Tuesday }
  LOCALE_SDAYNAME3            { long name for Wednesday }
  LOCALE_SDAYNAME4            { long name for Thursday }
  LOCALE_SDAYNAME5            { long name for Friday }
  LOCALE_SDAYNAME6            { long name for Saturday }
  LOCALE_SDAYNAME7            { long name for Sunday }
  LOCALE_SABBREVDAYNAME1      { abbreviated name for Monday }
  LOCALE_SABBREVDAYNAME2      { abbreviated name for Tuesday }
  LOCALE_SABBREVDAYNAME3      { abbreviated name for Wednesday }
  LOCALE_SABBREVDAYNAME4      { abbreviated name for Thursday }
  LOCALE_SABBREVDAYNAME5      { abbreviated name for Friday }
  LOCALE_SABBREVDAYNAME6      { abbreviated name for Saturday }
  LOCALE_SABBREVDAYNAME7      { abbreviated name for Sunday }
  LOCALE_SMONTHNAME1          { long name for January }
  LOCALE_SMONTHNAME2          { long name for February }
  LOCALE_SMONTHNAME3          { long name for March }
  LOCALE_SMONTHNAME4          { long name for April }
  LOCALE_SMONTHNAME5          { long name for May }
  LOCALE_SMONTHNAME6          { long name for June }
  LOCALE_SMONTHNAME7          { long name for July }
  LOCALE_SMONTHNAME8          { long name for August }
  LOCALE_SMONTHNAME9          { long name for September }
  LOCALE_SMONTHNAME10         { long name for October }
  LOCALE_SMONTHNAME11         { long name for November }
  LOCALE_SMONTHNAME12         { long name for December }
  LOCALE_SMONTHNAME13         { long name for 13th month (if exists) }
  LOCALE_SABBREVMONTHNAME1    { abbreviated name for January }
  LOCALE_SABBREVMONTHNAME2    { abbreviated name for February }
  LOCALE_SABBREVMONTHNAME3    { abbreviated name for March }
  LOCALE_SABBREVMONTHNAME4    { abbreviated name for April }
  LOCALE_SABBREVMONTHNAME5    { abbreviated name for May }
  LOCALE_SABBREVMONTHNAME6    { abbreviated name for June }
  LOCALE_SABBREVMONTHNAME7    { abbreviated name for July }
  LOCALE_SABBREVMONTHNAME8    { abbreviated name for August }
  LOCALE_SABBREVMONTHNAME9    { abbreviated name for September }
  LOCALE_SABBREVMONTHNAME10   { abbreviated name for October }
  LOCALE_SABBREVMONTHNAME11   { abbreviated name for November }
  LOCALE_SABBREVMONTHNAME12   { abbreviated name for December }
  LOCALE_SABBREVMONTHNAME13   { abbreviated name for 13th month (if exists) }
  LOCALE_SPOSITIVESIGN        { positive sign }
  LOCALE_SNEGATIVESIGN        { negative sign }
  LOCALE_IPOSSIGNPOSN         { positive sign position }
  LOCALE_INEGSIGNPOSN         { negative sign position }
  LOCALE_IPOSSYMPRECEDES      { mon sym precedes pos amt }
  LOCALE_IPOSSEPBYSPACE       { mon sym sep by space from pos amt }
  LOCALE_INEGSYMPRECEDES      { mon sym precedes neg amt }
  LOCALE_INEGSEPBYSPACE       { mon sym sep by space from neg amt }
  LOCALE_FONTSIGNATURE        { font signature }
  LOCALE_SISO639LANGNAME      { ISO abbreviated language name }
  LOCALE_SISO3166CTRYNAME     { ISO abbreviated country name }

答案 2 :(得分:0)

扩大用户RRUZ的回答..

只需要获取一次结果,因此开发人员可以在创建期间获取值或显示为可重用的变量或属性。

function LCIDToLocaleName(Locale: LCID; lpName: LPWSTR; cchName: Integer;
  dwFlags: DWORD): Integer; stdcall;external kernel32 name 'LCIDToLocaleName';

function LocaleIDString(): string;
 var
   strNameBuffer : array [0..255] of WideChar; // 84 was len from original process online
   //localID : TLocaleID;
   // localID was 0, so didn't initialize, but still returned proper code page.
   // using 0 in lieu of localID : nets the same result, var not required.
   i : integer;

begin
  Result := '';

  // LOCALE_USER_DEFAULT  vs. LOCALE_SYSTEM_DEFAULT
  // since XP LOCALE_USER_DEFAULT is considered good practice for compatibility
  if (LCIDToLocaleName(LOCALE_USER_DEFAULT, strNameBuffer, 255, 0) > 0) then
    for i := 0 to 255 do
     begin
      if strNameBuffer[i] = #0 then
        break
      else
        Result := Result + strNameBuffer[i];
    end;

  if (Length(Result) = 0) and
  (LCIDToLocaleName(0, strNameBuffer, 255, 0) > 0) then
    for i := 0 to 255 do
     begin
      if strNameBuffer[i] = #0 then
        break
      else
        Result := Result + strNameBuffer[i];
    end;

  if Length(Result) = 0 then
    Result := 'NR-NR' // defaulting to [No Reply - No Reply]
end;