“运行自定义工具”对于 MVC2 项目中的 resx 文件(来自外部应用程序/脚本)

发布于 2024-09-08 19:07:47 字数 2085 浏览 3 评论 0原文

我正在尝试使用我们现有的基础设施来编辑字符串资源,以实现我的 MVC 项目的本地化。我们将所有资源字符串存储在数据库表中,并有一个前端 Web UI 来编辑它们,以及一个生成 .resx 文件的导出应用程序。这一切都很好,但我在使用 MVC2 和 VS2010 的新项目中遇到了一些困难。

我对此提出了另一个问题,答案是几乎让我到达那里,但还没有完全到达。

我现在已按照许多人的建议将资源更改为位于 Resources 文件夹中(而不是 App_GlobalResources)。并对我的 .resx 文件进行以下设置...

Build Action             = Embedded Resource
Copy to Output Directory = Do not copy
Custom Tool              = PublicResXFileCodeGenerator
Custom Tool Namespace    = Resources
File Name                = MyApp.resx

我已更改导出应用程序以运行 resgen.exe 工具,具有以下参数......

string args = string.Format("/publicClass \"{0}\" /str:cs,Resources,{1},\"{2}\"", resourceFile, Path.GetFileNameWithoutExtension(resourceFile), csFilename);

...它生成一个几乎与我最初将 .resx 文件添加到我的项目时得到的 .designer.cs 文件相同的文件。唯一的区别是

生成的 .designer.cs 文件与我在导出应用程序中运行 resgen.exe 工具时获得的文件略有不同。

这是当我第一次将 .resx 文件添加到我的 Resources 文件夹时由 VS2010 生成的代码……

public static global::System.Resources.ResourceManager ResourceManager {
    get {
        if (object.ReferenceEquals(resourceMan, null)) {
            global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Resources.MyApp", typeof(MyApp).Assembly);
            resourceMan = temp;
        }
        return resourceMan;
    }
}

我运行 resgen.exe 工具时的区别在于前缀 MyCompany.MyApp 为ResourceManager 构造函数中的命名空间

new global::System.Resources.ResourceManager("MyCompany.MyApp.Resources.MyApp", typeof(MyApp).Assembly);

现在,对我来说这似乎是 resgen.exe 工具中的一个错误,因为我已经告诉它我的资源的命名空间是 Resources,而不是 MyCompany.MyApp.Resources

那么,这个问题有解决办法吗?

目前我唯一能想到的就是使用 powershell 对生成的 .designer.cs 文件进行后处理并修复它!

I am trying to get the localization for my MVC project working with our existing infrastructure for editing string resources. We store all our resource string in database tables and have a front end web UI to edit them with, and an export application which generated the .resx files. This all works great, but I am having a little difficulty with a new project using MVC2 and VS2010.

I have asked another question on this, the answer to which almost got me there, but not quite.

I have now changed the resources to be in a Resources folder (instead of App_GlobalResources), as recommended by a number of people. And have the following settings against my .resx files ...

Build Action             = Embedded Resource
Copy to Output Directory = Do not copy
Custom Tool              = PublicResXFileCodeGenerator
Custom Tool Namespace    = Resources
File Name                = MyApp.resx

I have changed my export application to run the resgen.exe tool with the following parameters ...

string args = string.Format("/publicClass \"{0}\" /str:cs,Resources,{1},\"{2}\"", resourceFile, Path.GetFileNameWithoutExtension(resourceFile), csFilename);

... which generates an almost identical .designer.cs file as I get when I add the .resx file to my project initially. The only difference is the

The generated .designer.cs file differs slightly from the file I get when I run the resgen.exe tool from within my export application.

This is the code generated by VS2010 when I first add the .resx file to my Resources folder ...

public static global::System.Resources.ResourceManager ResourceManager {
    get {
        if (object.ReferenceEquals(resourceMan, null)) {
            global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Resources.MyApp", typeof(MyApp).Assembly);
            resourceMan = temp;
        }
        return resourceMan;
    }
}

... the difference when I run the resgen.exe tool is that is prefixs MyCompany.MyApp to the namespace in the constructor to ResourceManager

new global::System.Resources.ResourceManager("MyCompany.MyApp.Resources.MyApp", typeof(MyApp).Assembly);

Now, this to me seems to be a bug in the resgen.exe tool, because I've told it that the Namespace for my resources is Resources, not MyCompany.MyApp.Resources.

So, is there a fix/work-around for this problem?

The only thing I can think to do at the moment is to post-process the generated .designer.cs file with powershell and fix it!

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

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

发布评论

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

评论(1

虚拟世界 2024-09-15 19:07:47

最后,我解决了这个问题。

我决定通过将资源分解为一个名为资源的新程序集来稍微简化一下事情。然后,我添加了我的 resx 文件并设置了它们的属性,如下所示...

Build Action             = Embedded Resource
Copy to Output Directory = Do not copy
Custom Tool              = PublicResXFileCodeGenerator
Custom Tool Namespace    = Resources
File Name                = MyApp.resx

然后我更改了导出应用程序以运行...

resgen MyApp.resx /str:c#,Resources,MyApp,MyApp.designer.cs /publicClass

...并从文件夹中删除 *.resources(由 resgen.exe 实用程序创建,但是不需要)

这消除了构造函数上 ResourceManager 的前缀,然后我只是将新的 Resources 程序集的引用添加到我的 Web 应用程序中。

我运行了一些测试以确保一切正常,包括进入 .designer.cs 文件并删除其中一个属性以导致编译器错误。然后重新运行我的导出应用程序,一切又恢复正常了。

所以,我是一只快乐的小兔子!

Finally, I have solved the problem.

I decided to simplify things a bit by breaking my resources out in to a new assembly called Resources. I then added my resx files and set the properties for them as below ...

Build Action             = Embedded Resource
Copy to Output Directory = Do not copy
Custom Tool              = PublicResXFileCodeGenerator
Custom Tool Namespace    = Resources
File Name                = MyApp.resx

I then changed my export application to run ...

resgen MyApp.resx /str:c#,Resources,MyApp,MyApp.designer.cs /publicClass

... and to delete *.resources from the folder (created by the resgen.exe utility, but not needed)

This got rid of the prefix on the constructor to ResourceManager, and then i just added a reference to my new Resources assembly to my web application.

I've run a few tests to make sure all is good, including going in to the .designer.cs file and deleting one of the properties to cause a compiler error. Then re-ran my export app, and everything worked again.

So, I am a happy bunny!

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