delphirules
delphirules

Reputation: 7390

How to correctly get system language in Windows 7 or later

I need to extract the user's Windows language, and i'm using the function below. However i tested in my current machine (it's op english), but the function return is "Portuguese (Brazil)".

Actually my machine was in this language originally, but after i installed a new language pack and it's now in English, not Portuguese ; so i assume the function below is not working properly. Is there another alternative ?

Thanks

function GetWindowsLanguage: string;
var
  WinLanguage: array [0..50] of char;
begin
  VerLanguageName(GetSystemDefaultLangID, WinLanguage, 50);
  Result := StrPas(WinLanguage);
end;

Upvotes: 1

Views: 1425

Answers (2)

SilverWarior
SilverWarior

Reputation: 8331

In order to get the information about which language is being used for Windows UI by current user you should use GetUserDefaultUILanguage function instead of GetSystemDefaultLangID

If the current user has not set any language then System Default UI Language is returned.

For Delphi 2007, you'll need to declare the function, as it didn't exist at the time that version was released. Note that doing so statically as shown here will mean your app will no longer run on versions of Windows prior to Windows 2000.

function GetUserDefaultUILanguage: LANGID; stdcall; external 'kernel32';

function GetUsersWindowsLanguage: string;
var
  WinLanguage: array [0..50] of char;
begin
  VerLanguageName(GetUserDefaultUILanguage, WinLanguage, 50);
  Result := WinLanguage;
end;

Upvotes: 6

Ken White
Ken White

Reputation: 125661

You want GetUserDefaultLangID instead. If your machine was set for Portugese when Windows was installed, that is the default language for your system. The user's currently selected language is the one chosen by the logged in user. (See the Remarks on the linked page.)

function GetUsersWindowsLanguage: string;
var
  WinLanguage: array [0..50] of char;
begin
  VerLanguageName(GetUserDefaultLangID, WinLanguage, 50);
  Result := StrPas(WinLanguage);
end;

Upvotes: 3

Related Questions