Search code examples
wpfvb.netresourcedictionary

Get a string from resource file


I looked at this sample :

http://www.dotnetfunda.com/articles/article811-simplest-way-to-implement-multilingual-wpf-application.aspx

However, this sample shows how to get strings into the GUI.

How do I get a string into a variable.

I want to display a messagebox , with a string from the resource file.

Thanks in advance Guy


Solution

  • You can you use the ResourceDictionary.Item this is the example code modified to make it do what you want it to.

    Class MainWindow
        Dim dict As ResourceDictionary = New ResourceDictionary()
        Public Sub New()
    
            ' This call is required by the designer.
            InitializeComponent()
            SetLanguageDictionary()
            MessageBox.Show(dict.Item("greeting").ToString)
        End Sub
    
        Private Sub SetLanguageDictionary()
    
            Select Case (Thread.CurrentThread.CurrentCulture.ToString())
                Case "en-US"
                    dict.Source = New Uri("..\Resources\StringResources.xaml", UriKind.Relative)
                Case "fr-CA"
                    dict.Source = New Uri("..\Resources\StringResources.fr-CA.xaml", UriKind.Relative)
                Case Else
                    dict.Source = New Uri("..\Resources\StringResources.xaml", UriKind.Relative)
            End Select
        End Sub
    End Class
    

    and the ResourceFile I was using

    <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="greeting">Hello World</system:String>
    </ResourceDictionary>