2010-12-23 69 views
0

我需要知道如何格式化給定的數字(或日期,或其他) 總是意大利語,在客戶端是什麼國家,無論...的Silverlight:如何十個分量相同的國產化,爲所有國家

例子:

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

必須在每一個國家返回 「1.520,45€」 被執行。 即使意大利語言未安裝在該機器中。

我該如何做到這一點? (可能是更好的,如果我可以做它應用程序範圍內)

在此先感謝。

回答

0

您可以明確設置Silverlight應用程序的UICulture和Culture,以確保無論用戶區域設置如何,UICulture和Culture都將得到修復。

這可以通過兩種方式

1集的對象標籤上的瀏覽器

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

即使在沒有安裝意大利語的環境? – ccen 2010-12-23 15:45:42