0

I have created a localization application in WPF. I have bind the controls in the xaml like this.

<Label Name="languageLabel" Content="{x:Static Resources:Resources.languageLabel}" Style="{StaticResource CommonRepSecStyle}" />

Everything works fine but still I am not sure the techniques I applied are good. I created a resource manager and assigned the string values to controls like this.

        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()

It does not seems to work until I assigned the values to the controls? Is there any way to improve this? Thank you!

Edit : The complete code is here

    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
  • and why would you do that in code and not in XAML?? – Sten Petrov Mar 04 '13 at 20:20
  • I did it in the xaml. But it does not change the culture until I implement it in the code behind. – user1521601 Mar 04 '13 at 20:22
  • Check this idea: http://stackoverflow.com/questions/10039102/is-there-any-way-to-use-staticresource-in-a-wpf-control-library-and-be-able-to-v In a nutshell: define your own class type, add custom properties on it, make these custom properties dependent on current culture and bind to them. This way your code doesn't have to know resource keys – Sten Petrov Mar 04 '13 at 20:28

1 Answers1

1

To add resources in WPF add a Resource.resx and a Resource.NL-nl.resx (or language of choice) file to the properties folder of you project. Add a resource with name StateLabel and a value in each resourcefile. Don't forget to change the Access Modifier to Public

Add xml namespace to your window/usercontrol: xmlns:Resources="clr-namespace:WpfApplication2.Properties" (where WpfApplication2 is your application name)

Add a label to your window/usercontrol <Label Content="{x:Static Resources:Resources.StateLabel}"></Label>

To change language call this from code to set the appropriate language (in this case Dutch):

System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-NL");
Ralf de Kleine
  • 11,464
  • 5
  • 45
  • 87