Westwind.Globalization.DbResXConverter.FormatWebResourceSetPath C# (CSharp) Method

FormatWebResourceSetPath() public method

Translates the resource set path ASP.NET WebForms Global or local resource path base (ie. without the .resx and localeId extension).
public FormatWebResourceSetPath ( string ResourceSet, bool LocalResources ) : string
ResourceSet string
LocalResources bool
return string
        public string FormatWebResourceSetPath(string ResourceSet, bool LocalResources)
        {
               // Make sure our slashes are right
               ResourceSet = ResourceSet.Replace("/","\\");

               if (LocalResources)
               {
                   // Inject App_LocalResource
                   ResourceSet = ResourceSet.Insert(ResourceSet.LastIndexOf('\\')+1, "App_LocalResources\\");
                   ResourceSet = BasePhysicalPath + ResourceSet;
               }
               else
               {
                   ResourceSet = BasePhysicalPath + "App_GlobalResources\\" + ResourceSet;
               }

               FileInfo fi = new FileInfo(ResourceSet);
               if (!fi.Directory.Exists)
                   fi.Directory.Create();
            
               return ResourceSet;
        }

Usage Example

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest Request = HttpContext.Current.Request;
            HttpResponse Response = HttpContext.Current.Response;

            string resourceSet = Request.Params["ResourceSet"];
            string localeId = Request.Params["LocaleId"] ?? "";
            string resourceType = Request.Params["ResourceType"] ?? "Resx";   // Resx/ResDb
            bool includeControls = (Request.Params["IncludeControls"] ?? "") != "";
            string varname = Request.Params["VarName"] ?? "localRes";
            string resourceMode = (Request.Params["ResourceMode"] ?? "0");

            // varname is embedded into script so validate to avoid script injection
            // it's gotta be a valid C# and valid JavaScript name
            Match match = Regex.Match(varname, @"^[\w|\d|_|$|@]*$");
            if (match.Length < 1 || match.Groups[0].Value != varname)
                this.SendErrorResponse("Invalid variable name passed.");

            if (string.IsNullOrEmpty(resourceSet))
                this.SendErrorResponse("Invalid ResourceSet specified.");

            Dictionary<string, object> resDict = null;

            if (resourceType.ToLower() == "resdb")
            {
                DbResourceDataManager manager = new DbResourceDataManager();
                resDict = manager.GetResourceSetNormalizedForLocaleId(localeId, resourceSet) as Dictionary<string, object>;
            }
            else  // Resx Resources
            {
                DbResXConverter converter = new DbResXConverter();
                // must figure out the path
                string resxPath = null;
                if (DbResourceConfiguration.Current.ProjectType == GlobalizationProjectTypes.WebForms)
                    resxPath = converter.FormatWebResourceSetPath(resourceSet, (resourceMode == "0") );
                else
                    resxPath = converter.FormatResourceSetPath(resourceSet);

                resDict = converter.GetResXResourcesNormalizedForLocale(resxPath, localeId) as Dictionary<string, object>;
            }


            if (resourceMode == "0" && !includeControls)
            {
                // filter the list to strip out controls (anything that contains a . in the ResourceId 
                // is considered a control value
                resDict = resDict.Where(res => !res.Key.Contains('.') && res.Value is string)
                                 .ToDictionary(dict => dict.Key, dict => dict.Value);
            }
            else
            {
                // return all resource strings
                resDict = resDict.Where(res => res.Value is string)
                           .ToDictionary(dict => dict.Key, dict => dict.Value);
            }

            string javaScript = this.SerializeResourceDictionary(resDict, varname);


            // client cache
            if (!HttpContext.Current.IsDebuggingEnabled)
            {                
                Response.ExpiresAbsolute = DateTime.UtcNow.AddDays(30);
                Response.Cache.SetLastModified(DateTime.UtcNow);
                Response.AppendHeader("Accept-Ranges", "bytes");
                Response.AppendHeader("Vary", "Accept-Encoding");
                //Response.Cache.SetETag("\"" + javaScript.GetHashCode().ToString("x") + "\"");
            }

            // OutputCache settings
            HttpCachePolicy cache = Response.Cache;

            cache.VaryByParams["LocaleId"] = true;
            cache.VaryByParams["ResoureType"] = true;
            cache.VaryByParams["IncludeControls"] = true;
            cache.VaryByParams["VarName"] = true;
            cache.VaryByParams["ResourceMode"] = true;           
            cache.SetOmitVaryStar(true);

            DateTime now = DateTime.Now;
            cache.SetCacheability(HttpCacheability.Public);

            cache.SetExpires(now + TimeSpan.FromDays(365.0));
            cache.SetValidUntilExpires(true);
            cache.SetLastModified(now);

            this.SendTextOutput(javaScript, "application/javascript");
        }