简体   繁体   English

为MVC生成Resx文件

[英]Generating Resx files for MVC

We use resx files for globalization, along with database lookups for things that can be configured (such as tab names, which can be different by product) by our CS staff, and thus aren't known at design-time. 我们使用resx文件进行全球化,并使用数据库查询来查找CS员工可以配置的内容(例如选项卡名称,根据产品的不同而有所不同),因此在设计时是未知的。

I created a custom tool that reads resx files and intelligently dumps the key/value pairs into a relational database (matching values so we don't have duplicates). 我创建了一个自定义工具,该工具可以读取resx文件,并将键/值对智能地转储到关系数据库中(匹配值,因此我们没有重复项)。

This has been a big help to our business - we don't have to send each resx for translation (and pay for duplicate translations of shared words) and we have a 'gold standard' with all our translations (in the database). 这对我们的业务有很大帮助-我们不必发送每个resx进行翻译(也不必为共享单词的重复翻译付费),并且我们所有的翻译都具有“黄金标准”(在数据库中)。

The tool I created also reads the database, picking up the key/value pairs and the translations of each value, and creates text files for each resx file (and each language's translation of the text file) and automates running resgen.exe, a command-line tool that ships with Visual Studio, to compile the resx files from the generated text files. 我创建的工具还读取数据库,获取键/值对和每个值的转换,并为每个resx文件(以及文本的每种语言的转换)创建文本文件,并自动运行resgen.exe(一个命令) Visual Studio附带的命令行工具,用于从生成的文本文件中编译resx文件。

I don't have any source-control integration, so we have to manually check out the resx files and manually check-in the generated files when using the tool, but this hasn't been a big problem. 我没有任何源代码控制集成,因此使用该工具时,我们必须手动检出resx文件并手动检入生成的文件,但这并不是一个大问题。

My problem is that this method is failing for our new MVC projects: the MVC projects require the resx files to be embedded resources with the Access Modifier of 'public'. 我的问题是,这种方法在我们的新MVC项目中失败了:MVC项目要求resx文件必须是带有“公共”访问修饰符的嵌入式资源。

Thusfar, we have been fixing this by hand, which introduces the possibility of human error and adds a non-trivial amount of work. 到目前为止,我们一直在手工修复此问题,这引入了人为错误的可能性,并增加了大量的工作量。

Is there a way to get resgen.exe to create resource files that are embedded and public? 有没有办法让resgen.exe创建嵌入式和公共的资源文件? If not, is there another way I can create resx files that will do so? 如果没有,还有其他方法可以创建resx文件吗?

Update, additional question: The resx files we generate with this method also raise a warning: 更新,另外一个问题:我们使用此方法生成的resx文件也会引发警告:

A custom tool 'PublicResXFileCodeGenerator' is associated with file '(resxname)',
but the output of the custom tool was not found in the project.
You may try re-running the custom tool by right-clicking on the file in the
Solution Explorer and choosing Run Custom Tool. 

The tool mentioned is the tool we initially use to create the resx files. 提到的工具是我们最初用于创建resx文件的工具。 Is there a way I can prevent this warning? 有什么办法可以防止此警告?

First of, you can generated resources as public by using the /publicClass command line option. 首先,可以使用/publicClass命令行选项将资源生成为公共资源。 Also see: Resgen.exe - Resource File Generator @ msdn 另请参阅: Resgen.exe-资源文件生成器@ 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. 其次,我认为您不能让resgen在默认情况下使资源文件嵌入资源,只是因为它不是资源的属性,而是项目的设置。

For example: when you add a new resource "Resource1", using the wizard, a new item group will be added to the project file: 例如:当您使用向导添加新资源“ Resource1”时,新项目组将被添加到项目文件中:

<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. 让向导执行此操作可能更好。

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. 对于MVC项目,我们将工具更改为使用ResXResourceWriter类来更新resx文件,匹配现有密钥并根据需要添加新密钥。

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. 第一次创建文件时,我们仍然必须正确设置文件,但这是一个更易于管理的问题。

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. 回顾基础知识总是值得一试的,因为有时它非常简单,而.NET资源可能难以理解。

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.将一个ASP.NET文件夹添加到您的Web解决方案中,右键单击解决方案,选择添加>添加ASP.NET文件夹> App_GlobalResources

  1. Add a resource file to the folder call it MyResourceFile.resx 将资源文件添加到名为MyResourceFile.resx的文件夹中

  2. Open Properties for file and select 打开文件属性,然后选择

    • Build Action : Embedded Resource 构建动作 :嵌入式资源
    • Custom Tool : PublicResXFileCodeGenerator 自定义工具 :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.en-GB.resx
    • MyResourceFile.fr-FR.resx MyResourceFile.fr-FR.resx
    • MyResourceFile.ja-JP.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. 等等MyResourceFile.en-JP.resx无法正常工作(除非您创建此自定义区域性),并且会导致各种其他问题。 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. 该文化将从应用程序中的CultureInfo映射来确定要使用的资源文件,因此它必须是有效的CultureInfo。

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

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