Recent

Author Topic: How does Free Pascal set DefaultFormatSettings under Linux  (Read 1190 times)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1353
  • Professional amateur ;-P
How does Free Pascal set DefaultFormatSettings under Linux
« on: June 28, 2025, 07:04:50 pm »
Hey Y'all,

I'm starting a new thread, cuz nobody ever solved this for @wp here: https://forum.lazarus.freepascal.org/index.php/topic,19898.0.html

I'm now under the same-ish condition... I've been able to translate the humanize package even when we provide a command line of -l|--lang.

The next logical step would be to then change the DefaultFormatSettings for the language we obtain with this:
Code: Pascal  [Select][+][-]
  1. function GetLanguageFromCommandLine: String;
  2. var
  3.   index: Integer;
  4. begin
  5.   Result:= EmptyStr;
  6.   for index:= 0 to ParamCount do
  7.   begin
  8.     if ParamStr(index) = '-l' then
  9.     begin
  10.       Result:= ParamStr(index + 1);
  11.       break;
  12.     end;
  13.     if Pos('--lang', ParamStr(index)) > 0 then
  14.     begin
  15.       if Pos('=', ParamStr(index)) > 0 then
  16.       begin
  17.         Result:= Copy(ParamStr(index), Pos('=', ParamStr(index)) + 1, Length(ParamStr(index)));
  18.         break;
  19.       end
  20.       else
  21.       begin
  22.         Result:= ParamStr(index + 1);
  23.         break;
  24.       end;
  25.     end;
  26.   end;
  27.   if Result = EmptyStr then
  28.     Result:= GetLanguageID.LanguageCode;
  29. end;

From the thread mentioned above, there is a solution to do that under Windows with GetLocaleFormatSettings.

Upon this information, one would ask: Is DefaultFormatSettings not being modified to the system's locale under Linux? And if so, can someone please point me and @wp to that place?!

In the mean time, I'm gonna ask how this is done in Go( for example ) to one of the AI Bots.

Cheers,
Gus

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1353
  • Professional amateur ;-P
Re: How does Free Pascal set DefaultFormatSettings under Linux
« Reply #1 on: June 28, 2025, 07:41:28 pm »
Hey Y'All,

Ok, after asking the AI Bot, my investigation is now around the fact that in Linux, all this is managed by setlocale.

Then, we can query for LC_NUMERIC and the other such constants to fill DefaultFormatSettings.
Actually, under the clocale unit, this is already done. And I suspect that this is what's called internally in the process of getting such values when a GUI app is started.

But as I realised before, the clocale is just a bunch of code that is called in it's initiliazation, but does not provide such tools for us to play about!!!

The problem is this: clocale DOES update DefaultFormatSettings, BUT it does so with only the system settings.
What we would wish is that somewhere the same code can be used with a string containing the language and change it to that.

I'm aware that it's rather safe to only do this using the system's locale. This ensures that we get all correct stuff, since the user has taken care of that in the system's settings.
Nonetheless, I would prefer that this could be allowed to be handled by the programmer. This is an Open Source language, after all, and it's common in such an environment to be spoiled with choice.

While the core devs ponder my question, I'll copy clocale to a test folder and see what I can hack. Even if it explodes in my face :D

@wp I'll keep you posted via private messages of my efforts!! If successful, we can add my code to the code you attached that works on Windows.

Cheers,
Gus

wp

  • Hero Member
  • *****
  • Posts: 13489
Re: How does Free Pascal set DefaultFormatSettings under Linux
« Reply #2 on: June 28, 2025, 07:56:48 pm »
Long time gone...

As for the system's formatsettings, add unit "clocale" to the uses clause - there was a discussion about it recently. As for any arbitrary language, I still don't know, but looking at the sources of clocale I get the expression that it might serve as a guideline to find out.

wp

  • Hero Member
  • *****
  • Posts: 13489
Re: How does Free Pascal set DefaultFormatSettings under Linux
« Reply #3 on: June 28, 2025, 08:03:06 pm »
I'll copy clocale to a test folder and see what I can hack. Even if it explodes in my face :D
Yes, that's what I'd do: Copy clocale to a test folder, add an overladed procedure "GetFormatSettings(ALang: String; out fmts: TFormatSettings)". Copy the original procedure. Immediately after the "begin" replace "setlocale(__LCL_ALL, '')" by "setlocale(__LCL_ALL, ALang)", and see what happens...

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1353
  • Professional amateur ;-P
Re: How does Free Pascal set DefaultFormatSettings under Linux
« Reply #4 on: June 28, 2025, 08:20:17 pm »
Hey WP,

Yes, that's what I'd do: Copy clocale to a test folder, add an overladed procedure "GetFormatSettings(ALang: String; out fmts: TFormatSettings)". Copy the original procedure. Immediately after the "begin" replace "setlocale(__LCL_ALL, '')" by "setlocale(__LCL_ALL, ALang)", and see what happens...

You read my mind !!! Great minds and all that !!!

I'm gonna do this and add it to my Test* with a waning: Here be Dragons !!! :D
I'll keep you posted here!!
I've already installed the Portuguese language on my Ubuntu system, to see if that will not go BOOM if I test it with that( not sure I need a reboot after installing it ).
But I'll try German and French, just to see how it goes up in flames  >:D

Cheers,
Gus

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1353
  • Professional amateur ;-P
Re: How does Free Pascal set DefaultFormatSettings under Linux
« Reply #5 on: June 28, 2025, 11:36:02 pm »
Hey WP,

SUCCESS!!!!

I was finally able to force the thang my way: Test Locale.

It has a big caveat: The system must have the language installed, or else it will not work.
In my case, I have English and Portuguese installed. I was able to get the different format settings for both.
But since I don't have French nor German, only the language of the app changes, not the DefaultFormatSettings.

While this is a nice proof of concept, it fails due to the fact that we depend on the system, not on a set of .po files :(

I think it would be a bit more practical to implement something based on a JSON file, or something like this...
This way we can provide the file in the locale folder with the project...
Something to think about...

Cheers,
Gus

wp

  • Hero Member
  • *****
  • Posts: 13489
Re: How does Free Pascal set DefaultFormatSettings under Linux
« Reply #6 on: June 28, 2025, 11:42:21 pm »
Why not declare resource strings for DecimalSeparator, Thousandsepator, month names, day names and whatever? Then they will be in the po file and can be translated. And after you called TranslateUnitResourceString you can pass them to the FormatSettings. This system will work for all operating systems for any language for which you have a translation, no clocale needed in Linux (and mac?)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1353
  • Professional amateur ;-P
Re: How does Free Pascal set DefaultFormatSettings under Linux
« Reply #7 on: June 28, 2025, 11:44:49 pm »
Hey WP,

Why not declare resource strings for DecimalSeparator, Thousandsepator, month names, day names and whatever? Then they will be in the po file and can be translated. And after you called TranslateUnitResourceString you can pass them to the FormatSettings. This system will work for all operating systems for any language for which you have a translation, no clocale needed in Linux (and mac?)

I'm soooo tired right now that this didn't even occurred to me!!! But of course that's a way better way to solve this!!! And in a single place !!

Many thanks for the suggestion !!!

Cheers,
Gus

 

TinyPortal © 2005-2018