Split Large Resource File into multiple files physically while compiler treat it as one
Is it possible to split one big Resource(.resx) file into several files physically and still use the same syntex to get data
i.e.
GlobalResource.resx
into
GlobalResource1.resx, GlobalResource2.resx, GlobalResource3.resx, ...
and all file will be compliled into one and use like
Resources.GlobalResource.errParamNameReq
using it in A开发者_JS百科sp.net 3.5. having problem while working on svn, every time it get conflicted while try to checkin, so i thought to split it into many files while the compiler treat it as one.
Every resource file has a code-behind file which is used to declare the resource items as static properties. Also a lot of automated code generation is used by tools as Visual Studio to synchronize between the xml/resource editor data and the code behind file. Splitting a resource into more than one file might be possible, but the effects of automated code generation later can cause even more problems than simply merging your changes. A possible solution is to create different resource files. Then have a class that keeps a collection of ResourceManager
instance for every resource manager. Something like this:
public class ResourceWrapper
{
private ICollection<ResourceManager> resourceManagers;
public ResourceWrapper(IEnumerable<ResourceManager> managers)
{
resourceManagers = new List<ResourceManager>(managers);
}
public string GetString(string key)
{
foreach (var resourceManager in resourceManagers)
{
string res = resourceManager.GetString(key);
if (res != null)
{
return res;
}
}
return null;
}
public string GetString(string key, CultureInfo culture)
{
foreach (var resourceManager in resourceManagers)
{
string res = resourceManager.GetString(key, culture);
if (res != null)
{
return res;
}
}
return null;
}
}
You only have to get all resources and pass them to the constructor. You might want to keep the wrapper instance once it is created to avoid creating a resource manager for each resource file - a static filed will do. The only issue with the above code is that there is no guarantee that 2 or more resource files define the same key. If this happens the code will return the first match only.
So you are trying to split it to avoid conflicts while checkin in svn? If yes, then you're not solving the root cause but the effects of it.
If it is so big that every time you checkin, you're likely to run into conflicts, the try refactoring them to smaller files with meaningful names. And then use @Ivaylo's solution. Every resource is complied to a class, ASP.Net website model simply hides it from you.
If you create a file called Resource1.resx in App_GlobalResources, you do get Resources.Resource1 as a class in your codebehind.
To my knowledge there is no way to have a resource file split over several files so that you can access the entries from several resx-files via one common accessor, like Resources.MyAggregatedResources.Resource1
.
You are surely aware of the fact that you can put as many resource files into you App_GlobalResources
folder as you wish and access them all as Resources.ResourceFile1.Resource1
, Resources.ResourceFile2.Resource1
.
All resources that are added to resx-files are automatically mapped to strongly typed properties in the code-behind files with the name resource.Designer.cs
(If you don't see this file, toggle "Show all files" option in the Solution Explorer (second button)). The "mapping" is implemented as code generation within Visual Studio and thus cannot be altered from your project. You need to implement your own abstraction layer above resources to return the values from several resource files and pass the name of the resource as string => no way to check for misspellings or missing resources in design-time and no IntelliSense.
One way to implement the functionality you want is to upgrade your solution to .Net 4.0 and to use the dynamic
properties that are resolved to run-time. You won't get any IntelliSense support for you resource names, however, as in case of static resources...
Found this thread will looking for a way to separate some large text resources (xml files with images), but it didn't help me nor did anything else using search terms like "split resx multiple filex"
During further search some weeks I found http://forums.asp.net/t/937583.aspx?Maximum+string+length+in+resx+file , because it seems that I wasn't able to save my xml files at all in the resource editor.
The solution was to use the “Add Resource –> Add New Text File” feature in the resource editor and to name the resource exactly like I would have named it in the "name" colum on the resource editor. Then I was able to paste everything in the new text file, that I would have normally filled in the "value" column.
Now I have one text file per each of those resources, handy diffs etc. Not sure it belongs exactly to this thread, but I wanted to share anyway.
精彩评论