C# – Generating Resx files for MVC

cembedded-resourceglobalizationmodel-view-controllerresx

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?

Best Answer

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.