Silverlight:如何为所有国家/地区保持相同的本地化
我需要知道如何格式化给定的数字(或日期,或其他) 始终使用意大利语,无论客户在哪个国家 例如:Silverlight:如何为所有国家/地区保持相同的本地化,silverlight,localization,culture,string-formatting,Silverlight,Localization,Culture,String Formatting,我需要知道如何格式化给定的数字(或日期,或其他) 始终使用意大利语,无论客户在哪个国家 例如: <TextBlock Text={Binding Price, StringFormat=C2} /> 必须在每个国家/地区返回“€1.520,45”。 即使机器上没有安装意大利语 我怎样才能做到这一点? (如果我能在应用程序范围内完成,可能会更好) 提前感谢。您可以显式设置Silverlight应用程序的UICulture和Culture,以确保无论用户语言环境如何,UICultu
<TextBlock Text={Binding Price, StringFormat=C2} />
必须在每个国家/地区返回“€1.520,45”。
即使机器上没有安装意大利语
我怎样才能做到这一点?
(如果我能在应用程序范围内完成,可能会更好)
提前感谢。您可以显式设置Silverlight应用程序的UICulture和Culture,以确保无论用户语言环境如何,UICulture和Culture都是固定的 这可以通过两种方式实现 1-在浏览器上的对象标记中设置
<param name="uiculture" value="it-IT" />
<param name="culture" value="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>
即使在没有安装意大利语的环境中?@ccen,是的,这仍然有效,Windows为大多数地区维护了所有UI区域性设置的列表。我在silverlight业务应用程序的应用程序启动中添加了两行,但结果仍然是美元:“1520.45美元”我找到的唯一方法是在我需要格式化的每个usercontrol的每个构造函数中添加Me.Language=XmlLanguage.GetLanguage(“it”),这种方法仍然有效?@ccen,在创建根visual之前添加了这两行吗?这些应该是应用程序启动的前两行。@ccen,这似乎是StringFormat的一个限制(bug?)。现在,您需要设置语言。就我个人而言,我只需要使用一个IValueConverter并使用ToString来处理格式化。
<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>
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();
}
}
}