0

Currently, we have a number of .NET Framework 4.7.1 VB projects (each under a solution of its own) that have the same global resources for language translations:

project1.vbproj
    App_GlobalResources
        module1.resx
        module1.en.resx
        module1.de.resx
        module2.resx
        module2.en.resx
        module2.de.resx
        …
        modulen.resx
        modulen.en.resx
        modulen.de.resx
project2.vbproj
    App_GlobalResources
        module1.resx
        module1.en.resx
        module1.de.resx
        module2.resx
        module2.en.resx
        module2.de.resx
        …
        modulen.resx
        modulen.en.resx
        modulen.de.resx
…
projectm.vbproj
    App_GlobalResources
        module1.resx
        module1.en.resx
        module1.de.resx
        module2.resx
        module2.en.resx
        module2.de.resx
        …
        modulen.resx
        modulen.en.resx
        modulen.de.resx

Then we access the resources with the usual notation, e.g., in .ascx files:

<%=Resources.module1.text1%>

Is it possible to get rid of the file duplication? As an example, put module1.resx to a shared project and then let all projects refer to that shared project?

There is no need to be able to modify the .resx files without re-compilation, we just want to simplify the source code control.

We would like to keep the current .resx files untouched.

We can modify the references to the resources (Resources.module1.text1) to something else if that is necessary and the change is a simple one.

masa
  • 2,762
  • 3
  • 21
  • 32

1 Answers1

4

Yes, that's certainly possible. You may follow the following steps:

  1. Create a new class-library project called CommonResources.
  2. Add all the resources files (including localized one) to it.
  3. Make sure you set the Access Modifier of each resource file to Public.

    CommonResources project demo

  4. Build the project.

  5. In each one of your main projects, add a reference to the main assembly of the CommonResources project (CommonResources.dll).

    Adding a reference

  6. To access a resource, simply use something like:

    CommonResources.Module1.Foo
    CommonResources.Module2.Bar
    

See it in action:

Using shared resources

  • Thanks for a clear answer! However, if I add `CommonResources` as a VB project, I need to use `CommonResources.My.Resources.Module1.Foo`. Is it possible to remove `My.Resources` somehow? If `CommonResources`is a C# project, I am not able to refer to `CommonResources` for some reason (?) I thought it should work. – masa Oct 08 '19 at 07:48
  • @masa _"Is it possible to remove My.Resources somehow?"_ Yes, do not add the resource files under `My Project`. Instead, add them in the root directory of your project as shown in the first screenshot. _"If `CommonResources` is a C# project, I am not able to refer to `CommonResources` for some reason"_ What you see in the first screenshot is actually a C# project. Again, add the resx files in the root directory and make sure the access modifier is set to `Public`. – 41686d6564 stands w. Palestine Oct 08 '19 at 08:27
  • In both cases, the `.resx` files were already in the root directory. For the VB project, I needed to clear the Custom Tool Namespace for the `.resx` file, and it started working. I still do not know, why the C# project is not working. – masa Oct 08 '19 at 08:44