teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

4

見直しキャンペーン中

2023/07/17 04:34

投稿

TN8001
TN8001

スコア10114

answer CHANGED
@@ -1,162 +1,164 @@
1
- アプリケーション全体で切り替えてよければ、
2
- `this.Resources.MergedDictionaries.Add(dictionary);//←この部分`を
3
- `Application.Current.Resources.MergedDictionaries.Add(dictionary);`に
4
- するのが簡単でしょうか。
5
-
6
- ---
7
- 追記 メインウィンドウと言語設定ダイアログがあると思い、こんな感じになりましたがどうでしょうか。
8
-
9
- MainWindow.xaml
10
- ```xaml
11
- <Window
12
- x:Class="Questions._224205.MainWindow"
13
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
14
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
15
- Title="MainWindow"
16
- Width="800"
17
- Height="450">
18
- <StackPanel>
19
- <Label Content="{DynamicResource language}" />
20
- <Button Click="Button_Click" Content="{DynamicResource setting}" />
21
- </StackPanel>
22
- </Window>
23
- ```
24
- MainWindow.xaml.cs
25
- ```cs
26
- using System;
27
- using System.Windows;
28
-
29
- namespace Questions._224205
30
- {
31
- public partial class MainWindow : Window
32
- {
33
- // 何か設定ファイルから読んだとして
34
- internal static string NowLanguage = "ja-JP";
35
- public MainWindow()
36
- {
37
- InitializeComponent();
38
- SetLanguage(NowLanguage);
39
- }
40
- private void Button_Click(object sender, RoutedEventArgs e)
41
- => new Language().ShowDialog();
42
-
43
- public static void SetLanguage(string cultureCode)
44
- {
45
- var dictionary = new ResourceDictionary
46
- {
47
- Source = new Uri(@"Resources/Language/StringResource." + cultureCode + @".xaml", UriKind.Relative)
48
- };
49
- Application.Current.Resources.MergedDictionaries.Clear();
50
- Application.Current.Resources.MergedDictionaries.Add(dictionary);
51
- }
52
- }
53
- }
54
- ```
55
- Language.xaml
56
- ```xaml
57
- <Window
58
- x:Class="Questions._224205.Language"
59
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
60
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
61
- Title="{DynamicResource setting}"
62
- Width="800"
63
- Height="450"
64
- WindowStartupLocation="CenterScreen">
65
- <StackPanel>
66
- <TextBlock x:Name="TestTextBox" Text="{DynamicResource language}" />
67
- <RadioButton
68
- x:Name="radioButton_ENG"
69
- VerticalContentAlignment="Center"
70
- Checked="RadioButtonENG_Checked"
71
- Content="English" />
72
- <RadioButton
73
- x:Name="radioButton_JPA"
74
- VerticalContentAlignment="Center"
75
- Checked="RadioButtonJPA_Checked"
76
- Content="日本語" />
77
- </StackPanel>
78
- </Window>
79
- ```
80
- Language.xaml.cs
81
- ```cs
82
- using System.Windows;
83
-
84
- namespace Questions._224205
85
- {
86
- public partial class Language : Window
87
- {
88
- public Language()
89
- {
90
- InitializeComponent();
91
-
92
- // 雑すぎますが本題ではないので。。
93
- switch(MainWindow.NowLanguage)
94
- {
95
- case "en-US":
96
- radioButton_ENG.IsChecked = true;
97
- break;
98
- case "ja-JP":
99
- radioButton_JPA.IsChecked = true;
100
- break;
101
- }
102
- }
103
-
104
- private void RadioButtonENG_Checked(object sender, RoutedEventArgs e)
105
- // 雑すぎますが本題ではないので。。
106
- => MainWindow.SetLanguage("en-US");
107
-
108
- private void RadioButtonJPA_Checked(object sender, RoutedEventArgs e)
109
- => MainWindow.SetLanguage("ja-JP");
110
- }
111
- }
112
- ```
113
- StringResource.en-us.xaml
114
- ```xaml
115
- <ResourceDictionary
116
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
117
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
118
- xmlns:system="clr-namespace:System;assembly=mscorlib">
119
- <system:String x:Key="language">Language</system:String>
120
- <system:String x:Key="setting">setting</system:String>
121
- </ResourceDictionary>
122
- ```
123
- StringResource.ja-jp.xaml
124
- ```xaml
125
- <ResourceDictionary
126
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
127
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
128
- xmlns:system="clr-namespace:System;assembly=mscorlib">
129
- <system:String x:Key="language">言語</system:String>
130
- <system:String x:Key="setting">設定</system:String>
131
- </ResourceDictionary>
132
- ```
133
- ![イメージ説明](5f42c41892cb0664d8a1ed9db1bf37e2.png)
134
-
135
- ---
136
- 追記 画像の場合
137
- StringResource.ja-jp.xaml
138
- ```xaml
139
- <ResourceDictionary
140
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
141
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
142
- xmlns:system="clr-namespace:System;assembly=mscorlib">
143
- <BitmapImage x:Key="ButtonImage" UriSource="/Questions.224205;component/Resources/Image/ButtonImage_jpa.png" />
144
- </ResourceDictionary>
145
- ```
146
- `Questions.224205`の部分はアセンブリ名(マウスで簡単に指定できます)
147
- ![イメージ説明](9b69bdf8168d3322fc2369c84cd24e14.png)
148
-
149
- MainWindow.xaml
150
- ```xaml
151
- <Window
152
- x:Class="Questions._224205.MainWindow"
153
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
154
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
155
- Title="MainWindow"
156
- Width="800"
157
- Height="450">
158
- <StackPanel>
159
- <Image Source="{DynamicResource ButtonImage}" />
160
- </StackPanel>
161
- </Window>
1
+ アプリケーション全体で切り替えてよければ、
2
+ `this.Resources.MergedDictionaries.Add(dictionary);//←この部分`を
3
+ `Application.Current.Resources.MergedDictionaries.Add(dictionary);`に
4
+ するのが簡単でしょうか。
5
+
6
+ ---
7
+
8
+ 追記
9
+ メインウィンドウと言語設定ダイアログがあると思い、こんな感じになりましたがどうでしょうか。
10
+
11
+
12
+ ```xml:MainWindow.xaml
13
+ <Window
14
+ x:Class="Questions._224205.MainWindow"
15
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
16
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
17
+ Title="MainWindow"
18
+ Width="800"
19
+ Height="450">
20
+ <StackPanel>
21
+ <Label Content="{DynamicResource language}" />
22
+ <Button Click="Button_Click" Content="{DynamicResource setting}" />
23
+ </StackPanel>
24
+ </Window>
25
+ ```
26
+
27
+ ```cs:MainWindow.xaml.cs
28
+ using System;
29
+ using System.Windows;
30
+
31
+ namespace Questions._224205
32
+ {
33
+ public partial class MainWindow : Window
34
+ {
35
+ // 何か設定ファイルから読んだとして
36
+ internal static string NowLanguage = "ja-JP";
37
+ public MainWindow()
38
+ {
39
+ InitializeComponent();
40
+ SetLanguage(NowLanguage);
41
+ }
42
+ private void Button_Click(object sender, RoutedEventArgs e)
43
+ => new Language().ShowDialog();
44
+
45
+ public static void SetLanguage(string cultureCode)
46
+ {
47
+ var dictionary = new ResourceDictionary
48
+ {
49
+ Source = new Uri(@"Resources/Language/StringResource." + cultureCode + @".xaml", UriKind.Relative)
50
+ };
51
+ Application.Current.Resources.MergedDictionaries.Clear();
52
+ Application.Current.Resources.MergedDictionaries.Add(dictionary);
53
+ }
54
+ }
55
+ }
56
+ ```
57
+
58
+ ```xml:Language.xaml
59
+ <Window
60
+ x:Class="Questions._224205.Language"
61
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
62
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
63
+ Title="{DynamicResource setting}"
64
+ Width="800"
65
+ Height="450"
66
+ WindowStartupLocation="CenterScreen">
67
+ <StackPanel>
68
+ <TextBlock x:Name="TestTextBox" Text="{DynamicResource language}" />
69
+ <RadioButton
70
+ x:Name="radioButton_ENG"
71
+ VerticalContentAlignment="Center"
72
+ Checked="RadioButtonENG_Checked"
73
+ Content="English" />
74
+ <RadioButton
75
+ x:Name="radioButton_JPA"
76
+ VerticalContentAlignment="Center"
77
+ Checked="RadioButtonJPA_Checked"
78
+ Content="日本語" />
79
+ </StackPanel>
80
+ </Window>
81
+ ```
82
+
83
+ ```cs:Language.xaml.cs
84
+ using System.Windows;
85
+
86
+ namespace Questions._224205
87
+ {
88
+ public partial class Language : Window
89
+ {
90
+ public Language()
91
+ {
92
+ InitializeComponent();
93
+
94
+ // 雑すぎますが本題ではないので。。
95
+ switch(MainWindow.NowLanguage)
96
+ {
97
+ case "en-US":
98
+ radioButton_ENG.IsChecked = true;
99
+ break;
100
+ case "ja-JP":
101
+ radioButton_JPA.IsChecked = true;
102
+ break;
103
+ }
104
+ }
105
+
106
+ private void RadioButtonENG_Checked(object sender, RoutedEventArgs e)
107
+ // 雑すぎますが本題ではないので。。
108
+ => MainWindow.SetLanguage("en-US");
109
+
110
+ private void RadioButtonJPA_Checked(object sender, RoutedEventArgs e)
111
+ => MainWindow.SetLanguage("ja-JP");
112
+ }
113
+ }
114
+ ```
115
+
116
+ ```xml:StringResource.en-us.xaml
117
+ <ResourceDictionary
118
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
119
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
120
+ xmlns:system="clr-namespace:System;assembly=mscorlib">
121
+ <system:String x:Key="language">Language</system:String>
122
+ <system:String x:Key="setting">setting</system:String>
123
+ </ResourceDictionary>
124
+ ```
125
+
126
+ ```xml:StringResource.ja-jp.xaml
127
+ <ResourceDictionary
128
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
129
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
130
+ xmlns:system="clr-namespace:System;assembly=mscorlib">
131
+ <system:String x:Key="language">言語</system:String>
132
+ <system:String x:Key="setting">設定</system:String>
133
+ </ResourceDictionary>
134
+ ```
135
+ ![イメージ説明](5f42c41892cb0664d8a1ed9db1bf37e2.png)
136
+
137
+ ---
138
+
139
+ 追記 画像の場合
140
+
141
+ ```xml:StringResource.ja-jp.xaml
142
+ <ResourceDictionary
143
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
144
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
145
+ xmlns:system="clr-namespace:System;assembly=mscorlib">
146
+ <BitmapImage x:Key="ButtonImage" UriSource="/Questions.224205;component/Resources/Image/ButtonImage_jpa.png" />
147
+ </ResourceDictionary>
148
+ ```
149
+ `Questions.224205`の部分はアセンブリ名(マウスで簡単に指定できます)
150
+ ![イメージ説明](9b69bdf8168d3322fc2369c84cd24e14.png)
151
+
152
+ ```xml:MainWindow.xaml
153
+ <Window
154
+ x:Class="Questions._224205.MainWindow"
155
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
156
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
157
+ Title="MainWindow"
158
+ Width="800"
159
+ Height="450">
160
+ <StackPanel>
161
+ <Image Source="{DynamicResource ButtonImage}" />
162
+ </StackPanel>
163
+ </Window>
162
164
  ```

