2017-01-25 15 views
0

現在WPF/XAMLでアプリケーションをビルドする必要があります。これは現在C#で行われています。
私もこのコードを書いていないので、すべてについて説明することはできません。WPF/XAMLで言語を変更する方法

ホームメニューでユーザーが選択したものにアプリの言語を変更するコードが必要です。これはC#で動作するコードです:

public static void TranslateForm(string Language, Form f) 
{ 
    try 
    { 
     string Sprachtext = string.Empty; 
     clstools tools = new clstools(string.Format(string.Format(clsGlobal.CONNECTION_STRING, clsGlobal.TNSNames, 
       clsGlobal.DBUser, clsGlobal.DBPassword)), clsGlobal.IDOPERATOR); 

     //caption text of the form :-) 
     try 
     { 
      if (f.Tag != null) 
      { 
       if (tools.IsNumeric(f.Tag.ToString()) == true) 
       { 
        Sprachtext = string.Empty; 

        if (tools.GetLanguageText(Convert.ToInt32(f.Tag.ToString()), Language, ref Sprachtext) == true) 
        { 
         f.Text = Sprachtext; 
        } 
       } 
      } 
     } 
     catch (Exception) 
     { 
      //ignore and proceed 
     } 

     foreach (Control c in f.Controls) 
     { 
      if (c.Tag != null) 
      { 
       if (string.IsNullOrEmpty(c.Tag.ToString()) == false) 
       { 
        if (tools.IsNumeric(c.Tag.ToString()) == true) 
        { 
         Sprachtext = string.Empty; 

         if (tools.GetLanguageText(Convert.ToInt32(c.Tag.ToString()), Language, ref Sprachtext) == true) 
         { 
          c.Text = Sprachtext; 
         } 
        } 
       } 
      } 
     } 
    } 
    catch (Exception) 
    { 
     //ignore 
    } 
} 

これに答える質問があれば教えてください。

また、この質問を改善するものがあれば教えてください。

+0

var culture = "en-GB"; Thread.CurrentThread.CurrentCulture =新しいCultureInfo(カルチャ); Thread.CurrentThread.CurrentUICulture =新しいCultureInfo(カルチャ); – jannagy02

答えて

0

ここRessourceDictionnary使用の基本的な例として、第1のコントローラ:

public partial class Example : Page 
{ 
    public Example() { 
     InitializeComponent(); 
     // In english 
     this.Resources.MergedDictionaries.Add("en")); 
     // In french 
     //this.Resources.MergedDictionaries.Add("fr")); 
    } 
    public ResourceDictionary setLanguageDictionary(string language) { 
     ResourceDictionary dict = new ResourceDictionary(); 
     switch (language) { 
      case "en": 
       dict.Source = new Uri(AppDomain.CurrentDomain.BaseDirectory + "Resources\\StringResources.en-GB.xaml", UriKind.Absolute); 
       break; 
      case "fr": 
       dict.Source = new Uri(AppDomain.CurrentDomain.BaseDirectory + "Resources\\StringResources.fr-FR.xaml", 
            UriKind.Absolute); 
       break; 
      default: 
       dict.Source = new Uri(AppDomain.CurrentDomain.BaseDirectory + "Resources\\StringResources.en-GB.xaml", 
            UriKind.Absolute); 
       break; 
     } 
     return dict; 
    } 
} 

ここでのXAMLのResourceDictionary(StringResources.en-GB.xaml)

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:SolutionName" 
    xmlns:system="clr-namespace:System;assembly=mscorlib"> 

    <system:String x:Key="offline">Offline mode</system:String> 
    <system:String x:Key="login">Login</system:String> 
    <system:String x:Key="domain">Domain:</system:String> 
    <system:String x:Key="username">Username:</system:String> 
    <system:String x:Key="password">Password:</system:String> 
    <system:String x:Key="channel">Channel</system:String> 

</ResourceDictionary> 

そして最後ビューのいずれか

<Page x:Class="SolutionName.Example" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:local="clr-namespace:Uuniti.Vue" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="300" 
    Title="Connexion1" 
    xmlns:system="clr-namespace:System;assembly=mscorlib"> 
<Grid> 
    <TextBlock Text="{DynamicResource channel}"/> 
</Grid> 
</Page> 
+0

ありがとうございます。私は明日それを試して、それが動作するかどうかを確認します。 – Luca

関連する問題