.netvb.netlocalization.net-4.7.1

How to share .resx files between projects in vb.net?


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.


Solution

  • 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