Helper class that handles access to the DbResourceManager more easily with single method access. The T() method provides an easy way to embed resources into applications using the resource key. Also allows for resource reading, writing (new and updates transparently), deleting and clearing of resources from memory. This class uses the DbResourceManager class to access resources and still uses the standard ResourceManager infrastructure of .NET to cache resources efficiently in memory. Data access occurs only on intial access of each resource set/locale.
        /// <summary>
        /// This static method clears all resources from the loaded Resource Providers
        /// and forces them to be reloaded the next time they are requested.
        ///
        /// Use this method after you've edited resources in the database and you want
        /// to refresh the UI to show the newly changed values.
        ///
        /// This method works by internally tracking all the loaded ResourceProvider
        /// instances and calling the IwwResourceProvider.ClearResourceCache() method
        /// on each of the provider instances. This method is called by the Resource
        /// Administration form when you explicitly click the Reload Resources button.
        /// <seealso>Class DbResourceConfiguration</seealso>
        /// </summary>
        public static void ClearResourceCache()
        {
            foreach (IWestWindResourceProvider provider in LoadedProviders)
            {
                provider.ClearResourceCache();
            }

            // clear any resource managers
            DbRes.ClearResources();
        }
 /// <summary>
 /// Helper function called from strongly typed resources to retrieve
 /// non-string based resource values.
 ///
 /// This method returns a resource value based on the active
 /// Generated ResourceAccessMode.
 /// </summary>
 /// <param name="resourceSet"></param>
 /// <param name="resourceId"></param>
 /// <param name="manager"></param>
 /// <param name="resourceMode"></param>
 /// <returns></returns>
 public static object GetResourceObject(string resourceSet, string resourceId,
                                        ResourceManager manager,
                                        ResourceAccessMode resourceMode)
 {
     if (resourceMode == ResourceAccessMode.AspNetResourceProvider)
     {
         return(GetAspNetResourceProviderValue(resourceSet, resourceId));
     }
     if (resourceMode == ResourceAccessMode.Resx)
     {
         return(manager.GetObject(resourceId));
     }
     return(DbRes.TObject(resourceSet, "LocalizationForm"));
 }
        /// <summary>
        /// Helper function called from strongly typed resources to retrieve
        /// string based resource values.
        ///
        /// This method returns a resource string based on the active
        /// Generated ResourceAccessMode.
        /// </summary>
        /// <param name="resourceSet"></param>
        /// <param name="resourceId"></param>
        /// <param name="manager"></param>
        /// <param name="resourceMode"></param>
        /// <returns></returns>
        public static string GetResourceString(string resourceSet, string resourceId,
                                               ResourceManager manager,
                                               ResourceAccessMode resourceMode)
        {
            if (resourceMode == ResourceAccessMode.AspNetResourceProvider)
            {
                return(GetAspNetResourceProviderValue(resourceSet, resourceId) as string);
            }
            if (resourceMode == ResourceAccessMode.Resx)
            {
                return(manager.GetString(resourceId));
            }

            return(DbRes.T(resourceId, resourceSet));
        }
        /// <summary>
        /// Helper function called from strongly typed resources to retrieve
        /// non-string based resource values.
        ///
        /// This method returns a resource value based on the active
        /// Generated ResourceAccessMode.
        /// </summary>
        /// <param name="resourceSet"></param>
        /// <param name="resourceId"></param>
        /// <param name="manager"></param>
        /// <param name="resourceMode"></param>
        /// <returns></returns>
        public static object GetResourceObject(string resourceSet, string resourceId,
                                               ResourceManager manager,
                                               ResourceAccessMode resourceMode)
        {
#if NETFULL
            if (resourceMode == ResourceAccessMode.AspNetResourceProvider)
            {
                return(GetAspNetResourceProviderValue(resourceSet, resourceId));
            }
#endif

            if (resourceMode == ResourceAccessMode.Resx)
            {
                return(manager.GetObject(resourceId));
            }
            return(DbRes.TObject(resourceId, resourceSet));
        }
        /// <summary>
        /// Helper function called from strongly typed resources to retrieve
        /// string based resource values.
        ///
        /// This method returns a resource string based on the active
        /// Generated ResourceAccessMode.
        /// </summary>
        /// <param name="resourceSet"></param>
        /// <param name="resourceId"></param>
        /// <param name="manager"></param>
        /// <param name="resourceMode"></param>
        /// <returns></returns>
        public static string GetResourceString(string resourceSet, string resourceId,
                                               ResourceManager manager,
                                               ResourceAccessMode resourceMode)
        {
#if NETFULL
            if (resourceMode == ResourceAccessMode.AspNetResourceProvider)
            {
                return(GetAspNetResourceProviderValue(resourceSet, resourceId) as string);
            }
#endif
            try
            {
                if (resourceMode == ResourceAccessMode.Resx)
                {
                    return(manager.GetString(resourceId));
                }
            }
            catch (Exception ex)
            {
                return(resourceId);
            }

            return(DbRes.T(resourceId, resourceSet));
        }
        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"] ?? "auto";
            string resourceType    = Request.Params["ResourceType"] ?? "Resx"; // Resx/ResDb
            bool   includeControls = (Request.Params["IncludeControls"] ?? "") != "";
            string varname         = Request.Params["VarName"] ?? "resources";
            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)
            {
                SendErrorResponse("Invalid variable name passed.");
            }

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

            // pick current UI Culture
            if (localeId == "auto")
            {
                localeId = Thread.CurrentThread.CurrentUICulture.IetfLanguageTag;
            }

            Dictionary <string, object> resDict = null;

            if (string.IsNullOrEmpty(resourceType) || resourceType == "auto")
            {
                if (DbResourceProvider.ProviderLoaded || DbSimpleResourceProvider.ProviderLoaded)
                {
                    resourceType = "resdb";
                }
                else
                {
                    resourceType = "resx";
                }
            }


            if (resourceType.ToLower() == "resdb")
            {
                // use existing/cached resource manager if previously used
                // so database is accessed only on first hit
                var resManager = DbRes.GetResourceManager(resourceSet);

                DbResXConverter converter = new DbResXConverter(context.Server.MapPath(DbResourceConfiguration.Current.ResxBaseFolder));
                resDict = converter.GetResourcesNormalizedForLocale(resManager, localeId);

                //resDict = manager.GetResourceSetNormalizedForLocaleId(localeId, resourceSet);
                if (resDict == null || resDict.Keys.Count < 1)
                {
                    // try resx instead
                    string resxPath = converter.FormatResourceSetPath(resourceSet);
                    resDict = converter.GetResXResourcesNormalizedForLocale(resxPath, localeId);
                }
            }
            else  // Resx Resources
            {
                string          basePath  = context.Server.MapPath(DbResourceConfiguration.Current.ResxBaseFolder);
                DbResXConverter converter = new DbResXConverter(basePath);

                resDict = converter.GetCompiledResourcesNormalizedForLocale(resourceSet,
                                                                            DbResourceConfiguration.Current.ResourceBaseNamespace,
                                                                            localeId);

                if (resDict == null)
                {
                    // check for .resx disk resources
                    string resxPath = converter.FormatResourceSetPath(resourceSet);
                    resDict = converter.GetResXResourcesNormalizedForLocale(resxPath, localeId);
                }
                else
                {
                    resDict = resDict.OrderBy(kv => kv.Key).ToDictionary(k => k.Key, v => v.Value);
                }
            }


            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 = SerializeResourceDictionary(resDict, varname);


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

                // OutputCache settings
                HttpCachePolicy cache = Response.Cache;

                cache.VaryByParams["ResourceSet"]     = true;
                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(1));
                cache.SetValidUntilExpires(true);
                cache.SetLastModified(now);
            }

            SendTextOutput(javaScript, "text/javascript");
        }