Re: [GNC-dev] Early locale initialization

2021-03-18 Thread John Ralls



> On Mar 18, 2021, at 6:08 AM, Geert Janssens  
> wrote:
> 
> Op donderdag 18 maart 2021 13:56:35 CET schreef Derek Atkins:
>> On Thu, March 18, 2021 7:58 am, Geert Janssens wrote:
>> [snip]
>> 
>>> 3. While all three allow to tweak some environment parameters, only linux
>>> allows the user to do so via the environment file. For Windows and Macos
>>> that
>>> file is only read after locale is set. Whether that's a good thing depends
>>> on
>>> the answer to question 2.
>> 
>> Are you saying that /etc/gnucash/environment (or the equivalent thereof)
>> is not read/used on Windows or Mac?
>> 
> No, it is used on all platforms. But it's only read after locale 
> initialization on mac/windows has been done. So for locale initialization it 
> only will affect linux. The other parameters in there (like LD_LIBRARY_PATH 
> tweaks and so on) will still affect all platforms.
> 

No, it's read after the other initializations so that it will override them. As 
long as setlocale is called after loading the environment then the last 
computed locale is what GnuCash starts up with. I've tested that pretty 
extensively on both macOS and Win32, though not since last fall.

The one thing that doesn't work is passing LANG/LANGUAGES in the Win32 CMD 
shell because that shell doesn't understand that. It should be possible on 
Powershell with something like $Env:LANG=nl_BE "c:\Program Files 
(x86)\gnucash\bin\gnucash.exe"; I've never actually tried that.

On macOS the language fallback is used because Apple's localization can result 
in locales that libc thinks aren't legal, e.g. en_BE so we have to find 
something close or the user gets "C". That's done only when getting the locale 
from system preferences (aka "defaults").

Regards,
John Ralls


___
gnucash-devel mailing list
gnucash-devel@gnucash.org
https://lists.gnucash.org/mailman/listinfo/gnucash-devel


Re: [GNC-dev] Early locale initialization

2021-03-18 Thread Geert Janssens
Op donderdag 18 maart 2021 13:56:35 CET schreef Derek Atkins:
> On Thu, March 18, 2021 7:58 am, Geert Janssens wrote:
> [snip]
> 
> > 3. While all three allow to tweak some environment parameters, only linux
> > allows the user to do so via the environment file. For Windows and Macos
> > that
> > file is only read after locale is set. Whether that's a good thing depends
> > on
> > the answer to question 2.
> 
> Are you saying that /etc/gnucash/environment (or the equivalent thereof)
> is not read/used on Windows or Mac?
> 
No, it is used on all platforms. But it's only read after locale 
initialization on mac/windows has been done. So for locale initialization it 
only will affect linux. The other parameters in there (like LD_LIBRARY_PATH 
tweaks and so on) will still affect all platforms.

Geert


___
gnucash-devel mailing list
gnucash-devel@gnucash.org
https://lists.gnucash.org/mailman/listinfo/gnucash-devel


Re: [GNC-dev] Early locale initialization

2021-03-18 Thread Derek Atkins


On Thu, March 18, 2021 7:58 am, Geert Janssens wrote:
[snip]
> 3. While all three allow to tweak some environment parameters, only linux
> allows the user to do so via the environment file. For Windows and Macos
> that
> file is only read after locale is set. Whether that's a good thing depends
> on
> the answer to question 2.

Are you saying that /etc/gnucash/environment (or the equivalent thereof)
is not read/used on Windows or Mac?

> Regards,
>
> Geert

-derek
-- 
   Derek Atkins 617-623-3745
   de...@ihtfp.com www.ihtfp.com
   Computer and Internet Security Consultant

___
gnucash-devel mailing list
gnucash-devel@gnucash.org
https://lists.gnucash.org/mailman/listinfo/gnucash-devel


Re: [GNC-dev] Early locale initialization

2021-03-18 Thread Geert Janssens
Op donderdag 18 maart 2021 12:58:28 CET schreef Geert Janssens:
> On Windows the code will first look for environment variables "LC_ALL",
> "LC_MESSAGES" or "LANG" (in that order). If any of those exists it will try
> to convert that to a locale. If that fails it will fall back to the system
> default locale and if that fails it falls through to our main code which
> then defaults to "C".

This is not even true. The way it is currently implemented it won't reach the 
fallback-to-C code at all.

Geert


___
gnucash-devel mailing list
gnucash-devel@gnucash.org
https://lists.gnucash.org/mailman/listinfo/gnucash-devel


Re: [GNC-dev] Early locale initialization

2021-03-18 Thread Geert Janssens
In addition, the Macos code doesn't return the final locale string that was 
set. As a result the PINFO message to log the locale details to the trace file 
is partly bogus. Granted this was written with the Windows locale issues in 
mind, but again, it would be nice if this could be made consistent.

Regards,

Geert

