4

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?

Jeff
  • 2,835
  • 3
  • 41
  • 69

3 Answers3

2

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.

Lodder
  • 19,758
  • 10
  • 59
  • 100
Ron Sijm
  • 8,490
  • 2
  • 31
  • 48
0

We did end up finding a solution to this problem.

For MVC projects, we changed the tool to use the ResXResourceWriter class to update the resx files, matching existing keys and adding new ones as needed.

This preserves the 'Embedded Resource' status as well as all of the other details that are needed.

We still have to set the file up correctly the first time it's created, but that is a much more manageable problem.

Jeff
  • 2,835
  • 3
  • 41
  • 69
0

I have struggled with this before but have resourcing working well. This may help although I am not sure. Its always worthwhikle to go over the basics as sometimes its something very simple and .NET resourcing can be infelxible.

I will explain my use of resourcing and hopefully you can apply it to your scenario (I cant really figure out what they exact problem is based on your question.)

Steps to setting up resourcing 1. Add a ASP.NET Folder to your web solution right click on solution select Add > Add ASP.NET Folder > App_GlobalResources

  1. Add a resource file to the folder call it MyResourceFile.resx

  2. Open Properties for file and select

    • Build Action : Embedded Resource
    • Custom Tool : PublicResXFileCodeGenerator
  3. Then add your different resource files for different languages etc (specify the same properties for all resource file etc Build Action and Custom Tool)

    • MyResourceFile.en-GB.resx
    • MyResourceFile.fr-FR.resx
    • MyResourceFile.ja-JP.resx

This should auto generate your resource manager which can be accessed through calling

MyResourceFile.MyResourceText

It worth noting that if you havent got a culture installed or its incorrectly defined it wont work and you get all sorts of errors.

Etc MyResourceFile.en-JP.resx would not work (unless you create this custom culture) and cause all sorts of other issues. This culture will be mapped from the CultureInfo in the application to determine which resource file to use, therefore it must be a valid CultureInfo.

Jonathan
  • 2,318
  • 7
  • 25
  • 44