Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
711 views
in Technique[技术] by (71.8m points)

visual studio 2010 - "Run Custom Tool" for resx files in MVC2 project (from an external application/script)

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...