1
Vote

Problem with OS's with other culture than "en"

description

I'm trying to run the demo application, but it doesn't work on my Swedish Windows 7.
 
This code in Infusion.Silverlight.Controls.Helper.cs crashes:
 
public static Point ParsePoint(string pointAsString)
    {
        string xStr = pointAsString.Substring(0, pointAsString.IndexOf(","));
        string yStr = pointAsString.Substring(pointAsString.IndexOf(",") + 1);
 
        return new Point(double.Parse(xStr), double.Parse(yStr));
    }
 
pointAsString in is "0.5,0" which means that xStr="0.5" and yStr="0"
 
double.Parse(xStr) creates an exception, because in Swedish it is expecting "0,5" instead of "0.5"

comments

JamesHough wrote Mar 10, 2014 at 5:48 PM

Be sure to add a reference to the namespace ...
using System.Globalization;
Replace the problem code with this code...
{
    string xStr = pointAsString.Substring(0, 
        pointAsString.IndexOf(CultureInfo.InvariantCulture.TextInfo.ListSeparator));
    string yStr = pointAsString.Substring(
        pointAsString.IndexOf(CultureInfo.InvariantCulture.TextInfo.ListSeparator) 
        + CultureInfo.InvariantCulture.TextInfo.ListSeparator.Length);

    return new Point(
        double.Parse(xStr, CultureInfo.InvariantCulture.NumberFormat), 
        double.Parse(yStr, CultureInfo.InvariantCulture.NumberFormat));
}
Take note that this isn't the only place this must be done, there is a general problem with Internationalisation in the code. There are many places in the code that assume that the user's regional settings will match the US standard.


Helper.cs (164):
long num = long.Parse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
Helper.cs (249):
((RadialGradientBrush)brushObj).RadiusX = Convert.ToDouble(reader.Value, CultureInfo.InvariantCulture.NumberFormat);
Helper.cs (252):
((RadialGradientBrush)brushObj).RadiusY = Convert.ToDouble(reader.Value, CultureInfo.InvariantCulture.NumberFormat);
Helper.cs (283):
gradientStop.Offset = double.Parse(reader.Value, CultureInfo.InvariantCulture.NumberFormat);
Document.cs (185):
int newValue = Convert.ToInt32(args.NewValue, CultureInfo.InvariantCulture.NumberFormat);
Document.cs (452):
text.Text = Math.Abs((double)i / unitDivisions).ToString(
        CultureInfo.CurrentCulture.NumberFormat); // text displayed to user in own regional preference.
Document.cs (493):
for (int i = unitsToTheLeft * Convert.ToInt32(Units, CultureInfo.InvariantCulture.NumberFormat); 
        i <= unitsToTheRight * Convert.ToInt32(Units, CultureInfo.InvariantCulture.NumberFormat); 
        i++)
Document.cs (506):
text.Text = Math.Abs((double)i / unitDivisions).ToString(
         CultureInfo.CurrentCulture.NumberFormat); // text displayed to user in own regional preference.