3

コード修正

2019/11/20 04:38

投稿

TN8001
TN8001

スコア10114

answer CHANGED
@@ -46,6 +46,7 @@
46
46
  {
47
47
  Source = new Uri(@"Resources/Language/StringResource." + cultureCode + @".xaml", UriKind.Relative)
48
48
  };
49
+ Application.Current.Resources.MergedDictionaries.Clear();
49
50
  Application.Current.Resources.MergedDictionaries.Add(dictionary);
50
51
  }
51
52
  }

2

追記 画像の場合

2019/11/20 04:38

投稿

TN8001
TN8001

スコア10114

answer CHANGED
@@ -129,4 +129,33 @@
129
129
  <system:String x:Key="setting">設定</system:String>
130
130
  </ResourceDictionary>
131
131
  ```
132
- ![イメージ説明](5f42c41892cb0664d8a1ed9db1bf37e2.png)
132
+ ![イメージ説明](5f42c41892cb0664d8a1ed9db1bf37e2.png)
133
+
134
+ ---
135
+ 追記 画像の場合
136
+ StringResource.ja-jp.xaml
137
+ ```xaml
138
+ <ResourceDictionary
139
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
140
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
141
+ xmlns:system="clr-namespace:System;assembly=mscorlib">
142
+ <BitmapImage x:Key="ButtonImage" UriSource="/Questions.224205;component/Resources/Image/ButtonImage_jpa.png" />
143
+ </ResourceDictionary>
144
+ ```
145
+ `Questions.224205`の部分はアセンブリ名(マウスで簡単に指定できます)
146
+ ![イメージ説明](9b69bdf8168d3322fc2369c84cd24e14.png)
147
+
148
+ MainWindow.xaml
149
+ ```xaml
150
+ <Window
151
+ x:Class="Questions._224205.MainWindow"
152
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
153
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
154
+ Title="MainWindow"
155
+ Width="800"
156
+ Height="450">
157
+ <StackPanel>
158
+ <Image Source="{DynamicResource ButtonImage}" />
159
+ </StackPanel>
160
+ </Window>
161
+ ```

1

コード追記

2019/11/20 02:52

投稿

TN8001
TN8001

スコア10114

answer CHANGED
@@ -1,4 +1,132 @@
1
1
  アプリケーション全体で切り替えてよければ、
2
2
  `this.Resources.MergedDictionaries.Add(dictionary);//←この部分`を
