Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localising a WPF application doesn't work?

I must be missing something here.

I create a brand new WPF application in VS2015. I create a resource 'String1' and set the value to 'fksdlfdskfs'.

I update the default MainWindow.xaml.cs 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 below 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 into my WpfApplication5.csproj file and reload the project in VS:

<UICulture>en-US</UICulture>

And then uncommented the following line in AssemblyInfo.cs:

[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]

If I now go to run the application, the application no longer runs and I get the following exception on the line where I read the resource:

System.Resources.MissingManifestResourceException: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "WpfApplication5.Properties.Resources.en-US.resources" was correctly embedded or linked into assembly "WpfApplication5" at compile time, or that all the satellite assemblies required are loadable 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 locate resource 'mainwindow.xaml'

What am I doing wrong or what am I missing?

like image 376
GoldieLocks Avatar asked Jul 27 '17 13:07

GoldieLocks


1 Answers

You're not forced to use code behind for localization, you can simply use 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

Screen resource file

The error message you get typically means you do not have a Resource.en-US.resx file because [assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] is here to tell your app to use en en-US resource file as default source. Add a file named Resources.en-US.resx if you want to get rid of the error the quick way

What I personally do to localize a WPF app is :

  • I leave AssemblyInfo.cs as it is, which means that Resource.resx (without language id) file will be the default (which is generally en-US)
  • I create additional Resource.{id}.resx file next to default like this : sample,

    It is usually the same as Resource.resx but translated in the matching language

  • I force the culture at startup (typically in the App.xaml.cs) with a user settable language id so user can change application language :
// 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
like image 197
Uwy Avatar answered Nov 12 '22 02:11

Uwy