【问题标题】:Silverlight: How to mantain the same localization for all countriesSilverlight:如何为所有国家/地区保持相同的本地化
【发布时间】:2010-12-23 15:30:37
【问题描述】:

我需要知道如何格式化给定的数字(或日期等) 始终使用意大利语,无论客户在哪个国家/地区...

例子:

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

必须在每个国家/地区执行“€ 1.520,45”。 即使那台机器上没有安装意大利语。

我怎样才能做到这一点? (如果我能在应用范围内做到这一点可能会更好)

提前致谢。

【问题讨论】:

    标签: silverlight localization culture string-formatting


    【解决方案1】:

    您可以明确设置 Silverlight 应用程序的 UICulture 和 Culture,以确保无论用户区域设置如何,UICulture 和 Culture 都会得到修复。

    这可以通过两种方式实现

    1-在浏览器的object标签中设置

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

    2- 在Application_Startup中设置线程文化

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

    更新:上面使用StringFormat的时候好像没有生效。鉴于此,我将恢复使用自定义值转换器。下面是一个示例

    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();
        }
      }
    }
    

    【讨论】:

    • 即使在没有安装意大利语的环境中?
    • @ccen,是的,这仍然有效,Windows 维护了大多数语言环境的所有 UI 文化设置的列表。
    • 我在 Silverlight 业务应用程序的应用程序启动中添加了 2 行,但结果仍然是美元:“$ 1,520.45”我发现的唯一方法是添加 Me.Language = XmlLanguage。 GetLanguage("it-IT") 在我需要格式化的每个用户控件的每个构造函数中,这种方式仍然有效吗?
    • @ccen,您是否在创建 Root 视觉对象之前添加了这两行?这些应该是 Application_Startup 中的前两行。
    • @ccen,这似乎是 StringFormat 的限制(错误?)。现在您需要设置语言。就个人而言,我只会使用 IValueConverter 并使用 ToString 来处理格式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多