PxPlus User Forum

Main Board => Discussions => Programming => Topic started by: Thomas Bock on August 29, 2019, 10:34:57 AM

Title: locale settings in chromium browser
Post by: Thomas Bock on August 29, 2019, 10:34:57 AM
We use "parseFloat(value).toLocaleString();" in order to display decimal values with german formatting.
On my V14 WindX client I see english formatting.
On my V16 WindX client I see german formatting.
On our TS with a V14 WindX client I see german formatting.
On my colleague's V14 WindX client I see german formatting.

The OS settings for decimal formatting are all set correctly for both of us.
What else may have an effect on the formatting?

Marginalia: None of us sees options in the popup menu due to installation issues.
Title: Re: locale settings in chromium browser
Post by: Devon Austen on August 29, 2019, 11:03:47 AM
I would suspect that versions before 2019 (v16) would always treat the locale as US since it was not until 2019 that we implemented multi-language support for the embedded browser.

I would suggest resolving the installation issues (probably by doing a clean install in a new directory). If you are not getting a popup menu that means that the embedded chromium browser is not running correctly and any test results could be the result of undefined behavior cause by this.
Title: Re: locale settings in chromium browser
Post by: Frank Dreßler on August 29, 2019, 12:20:40 PM
As a workaround you could specify the target language "de-DE" explicitly like this:
Code: [Select]
(123456789.123456789).toLocaleString("de-DE")
Title: Re: locale settings in chromium browser
Post by: Frank Dreßler on August 29, 2019, 01:43:55 PM
Here is a more convenient workaround. After executing this code once any calls to toLocaleString/toLocaleTimeString/toLocaleDateString behave as if "de-DE" was passed explicitly. It should not be executed twice.

It might be considered bad practice to change the prototype of built-in objects and functions. One negative side effect of this work-around is the fact that .toLocaleString() does not behave like .toLocaleString(navigator.language) anymore.

Code: [Select]
// Hooks Number's and Date's .toLocale...() to use "de-DE" in case no language is passed expliclitly.
(function(defaultLocale) {
function hookObjectFunction(obj, name, hook) {
const orig = obj[name];
obj[name] = function(...args) { return hook.call(this, orig, args); }
}

function defaultLocaleHook(orig, args) {
args[0] = args[0] || defaultLocale;
return orig.apply(this, args);
}

hookObjectFunction(Date.prototype, "toLocaleDateString", defaultLocaleHook);
hookObjectFunction(Date.prototype, "toLocaleTimeString", defaultLocaleHook);
hookObjectFunction(Date.prototype, "toLocaleString", defaultLocaleHook);
hookObjectFunction(Number.prototype, "toLocaleString", defaultLocaleHook);
})("de-DE");

EDIT: Fixed a bug (orig.call() changed to orig.apply())