How to start an internationalized WPF-Project in SharpDevelop 4.2?

前端 未结 1 1765
青春惊慌失措
青春惊慌失措 2021-01-03 06:34

I want to create a Software, where the User can choose between several Languages.

As a start i want to learn how to handle Internationalization, since i have never

相关标签:
1条回答
  • 2021-01-03 07:30

    If you created two ResourceDictionary files you can binding by DynamicResource.

    Example:

    First resource file (Lang.en-US.xaml):

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:system="clr-namespace:System;assembly=mscorlib">
    
        <system:String x:Key="Username">Username:</system:String>
        <system:String x:Key="Password">Password:</system:String>
        <system:String x:Key="close">Close</system:String>
        <system:String x:Key="login">Login</system:String>        
    </ResourceDictionary>
    

    Second resource file (Lang.pl-PL.xaml):

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:system="clr-namespace:System;assembly=mscorlib">
    
        <system:String x:Key="Username">Login:</system:String>
        <system:String x:Key="Password">Hasło:</system:String>
        <system:String x:Key="close">Zamknij</system:String>
        <system:String x:Key="login">Zaloguj</system:String>
    </ResourceDictionary>
    

    Set default language in Application resources:

     <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Lang.en-US.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
     </Application.Resources>
    

    Let's say that we have ComboBox like below:

    <ComboBox Name="cbLang" Margin="2" SelectionChanged="cbLang_SelectionChanged" >
                    <ComboBoxItem Content="English" Tag="en-US" />
                    <ComboBoxItem Content="Polish" Tag="pl-PL" />
      </ComboBox>
    

    Code-behind SelectionChanged:

     private void cbLang_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
            {
                ResourceDictionary dict = new ResourceDictionary();
    
                switch (((sender as ComboBox).SelectedItem as ComboBoxItem).Tag.ToString())
                {
                    case "en-US":
                        dict.Source = new Uri("Lang.en-US.xaml", UriKind.Relative);
                        break;
                    case "pl-PL":
                        dict.Source = new Uri("Lang.pl-PL.xaml", UriKind.Relative);
                        break;
                    default:
                        break;
                }
                this.Resources.MergedDictionaries.Add(dict);
            }
    

    And you can binding like this:

     <TextBlock Text="{DynamicResource Username}" VerticalAlignment="Center" />
    
    0 讨论(0)
提交回复
热议问题