简体   繁体   English

本地化WPF应用程序不起作用?

[英]Localising a WPF application doesn't work?

I must be missing something here. 我必须在这里遗漏一些东西。

I create a brand new WPF application in VS2015. 我在VS2015中创建了一个全新的WPF应用程序。 I create a resource 'String1' and set the value to 'fksdlfdskfs'. 我创建了一个资源'String1'并将值设置为'fksdlfdskfs'。

I update the default MainWindow.xaml.cs so that the constructor has: 我更新默认的MainWindow.xaml.cs,以便构造函数具有:

    public MainWindow()
    {
        InitializeComponent();
        this.Title = Properties.Resources.String1;
    }

And run the application, and it works fine, my window title is fksdlfdskfs. 并运行该应用程序,它工作正常,我的窗口标题是fksdlfdskfs。

In the AssemblyInfo.cs file I see the below comments: 在AssemblyInfo.cs文件中,我看到以下注释:

//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: 所以我将以下内容添加到我的WpfApplication5.csproj文件中并在VS中重新加载项目:

<UICulture>en-US</UICulture>

And then uncommented the following line in AssemblyInfo.cs: 然后在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. System.Resources.MissingManifestResourceException:找不到适合指定文化或中性文化的任何资源。 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. 确保在编译时将“WpfApplication5.Properties.Resources.en-US.resources”正确嵌入或链接到程序集“WpfApplication5”中,或者所有所需的附属程序集都是可加载和完全签名的。

If I change UltimateResourceFallbackLocation.Satellite to UltimateResourceFallbackLocation.MainAssembly in the AssemblyInfo.cs file, I get the following exception instead: 如果我改变UltimateResourceFallbackLocation.SatelliteUltimateResourceFallbackLocation.MainAssembly AssemblyInfo.cs文件中,我得到下面的异常,而不是:

System.IO.IOException: Cannot locate resource 'mainwindow.xaml' System.IO.IOException:找不到资源'mainwindow.xaml'

What am I doing wrong or what am I missing? 我做错了什么或我错过了什么?

You're not forced to use code behind for localization, you can simply use x:Static markup extension to bind to static fields: 你没有被迫使用代码来进行本地化,你可以简单地使用x:Static标记扩展来绑定到静态字段:

<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 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. 您获得的错误消息通常意味着您没有Resource.en-US.resx文件,因为[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]告诉您的应用使用en en-US资源file作为默认源。 Add a file named Resources.en-US.resx if you want to get rid of the error the quick way 如果要快速排除错误,请添加名为Resources.en-US.resx的文件

What I personally do to localize a WPF app is : 我个人做的本地化WPF应用程序是:

  • 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) 我按原样保留AssemblyInfo.cs ,这意味着Resource.resx (没有语言ID)文件将是默认文件(通常是en-US)
  • I create additional Resource.{id}.resx file next to default like this : 我在默认旁边创建了额外的Resource.{id}.resx文件,如下所示: 样品 ,

    It is usually the same as Resource.resx but translated in the matching language 它通常与Resource.resx相同,但在匹配语言中进行翻译

  • I force the culture at startup (typically in the App.xaml.cs ) with a user settable language id so user can change application language : 我在启动时(通常在App.xaml.cs )使用用户可设置的语言ID强制文化,以便用户可以更改应用程序语言:
// 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM