WPFでローカリゼーションアプリケーションを作成しました。このようにxamlのコントロールをバインドしました。
<Label Name="languageLabel" Content="{x:Static Resources:Resources.languageLabel}" Style="{StaticResource CommonRepSecStyle}" />
すべてが正常に機能しますが、それでも私が適用した手法が優れているかどうかはわかりません。リソースマネージャーを作成し、このようなコントロールに文字列値を割り当てました。
Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture(cultureName)
Dim localResource As System.Resources.ResourceManager = New System.Resources.ResourceManager("LocalTest.Resources", System.Reflection.Assembly.GetExecutingAssembly())
languageLabel.Content = localResource.GetString("languageLabel", culture)
stateLabel.Content = localResource.GetString("stateLabel", culture)
cityLabel.Content = localResource.GetString("cityLabel", culture)
stateComboBox.Text = localResource.GetString("stateComboBox", culture)
cityComboBox.Text = localResource.GetString("cityComboBox", culture)
infoLabel.Content = localResource.GetString("infoLabel", culture)
infoTextBox.Text = localResource.GetString("infoTextBox", culture)
localResources.ReleaseAllResources()
コントロールに値を割り当てるまで機能しないようですか?これを改善する方法はありますか?ありがとうございました!
編集:完全なコードはここにあります
Imports System.IO
Imports System.Windows.Controls
Imports System.Globalization
Imports System.Configuration
Imports System.Threading
Imports System.ComponentModel
Imports System.Resources
Imports System.Windows.Markup
Imports System.Reflection
Imports System.Uri
Namespace LocalTest
Public Class MainWindow
Private cultureName As String
Public Sub New()
' ' ' This call is required by the designer.
InitializeComponent()
' ' ' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
End Sub
Private Sub ButtonEnglish_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles ButtonEnglish.Click
cultureName = "en-US"
SetControlsCulture()
'Dim ob As Object
'ob = Application.Current.TryFindResource("Resources.en-US.resx")
'Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
'Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("en-US")
End Sub
Private Sub ButtonFrench_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles ButtonFrench.Click
cultureName = "fr-CA"
System.Threading.Thread.CurrentThread.CurrentUICulture = New CultureInfo("fr-CA")
SetControlsCulture()
End Sub
' Dim s As String = r.GetString("LabelContent", culture)
'Thread.CurrentThread.CurrentCulture = culture
'Thread.CurrentThread.CurrentUICulture = culture
''' <summary>
''' Select the Controls Culture
''' </summary>
''' <remarks></remarks>
Private Sub SetControlsCulture()
Select Case cultureName
Case "fr-CA"
UIControlsBinding()
Case "en-US"
UIControlsBinding()
End Select
End Sub
Private Sub UIControlsBinding()
Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture(cultureName)
Dim localResource As System.Resources.ResourceManager = New System.Resources.ResourceManager("LocalTest.Resources", System.Reflection.Assembly.GetExecutingAssembly())
languageLabel.Content = localResource.GetString("languageLabel", culture)
stateLabel.Content = localResource.GetString("stateLabel", culture)
cityLabel.Content = localResource.GetString("cityLabel", culture)
stateComboBox.Text = localResource.GetString("stateComboBox", culture)
cityComboBox.Text = localResource.GetString("cityComboBox", culture)
infoLabel.Content = localResource.GetString("infoLabel", culture)
infoTextBox.Text = localResource.GetString("infoTextBox", culture)
localResources.ReleaseAllResources()
End Sub
End class
End Namespace