views:

127

answers:

2

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!

+1  A: 

Antony:

Take a look at the comment by Lifeng Lu 08-29-2007 7:52 PM on this post:

How to make generated resource class public

Hope the workaround solves your problem... :)

Leniel Macaferi
good find, but that didn't quite get me to where i wanted to be
Antony Scott
, which is that I didn't want to have to rebuild after having run my export app, just so I can use code completion to get to the right string in my code :)
Antony Scott
+2  A: 

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!

Antony Scott