Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 Asp.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.

like image 904
Lalit Avatar asked Jun 09 '11 10:06

Lalit


1 Answers

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.

like image 144
Ivaylo Slavov Avatar answered Nov 12 '22 07:11

Ivaylo Slavov