Recent

Author Topic: Get Language OS  (Read 20851 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2259
    • Lazarus and Free Pascal italian community
Get Language OS
« on: April 26, 2011, 01:31:14 pm »
Hello, someone can tell me how I can recover the language used by the operating system? Primarily for windows, but if anyone knows for linux. Thanks
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

lainz

  • Guest
Re: Get Language OS
« Reply #1 on: April 26, 2011, 04:01:01 pm »
Hello, someone can tell me how I can recover the language used by the operating system? Primarily for windows, but if anyone knows for linux. Thanks

See here:
http://wiki.lazarus.freepascal.org/Translations_/_i18n_/_localizations_for_programs

xinyiman

  • Hero Member
  • *****
  • Posts: 2259
    • Lazarus and Free Pascal italian community
Re: Get Language OS
« Reply #2 on: April 26, 2011, 04:11:55 pm »
I wanted to know if in my XP system I'm using the Italian or English or another, I can not understand what the center with what I've suggested. And if there is cross-platform solution.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Get Language OS
« Reply #3 on: April 26, 2011, 04:22:59 pm »
If I'm understanding your question, SysLocale.DefaultLCID will gire you the default Locale as a number, which will tell you what the computer location is set to.

type TSysLocale = packed record
  DefaultLCID: LCID;
  PriLangID: LANGID;
  SubLangID: LANGID;
  FarEast: Boolean;
  MiddleEast: Boolean;
end;
Lazarus Trunk / fpc 2.6.2 / Win32

xinyiman

  • Hero Member
  • *****
  • Posts: 2259
    • Lazarus and Free Pascal italian community
Re: Get Language OS
« Reply #4 on: April 26, 2011, 04:47:22 pm »
And how do I know the correspondence between ID and country code? That is, how do I know which code is Italy, which code has the cake and so on?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Get Language OS
« Reply #5 on: April 26, 2011, 05:09:53 pm »
Add a TListBox and a TButton to a Form and then add this code to the OnClick event of the button.

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
begin
  for I:= 0 to Languages.Count-1 do begin
    ListBox1.Items.Add(Languages.Name+' : '+IntToStr(Languages.LocaleID));
  end;
end;
Lazarus Trunk / fpc 2.6.2 / Win32

xinyiman

  • Hero Member
  • *****
  • Posts: 2259
    • Lazarus and Free Pascal italian community
Re: Get Language OS
« Reply #6 on: April 26, 2011, 05:29:31 pm »
Hint: Start of reading config file c:\lazarus\fpc\2.4.2\bin\i386-win32\fpc.cfg
Hint: End of reading config file c:\lazarus\fpc\2.4.2\bin\i386-win32\fpc.cfg
Free Pascal Compiler version 2.4.2 [2011/03/08] for i386
Copyright (c) 1993-2010 by Florian Klaempfl
Target OS: Win32 for i386
Compiling project1.lpr
Compiling unit1.pas
unit1.pas(23,28) Hint: Parameter "Sender" not used
unit1.pas(24,28) Hint: Parameter "Sender" not used
unit1.pas(71,25) Error: Identifier not found "Languages"
unit1.pas(72,33) Error: Identifier not found "Languages"
unit1.pas(72,63) Error: Identifier not found "Languages"
unit1.pas(130) Fatal: There were 3 errors compiling module, stopping
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Get Language OS
« Reply #7 on: April 26, 2011, 05:36:21 pm »
Sorry for misleading you.  The code I gave is for Delphi.  I don't think it will work in Lazarus.
Lazarus Trunk / fpc 2.6.2 / Win32

jixian.yang

  • Full Member
  • ***
  • Posts: 173
Re: Get Language OS
« Reply #8 on: April 26, 2011, 06:06:10 pm »
On linux it is GetEnvironmentVariable('LANG');

  for i := 1 to GetEnvironmentVariableCount do
    Memo1.Lines.Add(GetEnvironmentString(i));


And get language with Delphi there should be []:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
begin
  for I:= 0 to Languages.Count-1 do begin
    ListBox1.Items.Add(Languages.Name[I]+' : '+IntToStr(Languages.LocaleID[I]));
  end;
end;
« Last Edit: April 26, 2011, 06:17:37 pm by jixian.yang »

jixian.yang

  • Full Member
  • ***
  • Posts: 173
Re: Get Language OS
« Reply #9 on: April 26, 2011, 06:32:50 pm »
Code: [Select]
function GetLocaleInformation(Flag: integer): string;
var
  pcLCA: array[0..20] of char;
begin
  if (GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, Flag, pcLCA, 19) <= 0) then
  begin
    pcLCA[0] := #0;
  end;
  Result := pcLCA;
