public static string GetLocaleResource(this HttpContext context, string classKey, string resourceKey) { var cityId = (string)context.Session[Constants.ViewData.CurrentCityId]; int cityID; if (int.TryParse(cityId, out cityID) && cityID > 0) { //var cid = (int)context.Items["CustomerId"]; var customerCache = SqlResourceHelper.GetCustomerResourceCache(cityID); var value = customerCache[resourceKey]; if (value != null) { return(value.ToString()); } } //This has to call the original GetGlocalResourceObject method call to get it out of the regular langauge cache var globalResourceObject = HttpContext.GetGlobalResourceObject(classKey, resourceKey); if (globalResourceObject != null) { return(globalResourceObject.ToString()); } return(resourceKey); }
/// <summary> /// Gets a resource item that matches a specified culture and Type /// </summary> /// <param name="resourceKey">Type </param> /// <param name="culture">Culture to use (en-US, es-EC, etc)</param> /// <returns></returns> object IResourceProvider.GetObject(string resourceKey, CultureInfo culture) { string cultureName = culture != null ? culture.Name : CultureInfo.CurrentUICulture.Name; //Get the resource from the cache. if it is not in cache, grab it from the db object value = GetResourceCache(cultureName)[resourceKey]; //if it doesnt exist in either location, create it. if (value == null) { // resource is missing for current culture, create it SqlResourceHelper.AddLocaleResource(resourceKey, _type, cultureName); //we just added to the resource, so we need to update the cache to include the new item //it should exist now, re-get it for this culture. have to reset the cache we pull from value = GetResourceCache(cultureName, true)[resourceKey]; } return(value); }
/// <summary> /// Gets a cache of the culture. if refresh is passed in as true, then ignore the cache and get it directly from the DB /// If nothing in the cache exist, also get it from the DB /// </summary> /// <param name="cultureName">Culture to look for (en-US, es-EC)</param> /// <param name="refresh">Bool to determine if we should ignore the cache and get the resources directly from the db</param> /// <returns></returns> private IDictionary GetResourceCache(string cultureName, bool refresh = false) { //if the cache is null, set it to an empty list if (_resourceCache == null) { _resourceCache = new ListDictionary(); } //try to get the resource dictionary from the cache var resourceDict = _resourceCache[cultureName] as IDictionary; //if there was nothing in the cache or we need to force a refresh //if it is a custom field or grid resource, we have to force a refresh if (resourceDict == null || refresh) { //get the resource dictionary for the culture and type and add it to the cache. resourceDict = SqlResourceHelper.GetResources(_type, cultureName); _resourceCache[cultureName] = resourceDict; } //return the dictionary return(resourceDict); }