3
3
  `Application.Current.Resources.MergedDictionaries.Add(dictionary);`に
4
- するのが簡単でしょうか。
4
+ するのが簡単でしょうか。
5
+
6
+ ---
7
+ 追記 メインウィンドウと言語設定ダイアログがあると思い、こんな感じになりましたがどうでしょうか。
8
+
9
+ MainWindow.xaml
10
+ ```xaml
11
+ <Window
12
+ x:Class="Questions._224205.MainWindow"
13
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
14
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
15
+ Title="MainWindow"
16
+ Width="800"
17
+ Height="450">
18
+ <StackPanel>
19
+ <Label Content="{DynamicResource language}" />
20
+ <Button Click="Button_Click" Content="{DynamicResource setting}" />
21
+ </StackPanel>
22
+ </Window>
23
+ ```
24
+ MainWindow.xaml.cs
25
+ ```cs
26
+ using System;
27
+ using System.Windows;
28
+
29
+ namespace Questions._224205
30
+ {
31
+ public partial class MainWindow : Window
32
+ {
33
+ // 何か設定ファイルから読んだとして
34
+ internal static string NowLanguage = "ja-JP";
35
+ public MainWindow()
36
+ {
37
+ InitializeComponent();
38
+ SetLanguage(NowLanguage);
39
+ }
40
+ private void Button_Click(object sender, RoutedEventArgs e)
41
+ => new Language().ShowDialog();
42
+
43
+ public static void SetLanguage(string cultureCode)
44
+ {
45
+ var dictionary = new ResourceDictionary
46
+ {
47
+ Source = new Uri(@"Resources/Language/StringResource." + cultureCode + @".xaml", UriKind.Relative)
48
+ };
49
+ Application.Current.Resources.MergedDictionaries.Add(dictionary);
50
+ }
51
+ }
52
+ }
53
+ ```
54
+ Language.xaml
55
+ ```xaml
56
+ <Window
57
+ x:Class="Questions._224205.Language"
58
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
59
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
60
+ Title="{DynamicResource setting}"
61
+ Width="800"
62
+ Height="450"
63
+ WindowStartupLocation="CenterScreen">
64
+ <StackPanel>
65
+ <TextBlock x:Name="TestTextBox" Text="{DynamicResource language}" />
66
+ <RadioButton
67
+ x:Name="radioButton_ENG"
68
+ VerticalContentAlignment="Center"
69
+ Checked="RadioButtonENG_Checked"
70
+ Content="English" />
71
+ <RadioButton
72
+ x:Name="radioButton_JPA"
73
+ VerticalContentAlignment="Center"
74
+ Checked="RadioButtonJPA_Checked"
75
+ Content="日本語" />
76
+ </StackPanel>
77
+ </Window>
78
+ ```
79
+ Language.xaml.cs
80
+ ```cs
81
+ using System.Windows;
82
+
83
+ namespace Questions._224205
84
+ {
85
+ public partial class Language : Window
86
+ {
87
+ public Language()
88
+ {
89
+ InitializeComponent();
90
+
91
+ // 雑すぎますが本題ではないので。。
92
+ switch(MainWindow.NowLanguage)
93
+ {
94
+ case "en-US":
95
+ radioButton_ENG.IsChecked = true;
96
+ break;
97
+ case "ja-JP":
98
+ radioButton_JPA.IsChecked = true;
99
+ break;
100
+ }
101
+ }
102
+
103
+ private void RadioButtonENG_Checked(object sender, RoutedEventArgs e)
104
+ // 雑すぎますが本題ではないので。。
105
+ => MainWindow.SetLanguage("en-US");
106
+
107
+ private void RadioButtonJPA_Checked(object sender, RoutedEventArgs e)
108
+ => MainWindow.SetLanguage("ja-JP");
109
+ }
110
+ }
111
+ ```
112
+ StringResource.en-us.xaml
113
+ ```xaml
114
+ <ResourceDictionary
115
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
116
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
117
+ xmlns:system="clr-namespace:System;assembly=mscorlib">
118
+ <system:String x:Key="language">Language</system:String>
119
+ <system:String x:Key="setting">setting</system:String>
120
+ </ResourceDictionary>
121
+ ```
122
+ StringResource.ja-jp.xaml
123
+ ```xaml
124
+ <ResourceDictionary
125
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
126
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
127
+ xmlns:system="clr-namespace:System;assembly=mscorlib">
128
+ <system:String x:Key="language">言語</system:String>
129
+ <system:String x:Key="setting">設定</system:String>
130
+ </ResourceDictionary>
131
+ ```
132
+ ![イメージ説明](5f42c41892cb0664d8a1ed9db1bf37e2.png)