end;

{$ENDIF}

function GetLocaleLanguage: string;
begin
  {$IFDEF MSWINDOWS}
   Result := GetLocaleInformation(LOCALE_SENGLANGUAGE);
  {$ELSE}
   Result := SysUtils.GetEnvironmentVariable('LANG');
  {$ENDIF}
end;                       

Fred vS

  • Hero Member
  • *****
  • Posts: 3727
    • StrumPract is the musicians best friend
Re: Get Language OS
« Reply #10 on: April 26, 2011, 09:03:05 pm »
@Jixiang.Yang
Nice shot, many thanks  :P
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

xinyiman

  • Hero Member
  • *****
  • Posts: 2259
    • Lazarus and Free Pascal italian community
Re: Get Language OS
« Reply #11 on: April 27, 2011, 08:20:28 am »
Thanks It's ok

  {$IFDEF MSWINDOWS}
function GetLocaleInformation(Flag: integer): string;
var
  pcLCA: array[0..20] of char;
begin
  if (GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, Flag, pcLCA, 19) <= 0) then
  begin
    pcLCA[0] := #0;
  end;
  Result := pcLCA;
end;

{$ENDIF}

function GetLocaleLanguage: string;
begin
  {$IFDEF MSWINDOWS}
   Result := GetLocaleInformation(LOCALE_SENGLANGUAGE);
  {$ELSE}
   Result := SysUtils.GetEnvironmentVariable('LANG');
  {$ENDIF}
end;   
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

jwdietrich

  • Hero Member
  • *****
  • Posts: 1266
    • formatio reticularis
Re: Get Language OS
« Reply #12 on: October 03, 2016, 07:51:52 pm »
Sorry for replying lately to this rather old post, but it may still be interesting.

In my programs I use the following code:

Code: Pascal  [Select][+][-]
  1. function GetOSLanguage: string;
  2.   {platform-independent method to read the language of the user interface}
  3. var
  4.   l, fbl: string;
  5.   {$IFDEF LCLCarbon}
  6.   theLocaleRef: CFLocaleRef;
  7.   locale: CFStringRef;
  8.   buffer: StringPtr;
  9.   bufferSize: CFIndex;
  10.   encoding: CFStringEncoding;
  11.   success: boolean;
  12.   {$ENDIF}
  13. begin
  14.   {$IFDEF LCLCarbon}
  15.   theLocaleRef := CFLocaleCopyCurrent;
  16.   locale := CFLocaleGetIdentifier(theLocaleRef);
  17.   encoding := 0;
  18.   bufferSize := 256;
  19.   buffer := new(StringPtr);
  20.   success := CFStringGetPascalString(locale, buffer, bufferSize, encoding);
  21.   if success then
  22.     l := string(buffer^)
  23.   else
  24.     l := '';
  25.   fbl := Copy(l, 1, 2);
  26.   dispose(buffer);
  27.   {$ELSE}
  28.   {$IFDEF UNIX}
  29.   fbl := Copy(GetEnvironmentVariable('LC_CTYPE'), 1, 2);
  30.   {$ELSE}
  31.   GetLanguageIDs(l, fbl);
  32.   {$ENDIF}
  33.   {$ENDIF}
  34.   Result := fbl;
  35. end;
  36.  

It has the advantage to also support OS X, e.g. macOS.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 4.2.0 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Get Language OS
« Reply #13 on: October 03, 2016, 08:03:16 pm »
It has the advantage to also support OS X, e.g. macOS.

Try replacing your Mac code with this:

{IFDEF DARWIN}
  fbl := NSLocale.currentLocale.localeIdentifier.UTF8String;
{$ENDIF}

ASerge

  • Hero Member
  • *****
  • Posts: 2470
Re: Get Language OS
« Reply #14 on: October 03, 2016, 08:27:14 pm »
Hello, someone can tell me how I can recover the language used by the operating system? Primarily for windows, but if anyone knows for linux. Thanks
It's not simple question. On Windows:
1. Locale - settings that are used for formatting dates, times, currency, and large numbers. Also determines the sort order for sorting text.
2. Input language - consists of a language and a method of input.
3. UI language - determines the language of menus and dialogs, messages, setup information (INF) files, and help file.
Items can be independent. Often programmers want to use p.3 (message translation), but uses p.1 to determine the language.

 

TinyPortal © 2005-2018