“运行自定义工具”对于 MVC2 项目中的 resx 文件(来自外部应用程序/脚本)
我正在尝试使用我们现有的基础设施来编辑字符串资源,以实现我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后,我解决了这个问题。
我决定通过将资源分解为一个名为资源的新程序集来稍微简化一下事情。然后,我添加了我的 resx 文件并设置了它们的属性,如下所示...
然后我更改了导出应用程序以运行...
...并从文件夹中删除 *.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 ...
I then changed my export application to run ...
... 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!