如何修改C#项目文件,以便将Resources.resx编译到UICulture的附属程序集中? [英] How can I modify my C# project file so that Resources.resx is compiled into the satellite assembly for my UICulture?

查看:357
本文介绍了如何修改C#项目文件,以便将Resources.resx编译到UICulture的附属程序集中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#的WPF应用程序,将来可能需要本地化.我想支持XAML/BAML本地化传统的resx本地化. (前者对于大多数代码很有用,但是一些本地化的内容来自视图模型,在这里使用resx文件更直接.)

I have a WPF application in C# that may need to be localized in the future. I want to support XAML/BAML localization and conventional resx localization. (The former is useful for most of the code, but some localized content comes from the view model, where it is more straightforward to use resx files.)

我已经阅读了 WPF本地化指南的相关部分.我已经在msbuild文件中设置了UICulture属性.我已将以下行添加到AssemblyInfo.cs:

I have read the relevant parts of the WPF Localization Guidance. I have set the UICulture property in the msbuild file. I have added the following line to AssemblyInfo.cs:

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

我知道我需要将Resources.resx复制为Resources.en.resx,以便将本地化的资源写入附属程序集.

I understand that I need to copy Resources.resx as Resources.en.resx so that my localized resources get written to the satellite assembly.

我已将Resources.resx上的构建目标设置为None,并将Resources.en.resx上的构建目标设置为EmbeddedResource. Resources.resx上的自定义工具是PublicResXFileCodeGenerator,用于生成强类型的资源类.我知道生成器仅适用于没有特定文化区域后缀的文件.

I have set the build target on Resources.resx to None and on Resources.en.resx to EmbeddedResource. The custom tool on Resources.resx is PublicResXFileCodeGenerator to generate the strongly typed resources class. I know that the generator only works on a file that doesn't have a culture-specific suffix.

目前,我必须手动保持Resources.resx和Resources.en.resx同步.它们必须相同. ( Rick Stahl解释说,这里.)

At the moment I must manually keep Resources.resx and Resources.en.resx synchronized. They must be identical. (Rick Stahl explains that here.)

我试图修改我的C#项目文件以自动复制该文件.但是,我无法使它正常工作.我不是msbuild专家!我添加了以下构建目标:

I have tried to modify my C# project file to copy the file automatically. However, I can't get this to work. I'm no msbuild expert! I added the following build target:

<Target Name="BeforeResGen">
    <Copy SourceFiles="@(CopyAsLocalizedResources)" DestinationFiles="$(IntermediateOutputPath)Resources.$(UICulture).resx">
        <Output ItemName="EmbeddedResource" TaskParameter="DestinationFiles"/>
    </Copy>
</Target>

我将Resources.resx的构建操作从无更改为CopyAsLocalizedResources.

I changed the Build Action for Resources.resx from None to CopyAsLocalizedResources.

我看到在构建期间将我的Resources.en.resx文件复制到中间目录,但是在运行时找不到我的资源,并且出现异常. Presumaby从未将它们编译到卫星程序集中.

I see my Resources.en.resx file being copied to the intermediate directory during build, but my resources aren't found at runtime, and I get an exception. Presumaby they are never being compiled into the satellite assembly.

有人可以通过修改项目文件来帮助我实现这一目标吗?

Can anyone help me achieve this using a modification to the project file?

推荐答案

尤里卡!

  <Target Name="CreateLocalizedResources" BeforeTargets="AssignTargetPaths">
    <Copy SourceFiles="@(CopyAsLocalizedResources)" DestinationFiles="$(IntermediateOutputPath)Resources.$(UICulture).resx" SkipUnchangedFiles="true" UseHardlinksIfPossible="true">
      <Output TaskParameter="DestinationFiles" ItemName="GeneratedLocalizedResources" />
    </Copy>
    <ItemGroup>
      <EmbeddedResource Include="@(GeneratedLocalizedResources)">
        <ManifestResourceName>$(RootNamespace).Properties.Resources.$(UICulture)</ManifestResourceName>
      </EmbeddedResource>
    </ItemGroup>
    <Message Text="Generated resources are: @(GeneratedLocalizedResources)" Importance="high" />
  </Target>

我将Resources.resx文件上的构建操作"设置为CopyAsLocalizedResources,而自定义构建目标将处理该构建以构建正确的附属程序集.

I set the Build Action on my Resources.resx file to CopyAsLocalizedResources, and the custom build target handles tricking the build into making the proper satellite assembly.

这篇关于如何修改C#项目文件,以便将Resources.resx编译到UICulture的附属程序集中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