Generating Resx files for MVC

情到浓时终转凉″ 提交于 2019-12-05 15:37:34
Ron Sijm

First of, you can generated resources as public by using the /publicClass command line option. Also see: Resgen.exe - Resource File Generator @ msdn

Second, I don't think you can let resgen make the resource files embedded resources by default, simply because its not a property of a resource, but a setting of the project.

For example: when you add a new resource "Resource1", using the wizard, a new item group will be added to the project file:

<ItemGroup>
  <EmbeddedResource Include="Resource1.resx">
    <Generator>ResXFileCodeGenerator</Generator>
    <LastGenOutput>Resource1.Designer.cs</LastGenOutput>
  </EmbeddedResource>
</ItemGroup>

Maybe there are libraries to programmatically modify project files, but not that I know of.

What I would do is just try to serialize and deserialize the project file yourself, and add that section to it, for each resource your generate.

EDIT:

It will also add in a different Item Group:

<ItemGroup>
  <Compile Include="Resource1.Designer.cs">
    <AutoGen>True</AutoGen>
    <DependentUpon>Resource1.resx</DependentUpon>
    <DesignTime>True</DesignTime>
  </Compile>
</ItemGroup>

So, unless you have a good 3rd Party program to serialize, edit, deserialize the project file. It it probably better to let the wizard do it.

I have struggled with this before but have resourcing working well. This may help although I am not sure. Its always worthwhikle to go over the basics as sometimes its something very simple and .NET resourcing can be infelxible.

I will explain my use of resourcing and hopefully you can apply it to your scenario (I cant really figure out what they exact problem is based on your question.)

Steps to setting up resourcing 1. Add a ASP.NET Folder to your web solution right click on solution select Add > Add ASP.NET Folder > App_GlobalResources

  1. Add a resource file to the folder call it MyResourceFile.resx

  2. Open Properties for file and select

    • Build Action : Embedded Resource
    • Custom Tool : PublicResXFileCodeGenerator
  3. Then add your different resource files for different languages etc (specify the same properties for all resource file etc Build Action and Custom Tool)

    • MyResourceFile.en-GB.resx
    • MyResourceFile.fr-FR.resx
    • MyResourceFile.ja-JP.resx

This should auto generate your resource manager which can be accessed through calling

MyResourceFile.MyResourceText

It worth noting that if you havent got a culture installed or its incorrectly defined it wont work and you get all sorts of errors.

Etc MyResourceFile.en-JP.resx would not work (unless you create this custom culture) and cause all sorts of other issues. This culture will be mapped from the CultureInfo in the application to determine which resource file to use, therefore it must be a valid CultureInfo.

We did end up finding a solution to this problem.

For MVC projects, we changed the tool to use the ResXResourceWriter class to update the resx files, matching existing keys and adding new ones as needed.

This preserves the 'Embedded Resource' status as well as all of the other details that are needed.

We still have to set the file up correctly the first time it's created, but that is a much more manageable problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!