Op donderdag 18 maart 2021 12:58:28 CET schreef Geert Janssens:
> In a moment of distraction I started looking at the locale initialization
> code in gnucash-core-app.cpp.
> 
> If I understand the actual code properly it's not exactly working as I had
> intended. So with this thread, I'd like to clear this out.
> 
> For reference I'm talking about the code that starts here:
> https://github.com/Gnucash/gnucash/blob/
> 1756f76079221f77904ccb9c9a504f9ebca2eb0e/gnucash/gnucash-core-app.cpp#L418
> 
> Locale initialization is strongly influenced by platform, so there are
> different code paths to set locale.
> 1. Macos will use set_mac_locale
> 2. Windows will use set_win32_thread_locale
> 3. Linux will fall through and use setlocale
> 
> So far so good.
> 
> Looking deeper, the linux method will search for an environment variables
> LC_ALL or LANG in that order and uses its value as locale. If that fails, it
> will fall back to the C locale. There is also code  in place to allow the
> user to override any of these variables by setting them in the gnucash
> environment file.
> 
> On Macos the code will first try to use the Macos proper locale setup code.
> If that fails it falls back to locale "en_US.utf-8". Then it sets the LANG
> variable to the same locale and proceeds with calling gnc_localeconv which
> will fill in the locale details that will be used throughout the code.
> 
> On Windows the code will first look for environment variables "LC_ALL",
> "LC_MESSAGES" or "LANG" (in that order). If any of those exists it will try
> to convert that to a locale. If that fails it will fall back to the system
> default locale and if that fails it falls through to our main code which
> then defaults to "C".
> 
> 
> Putting these next to each other there are a few differences:
> 1. the final fallback locale. On Macos that will be "en_US.utf-8", on the
> other platforms it will be "C". Should we use the same default on all or
> rather not ?
> 2. Priority of locale source. On linux and Windows adding variables in the
> environment will allow the user to override the default system locale. On
> Macos it will only allow to set LANG or LANGUAGE variables, not locale. Do
> we want to allow the user to override system locale via environment
> variables or do we only want a fallback mechanism ? Do we want to allow the
> user specify the locale to use for that fallback ?
> I'll note the Macos code has a neat fallback trick in that it tries to find
> a related locale in case the default doesn't work. Related in the sense
> that it may be another language in same country or if that fails the
> language without country. Downside is that in  case of multiple fallback
> options the one chosen may not be the one the user actually would have
> preferred.
> 3. While all three allow to tweak some environment parameters, only linux
> allows the user to do so via the environment file. For Windows and Macos
> that file is only read after locale is set. Whether that's a good thing
> depends on the answer to question 2.
> 
> Regards,
> 
> Geert
> 
> 
> ___
> gnucash-devel mailing list
> gnucash-devel@gnucash.org
> https://lists.gnucash.org/mailman/listinfo/gnucash-devel




___
gnucash-devel mailing list
gnucash-devel@gnucash.org
https://lists.gnucash.org/mailman/listinfo/gnucash-devel


[GNC-dev] Early locale initialization

2021-03-18 Thread Geert Janssens
In a moment of distraction I started looking at the locale initialization code 
in gnucash-core-app.cpp.

If I understand the actual code properly it's not exactly working as I had 
intended. So with this thread, I'd like to clear this out.

For reference I'm talking about the code that starts here:
https://github.com/Gnucash/gnucash/blob/
1756f76079221f77904ccb9c9a504f9ebca2eb0e/gnucash/gnucash-core-app.cpp#L418

Locale initialization is strongly influenced by platform, so there are 
different code paths to set locale.
1. Macos will use set_mac_locale
2. Windows will use set_win32_thread_locale
3. Linux will fall through and use setlocale

So far so good.

Looking deeper, the linux method will search for an environment variables 
LC_ALL or LANG in that order and uses its value as locale. If that fails, it 
will fall back to the C locale. There is also code  in place to allow the user 
to override any of these variables by setting them in the gnucash environment 
file.

On Macos the code will first try to use the Macos proper locale setup code. If 
that fails it falls back to locale "en_US.utf-8". Then it sets the LANG 
variable to the same locale and proceeds with calling gnc_localeconv which 
will fill in the locale details that will be used throughout the code.

On Windows the code will first look for environment variables "LC_ALL", 
"LC_MESSAGES" or "LANG" (in that order). If any of those exists it will try to 
convert that to a locale. If that fails it will fall back to the system 
default locale and if that fails it falls through to our main code which then 
defaults to "C".


Putting these next to each other there are a few differences:
1. the final fallback locale. On Macos that will be "en_US.utf-8", on the 
other platforms it will be "C". Should we use the same default on all or 
rather not ?
2. Priority of locale source. On linux and Windows adding variables in the 
environment will allow the user to override the default system locale. On 
Macos it will only allow to set LANG or LANGUAGE variables, not locale. Do we 
want to allow the user to override system locale via environment variables or 
do we only want a fallback mechanism ? Do we want to allow the user specify 
the locale to use for that fallback ?
I'll note the Macos code has a neat fallback trick in that it tries to find a 
related locale in case the default doesn't work. Related in the sense that it 
may be another language in same country or if that fails the language without 
country. Downside is that in  case of multiple fallback options the one chosen 
may not be the one the user actually would have preferred.
3. While all three allow to tweak some environment parameters, only linux 
allows the user to do so via the environment file. For Windows and Macos that 
file is only read after locale is set. Whether that's a good thing depends on 
the answer to question 2.

Regards,

Geert


___
gnucash-devel mailing list
gnucash-devel@gnucash.org
https://lists.gnucash.org/mailman/listinfo/gnucash-devel