Generating Resx Files for MVC

We use resx files for globalization, as well as finding a database for things that can be customized (for example, tab names that may vary by product) by our CS staff, and thus are not known at design time.

I created a custom tool that reads resx files and intelligently uploads key / value pairs to a relational database (corresponding values, so we don’t have duplicates).

This was a big help for our business - we do not need to send every resx for translation (and pay for duplicate translations of common words), and we have a "gold standard" with all our translations (in the database).

The tool I created also reads the database, collects key / value pairs and translations of each value and creates text files for each resx file (and each text translation of the text file) and automates the launch of resgen.exe, the command line tool that comes with Visual Studio, to compile resx files from generated text files.

I have no integration with the source, so we have to manually check the resx files and manually check the generated files when using this tool, but this was not a big problem.

My problem is that this method is not suitable for our new MVC projects: MVC projects require resx files for embedded resources using the "public" access modifier.

Thus, we fixed it manually, which introduces the possibility of human error and adds a non-trivial amount of work.

Is there a way to get the resgen.exe file to create embedded and public resource files? If not, is there another way to create resx files that will do this?

Update, additional question: The resx files that we generate using 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 specified tool is the tool that we originally used to create resx files. Is there a way to prevent this warning?

+4
source share
3 answers

We really found a solution to this problem.

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

This retains the status of "Embedded Resource", as well as all other details that are necessary.

We still need to configure the file correctly the first time we create it, but this is a much more manageable problem.

0
source

First, you can create resources as public using the /publicClass command line /publicClass . Also see: Resgen.exe - @msdn Resource File Generator

Secondly, I don’t think that you can let resgen make resource resources of embedded resources by default, simply because it is not a property of the resource, but a project setting.

For example: when adding a new resource "Resource1" using the wizard, a new group of elements will be added to the project file:

 <ItemGroup> <EmbeddedResource Include="Resource1.resx"> <Generator>ResXFileCodeGenerator</Generator> <LastGenOutput>Resource1.Designer.cs</LastGenOutput> </EmbeddedResource> </ItemGroup> 

Perhaps there are libraries for programmatically modifying project files, but not what I know.

What would I do, just try to serialize and deserialize the project file yourself and add this section to it for each resource that you create.

EDIT:

It will also add to another group of elements:

 <ItemGroup> <Compile Include="Resource1.Designer.cs"> <AutoGen>True</AutoGen> <DependentUpon>Resource1.resx</DependentUpon> <DesignTime>True</DesignTime> </Compile> </ItemGroup> 

So, if you do not have a good third-party program for serializing, editing, deserializing a project file. It is probably best to let the wizard do this.

+2
source

I struggled with this before, but I have a lot of resources. This may help, although I'm not sure. It is always worth approaching the basics, as sometimes it is something very simple, and .NET resources can be useless.

I will explain how I use the resources and hope that you can apply it to your scenario (I cannot understand what they are asking exactly, based on your question.)

Resource Configuration Steps 1. Add the ASP.NET folder to your web solution, right-click the solution, select Add> Add ASP.NET Folder> App_GlobalResources

  • Add the resource file to the folder, name it MyResourceFile.resx

  • Open the "Properties" for the file and select

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

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

This should automatically generate your resource manager, which can be accessed by calling

 MyResourceFile.MyResourceText 

It is worth noting that if you do not have an established culture or it is incorrectly defined, it will not work, and you will get all kinds of errors.

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

0
source

Source: https://habr.com/ru/post/1379506/


All Articles