为 MVC 生成 Resx 文件

发布于 2024-12-13 12:38:41 字数 1030 浏览 6 评论 0原文

我们使用 resx 文件进行全球化,并使用数据库查找我们的 CS 人员可以配置的内容(例如选项卡名称,可能因产品而异),因此在设计时是未知的。

我创建了一个自定义工具,可以读取 resx 文件并智能地将键/值对转储到关系数据库中(匹配值,这样我们就不会出现重复项)。

这对我们的业务有很大帮助 - 我们不必发送每个 resx 进行翻译(并为共享单词的重复翻译付费),而且我们的所有翻译(在数据库中)都有一个“黄金标准”。

我创建的工具还读取数据库,获取键/值对和每个值的翻译,并为每个 resx 文件(以及文本文件的每种语言的翻译)创建文本文件,并自动运行 resgen.exe(一个命令) Visual Studio 附带的 -line 工具,用于从生成的文本文件编译 resx 文件。

我没有任何源代码管理集成,因此我们必须在使用该工具时手动签出 resx 文件并手动签入生成的文件,但这并不是一个大问题。

我的问题是,此方法对于我们的新 MVC 项目失败:MVC 项目要求 resx 文件是访问修饰符为“public”的嵌入资源。

到目前为止,我们一直在手动修复这个问题,这引入了人为错误的可能性,并增加了不小的工作量。

有没有办法让 resgen.exe 创建嵌入式和公共的资源文件?如果没有,是否有其他方法可以创建 resx 文件来执行此操作?

更新,补充问题: 我们用这种方法生成的 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. 

提到的工具是我们最初用来创建 resx 文件的工具。有什么办法可以阻止这个警告吗?

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.

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).

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).

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.

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.

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'.

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? If not, is there another way I can create resx files that will do so?

Update, additional question:
The resx files we generate with this method also raise a warning:

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. Is there a way I can prevent this warning?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

メ斷腸人バ 2024-12-20 12:38:41

首先,您可以使用 /publicClass 命令行选项将资源生成为公共资源。另请参阅: Resgen.exe - 资源文件生成器 @ msdn

其次,我不要认为你可以让 resgen 默认将资源文件嵌入资源,因为它不是资源的属性,而是项目的设置。

例如:当您使用向导添加新资源“Resource1”时,一个新的项目组将添加到项目文件中:

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

也许有库可以以编程方式修改项目文件,但我不知道。

我要做的只是尝试自己序列化和反序列化项目文件,然后为生成的每个资源添加该部分。

编辑:

它还会添加不同的项目组:

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

所以,除非您有一个好的第 3 方程序来序列化、编辑、反序列化项目文件。让巫师来做可能会更好。

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.

忆离笙 2024-12-20 12:38:41

我以前曾为此苦苦挣扎,但资源运作良好。尽管我不确定,但这可能会有所帮助。回顾基础知识总是值得的,因为有时它非常简单,而 .NET 资源可能不太灵活。

我将解释我对资源的使用,希望您可以将其应用到您的场景中(我无法根据您的问题真正弄清楚他们的确切问题是什么。)

设置资源的步骤
1. 将 ASP.NET 文件夹添加到您的 Web 解决方案中,右键单击解决方案,选择“添加”>“添加 ASP.NET 文件夹 > App_GlobalResources

  1. 将资源文件添加到名为 MyResourceFile.resx 的文件夹中

  2. 打开文件的属性并选择

    • 构建操作:嵌入资源
    • 自定义工具:PublicResXFileCodeGenerator
  3. 然后为不同语言等添加不同的资源文件(指定相同的资源文件)所有资源文件等的属性(构建操作和自定义工具)

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

这应该会自动生成您的资源管理器,可以通过调用来访问

MyResourceFile.MyResourceText

它。值得注意的是,如果您没有安装区域性或其定义不正确,它将无法工作,并且您会收到各种错误。

等等 MyResourceFile.en-JP.resx 将不起作用(除非您创建此自定义区域性)并导致各种其他问题。该区域性将从应用程序中的 CultureInfo 进行映射,以确定要使用哪个资源文件,因此它必须是有效的 CultureInfo。

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.

怀里藏娇 2024-12-20 12:38:41

我们最终找到了解决这个问题的方法。

对于 MVC 项目,我们更改了工具以使用 ResXResourceWriter 类来更新 resx 文件、匹配现有密钥并根据需要添加新密钥。

这保留了“嵌入式资源”状态以及所需的所有其他详细信息。

我们仍然必须在第一次创建文件时正确设置该文件,但这是一个更容易管理的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文