Localizing WPF Application Not Working?
I am missing something.
I am creating a new WPF Application in VS2015. I create resource 'String1' and set the value to 'fksdlfdskfs'.
I am updating the default MainWindow.xaml.cs file so that the constructor has:
public MainWindow()
{
InitializeComponent();
this.Title = Properties.Resources.String1;
}
And run the application and it works fine, my window title is fksdlfdskfs.
In the AssemblyInfo.cs file, I see the following comments:
//In order to begin building localizable applications, set
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
//inside a <PropertyGroup>. For example, if you are using US english
//in your source files, set the <UICulture> to en-US. Then uncomment
//the NeutralResourceLanguage attribute below. Update the "en-US" in
//the line below to match the UICulture setting in the project file.
//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
So, I add the following to my WpfApplication5.csproj file and reload the project in VS:
<UICulture>en-US</UICulture>
And then uncomment the following line in AssemblyInfo.cs:
[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
If I run the application now, the application does not start anymore and I get the following exception on the line where I read the resource:
System.Resources.MissingManifestResourceException: Could not find resources suitable for the specified culture or culture neutral. Make sure "WpfApplication5.Properties.Resources.en-US.resources" was properly embedded or linked into assembly "WpfApplication5" at compile time, or that all required satellite assemblies are downloadable and fully signed.
If I change UltimateResourceFallbackLocation.Satellite
to UltimateResourceFallbackLocation.MainAssembly
in the AssemblyInfo.cs file, I get the following exception instead:
System.IO.IOException: Cannot find resource 'mainwindow.xaml'
What am I doing wrong or what am I missing?
source to share
You don't have to use localization code, you can just use a x:Static
markup extension to bind to static fields:
<Window
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:properties="clr-namespace:SandBox.Properties"
Title="{x:Static properties:Resources.TitleSandbox}">
</Window>
Just make sure your resource file access modifier is set to Public
The error message you usually get means you don't have the file Resource.en-US.resx
because [assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
here is to tell your application to use the en en-US resource file as the default source. Add a file named Resources.en-US.resx
if you want to get rid of the error in a quick way
What I personally do to localize my WPF application:
- I leave it
AssemblyInfo.cs
as is, which means the fileResource.resx
(no language) will be the default (usually en-US) -
I create an additional file
Resource.{id}.resx
next to the default as follows: ,It is usually the same as
Resource.resx
, but translated in the matching language - I force the culture on startup (usually in
App.xaml.cs
) with a custom defined language id so that the user can change the language of the application:
// language is typically "en", "fr" and so on
var culture = new CultureInfo(language);
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
// You'll need to restart the app if you do this after application init
source to share