Importing and Exporting Resx Resources from arbitrary Projects - RickStrahl/Westwind.Globalization GitHub Wiki
Using the Web Resource Editor it's possible to import resources from an arbitrary project on your local-machine (local to the Web Server running the Editor), edit them in the interactive resource editor and then export them back out to Resx files.
You can do this without requiring any Westwind.Globalization runtime requirements as you are simply using the Web Resource Editor for editing your external resources. The Resx files and strongly typed resources exported after editing are the same as standard .NET Resx resources and so there are no extra dependencies.
This is a very effective way to edit resources interactively and work with resources from any type of projects including Class Libraries and even Desktop Windows applications that use Resx resources.
Steps
- Import Resources from an arbitrary path
- Edit resources to add or make changes to resources
- Export resources back into the original path
- Open resources in Visual Studio to enable strongly typed resource generation
Importing Resources
Using the Web Resource Editor it's possible to import arbitrary resources into a project. If you run on localhost on your development machine you can for example import resx files from any location on the machine by specifying the path in the import dialog.
If you are importing resources from an 'external' project it's probably a good idea to set up a separte table or database to hold those new resources in order not to conflict with existing resource names. Most class libraries use a Resources Resx file name and you don't want to overlap. The simplest way to do this is change the TableName in your Localization database to a new name, then open the editor and
Create Database
.
<DbResourceConfiguration>
<add key="ResourceTableName" value="LocalizationsWestWindUtilities" />
</DbResourceConfiguration>
In this example I'll import Resx Resources from one of my Class Library Project called Westwind.Utilities which I simply cloned from a GitHub repository. When I start out I have the following folder structure for the Properties folder of the class library project.
I then import the Resources from this project using the import dialog.
Notice that I point at the Project's Properties
folder, where the single Resx file for this project resides. If multiple Resx files exist in that folder or any subfolders they would also be pulled in. In this case a single Resx file is pulled in as a Resources ResourceSet.
Once imported you can then start localizing the resources. In this case I added a few German Resources interactively in the editor.
Once you've made some changes that you'd like to save - or if you choose anytime you've made a change since the process is pretty quick - you can then re-export the resources back out to disk.
Note that you select the active resource set (if there is more than one in your project) and you specify the output path.
If you added or removed any resources in the editor, you will also want to re-generate the strongly typed Resx resources in the Resources.designer.cs
. To do this use Create Classes menu option.
Make sure you chose Resx Designer Classes
which creates the same designer files that Visual Studio creates in the editor from your .Resx files. Select the resource file or files to export and select the output folder to the external project's Properties folder or whereever your Resx files reside.
If you now open your folder you'll find the following files:
Notice that there's now a Resources.de.resx
file as well as an updated Resource.Designer.cs
file that reflects any added or removed resources. Note that when using standard Resx designer.cs exports you are not depending on any of the Westwind.Globalization runtime libaries - there are no dependencies to use your Resx files.
Voila - you've just imported, edited and re-exported external resources using the West Wind Globalization text editor.
Troubleshooting
Permissions
In order for resource exports to work it's important that the Web application has rights to read and write files in the resource folders. If you're running with IIS Express or another local Web server your local permissions should just work as is.
However, if you are running under IIS you probably will have to tweak the permissions for the Application Pools by switching the Application Pool Identity to an account that can write in the Resx folders.