#1,116 – An Example of Output that Obeys CurrentCulture

The first step in internationalizing an application is to respect the current regional settings when outputting numeric, date/time, or currency values.

Below is an example of an application that displays numeric and date/time values, using the ToString method for instances of double and DateTime.

    <StackPanel>
        <StackPanel Margin="10" Orientation="Horizontal">
            <StackPanel>
                <Label Name="lbl1"/>
                <Label Name="lbl2"/>
                <Label Name="lbl3"/>
                <Label Name="lbl4"/>
                <Label Name="lbl5"/>
            </StackPanel>
            <StackPanel>
                <Label Name="lbl6"/>
                <Label Name="lbl7"/>
            </StackPanel>
        </StackPanel>
        <Button Content="Gen Strings" Padding="10,5"
                Margin="10"
                Click="Button_Click"/>
    </StackPanel>

In the Click event handler, we set the labels’ content.

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DateTime rightNow = DateTime.Now;

            lbl1.Content = rightNow.ToString();
            lbl2.Content = rightNow.ToString("d");  // short date
            lbl3.Content = rightNow.ToString("D");  // long date
            lbl4.Content = rightNow.ToString("t");  // short time
            lbl5.Content = rightNow.ToString("T");  // long time

            double num = 123456.789;
            lbl6.Content = num.ToString();
            lbl7.Content = num.ToString("n");
        }

With regional settings set to English (United States), the output looks like this:

1116-001

If I change my region to French (France), the formatting changes as expected.

1116-003

1116-004

#1,115 – Internationalization I – Obey CurrentCulture for Output

The first step in internationalizing an application is to ensure that your application honors the user’s current regional settings, as reflected by the CurrentCulture property of the application’s main thread.

Current culture impacts the display of the following:

  • Numeric data
  • Date/time values
  • Currency values

All of these values are typically stored internally as numeric (e.g. double, int) or DateTime values.  These values exist in memory in a culture-agnostic form.  You only need to worry about regional settings when you display a value to the user.

In .NET, if you use the ToString method on a numeric or date/time object, with or without formatting strings, the resulting string will correctly reflect the current regional settings.

If you’re using data binding to convert numeric or date/time data to strings, you need to manually set the Language property on each window or page to force data binding to use the culture correctly.