How to: Use a ResourceDictionary to Manage Localizable String Resources
This example shows how to use a ResourceDictionary to package localizable string resources for Windows Presentation Foundation (WPF) applications.
To use a ResourceDictionary to manage localizable string resources
Create a ResourceDictionary that contains the strings you would like to localize. The following code shows an example.
<ResourceDictionary xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" xmlns:system="clr-namespace:System;assembly=mscorlib"> <!-- String resource that can be localized --> <system:String x:Key="localizedMessage">en-US Message</system:String> </ResourceDictionary>
This code defines a string resource, localizedMessage, of type String, from the System namespace in mscorlib.dll.
Add the ResourceDictionary to your application, using the following code.
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="StringResources.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
Use the string resource from markup, using Extensible Application Markup Language (XAML) like the following.
<!-- Declarative use of string resource from StringResources.xaml resource dictionary --> <TextBox DockPanel.Dock="Top" Text="{StaticResource localizedMessage}" />
Use the string resource from code-behind, using code like the following.
' Programmatic use of string resource from StringResources.xaml resource dictionary Dim localizedMessage As String = CStr(Application.Current.FindResource("localizedMessage")) MessageBox.Show(localizedMessage)
// Programmatic use of string resource from StringResources.xaml resource dictionary string localizedMessage = (string)Application.Current.FindResource("localizedMessage"); MessageBox.Show(localizedMessage);
Localize the application. For more information, see How to: Localize an Application.