Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force decimal separator in browsers #16236

Closed
ZecosMAX opened this issue Jul 4, 2024 · 4 comments
Closed

Force decimal separator in browsers #16236

ZecosMAX opened this issue Jul 4, 2024 · 4 comments

Comments

@ZecosMAX
Copy link

ZecosMAX commented Jul 4, 2024

Is your feature request related to a problem? Please describe.

I had a problem with a custom converter, which takes a double as it's argument. Like '2.3'

Because my system settings uses a dot (.) as a decimal separator, i haven't ever experienced any problems with that
Until now, for some reason my converter worked in previewer and in desktop builds:
Screenshot1

However it didn't work in any of my browsers (Firefox dev 128.0b9; Google Chrome 126.0.6478.127; MS Edge 126.0.2592.87)
Until i specifically installed in my Firefox browser an en-GB locale as suggested in this ticket on Mozilla support: (https://support.mozilla.org/en-US/questions/1172233)
изображение

Describe the solution you'd like

Maybe somehow force these types of settings to match the globalization settings of UI thread?
(I can't confirm if this is a C# side problem or a Browser side problem because debugging isn't working. Maybe i'll try to force globalization settings for UI Thread later)

Or give a warning that this can happen and avoid parsing doubles altogether.

Describe alternatives you've considered

As for now, changing locale of browser to one that uses decimal separator as yours are works as a fix, but it's not consumer friendly...

Additional context

No response

@stevemonaco
Copy link
Contributor

I suspect your MathConverter implementation is not using an InvariantCulture as that ConverterParameter should be string data as far as Avalonia is concerned.

@timunie
Copy link
Contributor

timunie commented Jul 5, 2024

Can you please file a minimum sample or ar least the converter impl? I guess you just need to consume the correct culture. CultureInfo class has some info about current culture btw.

@ZecosMAX
Copy link
Author

ZecosMAX commented Jul 5, 2024

@stevemonaco @timunie finally got time to check for culture.
Consuming culture from converter argument didn't help, but setting InvariantCulture to double.Parse did

public class MathConverter : IValueConverter
{
    object? IValueConverter.Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        if (value == null)
            return value;

        if (parameter == null)
            return value;

        if (value is double v && parameter is string p && targetType.IsAssignableTo(typeof(double)))
        {
            var args = p.Split(' ', StringSplitOptions.RemoveEmptyEntries);

            if (args.Length % 2 != 0)
                throw new ArgumentException("ConverterParameter should contain '{operation} {value}'");

            for (var i = 0; i < args.Length; i += 2)
            {
                double opv = double.Parse(args[i + 1], CultureInfo.InvariantCulture); // <- Added InvariantCulture here
                v = args[i + 0] switch
                {
                    "div" => v / opv,
                    "mul" => v * opv,
                    "add" => v + opv,
                    "sub" => v - opv,
                    _ => v,
                };
            }

            return v;
        }
        else
        {
            return value;
        }
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

So, now it works with all browsers that i've tested previously.
Probably can close this issue now

@maxkatz6
Copy link
Member

maxkatz6 commented Jul 6, 2024

Culture passed in the converter is a UI culture by default, retried from the .NET BCL. Unless it was redefined in the Binding - which is the way how you can force it.

But yes, if you want it to be always invariant in the converter - just use CultureInfo.InvariantCulture

@maxkatz6 maxkatz6 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants