Recent

Author Topic: Get selected country from system in Linux  (Read 10282 times)

hy

  • Full Member
  • ***
  • Posts: 221
Re: Get selected country from system in Linux
« Reply #15 on: October 22, 2014, 09:38:11 pm »
Ok, I will leave it to the user to select a country and write it to some config file.
Thanks for helping.
_____
***hy
OS: debian sid(64bit)  [fpc 3.20] Lazarus 2.0.12

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Get selected country from system in Linux
« Reply #16 on: October 22, 2014, 09:58:02 pm »
In my applications I successfully use the following platform-independent function:

Code: [Select]
function GetOSLanguage: string;
  {platform-independent method to read the language of the user interface}
var
  l, fbl: string;
  {$IFDEF LCLCarbon}
  theLocaleRef: CFLocaleRef;
  locale: CFStringRef;
  buffer: StringPtr;
  bufferSize: CFIndex;
  encoding: CFStringEncoding;
  success: boolean;
  {$ENDIF}
begin
  {$IFDEF LCLCarbon}
  theLocaleRef := CFLocaleCopyCurrent;
  locale := CFLocaleGetIdentifier(theLocaleRef);
  encoding := 0;
  bufferSize := 256;
  buffer := new(StringPtr);
  success := CFStringGetPascalString(locale, buffer, bufferSize, encoding);
  if success then
    l := string(buffer^)
  else
    l := '';
  fbl := Copy(l, 1, 2);
  dispose(buffer);
  {$ELSE}
  {$IFDEF LINUX}
  fbl := Copy(GetEnvironmentVariable('LC_CTYPE'), 1, 2);
    {$ELSE}
  GetLanguageIDs(l, fbl);
    {$ENDIF}
  {$ENDIF}
  Result := fbl;
end;

The function delivers the system language as two-byte ISO code. It works on Mac OS X, Linux and Windows.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

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

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Get selected country from system in Linux
« Reply #17 on: October 22, 2014, 10:19:23 pm »
That code only checks one environment variable. Moreover it tests for Linux when it should test for Unix, sloppy.

As said, LC_CTYPE might not be defined, or "C" (meaning, use internal hardcoded value, without )

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Get selected country from system in Linux
« Reply #18 on: October 22, 2014, 11:21:43 pm »
From GetText unit

Code: [Select]
procedure GetLanguageIDs(var Lang, FallbackLang: string);
begin
  lang := GetEnvironmentVariable('LC_ALL');
  if Length(lang) = 0 then
  begin
    lang := GetEnvironmentVariable('LC_MESSAGES');
    if Length(lang) = 0 then
    begin
      lang := GetEnvironmentVariable('LANG');
      if Length(lang) = 0 then
        exit;   // no language defined via environment variables
    end;
  end;
  FallbackLang := Copy(lang, 1, 2);
end;

Suffers from same flaws Marcov poited out in the previous message.

Bart

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Get selected country from system in Linux
« Reply #19 on: October 23, 2014, 01:16:58 am »
Suffers from same flaws Marcov poited out in the previous message.

Well, it seems to depend on the platform.

Windows is sufficiently supported by GetLanguageIDs. My solution has strengths on Mac OS X, where it uses a native API function. Linux seems to be still under-represented, however. In my test cases the function GetOSLanguage worked well on Linux, but my tests were restricted to Raspbian, openSUSE 11 and Ubuntu 13, so that the code may fail with other Linux distributions. Furthermore, additional Unix derivatives, e.g. BSD and Solaris are completely neglected. Therefore we are still in need for good ideas.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

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

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: Get selected country from system in Linux
« Reply #20 on: December 09, 2014, 11:29:38 pm »
My apologies for reopening an old topic, but it seemed the most appropriate place.

I have investigated identifying the installed language on a Linux Mint 17.1 PC. My environment is UK English - en_GB.

After installing German the available languages and variants were as follows:

mike@laptop ~ $ locale -a
C
C.UTF-8
de_AT.utf8
de_BE.utf8
de_CH.utf8
de_DE@euro
de_DE.iso885915@euro
de_DE.utf8
de_LI.utf8
de_LU.utf8
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8
nds_DE
nds_DE.utf8
nds_NL
nds_NL.utf8
POSIX

The format is basically country code plus language code plus optional modifier. Gentoo wiki says: A locale name is generally named ab_CD where ab is your two (or three) letter language code (as specified in ISO-639) and CD is your two letter country code (as specified in ISO-3166). Variants are often appended to locale names, e.g. en_GB.UTF-8 or de_DE@euro.

