开发者

Silverlight: How to mantain the same localization for all countries

I need to know how to format a given number (or date, or whatever) always italian language, no matter in what country the client is...

Example:

<TextBlock Text={Binding Price, StringFormat=C2} />

must return "€ 1.520,45" in every country is executed. even if Italian language is not installed in that machine.

How can i achiev开发者_如何转开发e that? (possibly is better if i can do it application wide)

Thanks in advance.


You can set the UICulture and Culture of the Silverlight application explicitly to ensure that regardless of the user locale the UICulture and Culture would be fixed.

This can be achieved in two ways

1- Set in the object tag on the browser

<param name="uiculture" value="it-IT" />
<param name="culture" value="it-IT" />

2- Set the thread culture in the Application_Startup

Thread.CurrentThread.CurrentCulture = new CultureInfo("it-IT");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("it-IT");

Update: The above does not seem to take effect when using StringFormat. Given this, I would revert to using a custom value converter. Below is a sample

MainPage.xaml

<UserControl x:Class="SLLocalizationTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:SLLocalizationTest" 
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
  <UserControl.Resources>
    <local:DoubleToStringConverter x:Key="DoubleToStringConverter" />
  </UserControl.Resources>
  <Grid x:Name="LayoutRoot" Background="White">
    <StackPanel>
      <TextBlock Text="{Binding Price, Converter={StaticResource DoubleToStringConverter}, ConverterParameter=C2 }"/>
      <TextBlock Text="{Binding Price, Converter={StaticResource DoubleToStringConverter} }"/>
    </StackPanel>
  </Grid>
</UserControl>

MainPage.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Globalization;
using System.Windows.Data;

namespace SLLocalizationTest
{
  public partial class MainPage : UserControl
  {
    public MainPage()
    {
      InitializeComponent();
      DataContext = this;
    }

    public double Price
    {
      get { return 12353.23; }
    }
  }

  public class DoubleToStringConverter : IValueConverter
  {
    public object Convert(object value, Type targetType, 
      object parameter, CultureInfo culture)
    {
      if (value is double)
      {
        return ((double)value).ToString((string)parameter);
      }      
      return value.ToString();    
    }

    public object ConvertBack(object value, Type targetType, 
      object parameter, CultureInfo culture)
    {
      throw new NotImplementedException();
    }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