I do not understand the C and POSIX lines, but there are 5 other clear formats that need to be decoded in any program that seeks to offer several languages.

Before installing German, the relevant system variables were:
mike@laptop ~ $ env
LANG=en_GB.UTF-8
MDM_LANG=en_GB.UTF-8

There were no LC_XXX system variables.

After installing German, the relevant system variables were:
mike@laptop ~ $ env
LC_PAPER=de_DE.UTF-8
LC_ADDRESS=de_DE.UTF-8
LC_MONETARY=de_DE.UTF-8
LC_NUMERIC=de_DE.UTF-8
LC_TELEPHONE=de_DE.UTF-8
LC_IDENTIFICATION=de_DE.UTF-8
LC_MEASUREMENT=de_DE.UTF-8
MDM_LANG=de_DE.UTF-8
LC_TIME=de_DE.UTF-8
LC_NAME=de_DE.UTF-8

LANG=xxx has gone (Unless I made a mistake editing), and LC_XXX variables have appeared.

I then selected English(GB) as the default language, but did not change the region.
mike@laptop ~ $ env
LC_PAPER=de_DE.UTF-8
LC_ADDRESS=de_DE.UTF-8
LC_MONETARY=de_DE.UTF-8
LC_NUMERIC=de_DE.UTF-8
LC_IDENTIFICATION=de_DE.UTF-8
LANG=en_GB.UTF-8
LC_MEASUREMENT=de_DE.UTF-8
MDM_LANG=en_GB.UTF-8
LC_TIME=de_DE.UTF-8
LC_NAME=de_DE.UTF-8

LANG=en_GB.UTF-8 has appeared.

Finally after changing the regional defaults to English(GB):
mike@laptop ~ $ env
LC_PAPER=en_GB.UTF-8
LC_ADDRESS=en_GB.UTF-8
LC_MONETARY=en_GB.UTF-8
LC_NUMERIC=en_GB.UTF-8
LC_TELEPHONE=en_GB.UTF-8
LC_IDENTIFICATION=en_GB.UTF-8
LANG=en_GB.UTF-8
MDM_LANG=en_GB.UTF-8
LC_TIME=de_DE.UTF-8                 //Clearly a Linux Mint error
LC_NAME=en_GB.UTF-8

It appears that the LC_XXX variables only appear after the language has been changed from the original default.

There was no LC_ALL, and I guess that Linux Mint uses LC_NAME instead.

At the moment I am combining the code from Bart and jwdietrich with allowances for the possible optional modifiers. I'll post it for comment when it is finished.

hy

  • Full Member
  • ***
  • Posts: 221
Re: Get selected country from system in Linux
« Reply #21 on: December 10, 2014, 11:21:53 am »
Well, I get
Code: [Select]
locale -a
be_BY.utf8
bg_BG.utf8
C
cs_CZ.utf8
C.UTF-8
da_DK.utf8
de_CH.utf8
de_DE.utf8
el_GR.utf8
en_AU.utf8
en_GB.utf8
en_IE.utf8
en_US.utf8
es_ES.utf8
fi_FI.utf8
fr_BE.utf8
fr_FR.utf8
ga_IE.utf8
he_IL.utf8
hr_HR.utf8
hu_HU.utf8
it_IT.utf8
ja_JP.utf8
ko_KR.utf8
nb_NO.utf8
nl_BE.utf8
nl_NL.utf8
nn_NO.utf8
pl_PL.utf8
POSIX
pt_BR.utf8
pt_PT.utf8
ro_RO.utf8
ru_RU.utf8
sk_SK.utf8
sl_SI.utf8
tr_TR.utf8                                                                                                                                                                                     
uk_UA.utf8                                                                                                                                                                                     
zh_CN.utf8                                                                                                                                                                                     
zh_TW.utf8
_____
***hy
OS: debian sid(64bit)  [fpc 3.20] Lazarus 2.0.12

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: Get selected country from system in Linux
« Reply #22 on: December 10, 2014, 05:25:00 pm »
According to http://linux.die.net/man/3/setlocale

The locale "C""" or "POSIX""" is a portable locale; its LC_CTYPE part corresponds to the 7-bit ASCII character set.

It seems to be used when installing a new release of a Linux distribution.

 

TinyPortal © 2005-2018