I created a Customer Resource Provider for my project. This provider is executing properly for Global resource requests (see sample #1 reference below). However, for Local resource references (see Sample #2 below), it is not loading anything. Can anybody help out or spot what is wrong?
Sample #1:
<asp:LabelID="lblAmount"runat="server"Text="<%$
Resources:TestResource, Total_Amount_is %>"></asp:Label
>
Sample #2:
<asp:TextBoxID="txtMoney"runat="server"meta:resourcekey="txtMoneyResource1"></asp:TextBox>
Here is the Customer Provider code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Compilation;
using System.Globalization;
using System.Resources;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Globalization_and_LocalizationV6
{
public sealed class SqlResourceProviderFactory : ResourceProviderFactory
{
public SqlResourceProviderFactory()
{
}
public override IResourceProvider CreateGlobalResourceProvider(string classKey)
{
return new SqlResourceProvider(null, classKey);
}
public override IResourceProvider CreateLocalResourceProvider(string virtualPath)
{
//virtualPath = System.IO.Path.GetFileName(virtualPath);
//virtualPath = virtualPath.Replace(HttpContext.Current.Request.ApplicationPath, "");//System.Web.VirtualPathUtility.ToAppRelative(virtualPath)
virtualPath = virtualPath.Replace(System.Web.VirtualPathUtility.ToAppRelative(virtualPath), "");
return new SqlResourceProvider(virtualPath, null);
}
}//End of Sealed Class called SqlResourceProviderFactory
internal class SqlResourceProvider : IResourceProvider
{
private string _virtualPath;
private string _className;
private IDictionary _resourceCache;
private static object CultureNeutralKey = new object();
public SqlResourceProvider(string virtualPath, string className)
{
_virtualPath = virtualPath;
_className = className;
}
private IDictionary GetResourceCache(string cultureName)
{
object cultureKey;
if (cultureName != null)
{
cultureKey = cultureName;
}
else
{
cultureKey = CultureNeutralKey;
}
if (_resourceCache == null)
{
_resourceCache = new ListDictionary();
}
IDictionary resourceDict = _resourceCache[cultureKey] as IDictionary;
if (resourceDict == null)
{
resourceDict = SqlResourceHelper.GetResources(_virtualPath, _className, cultureName, false, null);
_resourceCache[cultureKey] = resourceDict;
}
return resourceDict;
}
object IResourceProvider.GetObject(string resourceKey, CultureInfo culture)
{
string cultureName = null;
if (culture != null)
{
cultureName = culture.Name;
}
else
{
cultureName = CultureInfo.CurrentUICulture.Name;
}
object value = GetResourceCache(cultureName)[resourceKey];
if (value == null)
{
// resource is missing for current culture, use default
SqlResourceHelper.AddResource(resourceKey, _virtualPath, _className, cultureName);
value = GetResourceCache(null)[resourceKey];//How do you add a new item to the "list" inside this method? Or refresh the list with the updated data?
}
if (value == null)
{
// the resource is really missing, no default exists
SqlResourceHelper.AddResource(resourceKey, _virtualPath, _className, string.Empty);
}
return value;
}
IResourceReader IResourceProvider.ResourceReader
{
get
{
return new SqlResourceReader(GetResourceCache(null));
}
}
}//End of Sealed Class SqlResourceProvider
internal sealed class SqlResourceReader : IResourceReader
{
private IDictionary _resources;
public SqlResourceReader(IDictionary resources)
{
_resources = resources;
}
IDictionaryEnumerator IResourceReader.GetEnumerator()
{
return _resources.GetEnumerator();
}
void IResourceReader.Close()
{
}
IEnumerator IEnumerable.GetEnumerator()
{
return _resources.GetEnumerator();
}
void IDisposable.Dispose()
{
}
}//End of Sealed Class SqlResourceReader
internal static class SqlResourceHelper
{
public static IDictionary GetResources(string virtualPath, string className, string cultureName, bool designMode, IServiceProvider serviceProvider)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ASPNETDB"].ToString());
SqlCommand com = new SqlCommand();
//
// Build correct select statement to get resource values
//
if (!String.IsNullOrEmpty(virtualPath))
{
//
// Get Local resources
//
if (string.IsNullOrEmpty(cultureName))
{
// default resource values (no culture defined)
com.CommandType = CommandType.Text;
com.CommandText = "select resource_name, resource_value" +" from ASPNET_GLOBALIZATION_RESOURCES" +" where resource_object = @virtual_path" +" and culture_name is null";
com.Parameters.AddWithValue("@virtual_path", virtualPath);
}
else
{
com.CommandType = CommandType.Text;
com.CommandText = "select resource_name, resource_value" +" from ASPNET_GLOBALIZATION_RESOURCES " +"where resource_object = @virtual_path " +"and culture_name = @culture_name ";
com.Parameters.AddWithValue("@virtual_path", virtualPath);
com.Parameters.AddWithValue("@culture_name", cultureName);
}
}
else if (!String.IsNullOrEmpty(className))
{
//
// Get Global resources
//
string strFinalCultureName = string.Empty;
if (String.IsNullOrEmpty(cultureName))
{
strFinalCultureName = string.Empty;
}
else
{
strFinalCultureName = cultureName;
}
com.CommandType = CommandType.Text;
com.CommandText = "select resource_name, resource_value " +"from ASPNET_GLOBALIZATION_RESOURCES where " +"resource_object = @class_name and" +" culture_name = @culture_name ";
com.Parameters.AddWithValue("@class_name", className);
com.Parameters.AddWithValue("@culture_name", strFinalCultureName);
//if (string.IsNullOrEmpty(strFinalCultureName))
//{
// // default resource values (no culture defined)
// com.CommandType = CommandType.Text;
// com.CommandText = "select resource_name, resource_value" +
// " from ASPNET_GLOBALIZATION_RESOURCES " +
// "where resource_object = @class_name" +
// " and culture_name is null";
// com.Parameters.AddWithValue("@class_name", className);
//}
//else
//{
// com.CommandType = CommandType.Text;
// com.CommandText = "select resource_name, resource_value " +
// "from ASPNET_GLOBALIZATION_RESOURCES where " +
// "resource_object = @class_name and" +
// " culture_name = @culture_name ";
// com.Parameters.AddWithValue("@class_name", className);
// com.Parameters.AddWithValue("@culture_name", cultureName);
//}
}
else
{
//
// Neither virtualPath or className provided,
// unknown if Local or Global resource
//
throw new Exception("SqlResourceHelper.GetResources()" +" - virtualPath or className missing from parameters.");
}
ListDictionary resources = new ListDictionary();
try
{
con.Open();
//SqlCommand _com = con.CreateCommand();
//_com.Connection = con;
//_com.CommandType = CommandType.Text;
//_com.CommandText = com.CommandText;
//_com.Parameters.AddRange(com.Parameters.AddRange(com.Parameters.Cast<System.Data.Common.DbParameter>().ToArray()););
//foreach (var Parameters in com.Parameters)
//{
// _com.Parameters.
//}
com.Connection = con;
SqlDataReader sdr = com.ExecuteReader(CommandBehavior.CloseConnection);
while (sdr.Read())
{
string rn = sdr.GetString(sdr.GetOrdinal("resource_name"));
string rv = sdr.GetString(sdr.GetOrdinal("resource_value"));
resources.Add(rn, rv);
}
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
return resources;
}//End of GetResources
public static void AddResource(string resource_name, string virtualPath, string className, string cultureName)
{
string resource_object = "UNKNOWN **ERROR**";
if (!String.IsNullOrEmpty(virtualPath))
{
resource_object = virtualPath;
}
else if (!String.IsNullOrEmpty(className))
{
resource_object = className;
}
string strFinalCultureName = string.Empty;
if (String.IsNullOrEmpty(cultureName))
{
strFinalCultureName = string.Empty;
}
else
{
strFinalCultureName = cultureName;
}
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ASPNETDB"].ToString());
SqlCommand com = new SqlCommand();
StringBuilder sb = new StringBuilder();
sb.Append("MERGE ASPNET_GLOBALIZATION_RESOURCES as trg " +"using (values ('" + resource_object + "', '" + resource_name + "', '" + resource_name + " * DEFAULT * ', '" + strFinalCultureName + "')) " +"as source (RESOURCE_OBJECT, RESOURCE_NAME, RESOURCE_VALUE, CULTURE_NAME) " +"on " +" trg.RESOURCE_OBJECT = '" + resource_object + "' " +"and trg.RESOURCE_NAME = '" + resource_name + "' " +"and trg.CULTURE_NAME = '" + strFinalCultureName + "' " +"when matched then " +"update " +"set RESOURCE_VALUE = source.RESOURCE_VALUE " +"when not matched then " +"insert ( RESOURCE_OBJECT, RESOURCE_NAME, RESOURCE_VALUE, CULTURE_NAME) " +"values ( source.RESOURCE_OBJECT, source.RESOURCE_NAME, source.RESOURCE_VALUE, source.CULTURE_NAME);");
com.CommandText = sb.ToString();
//sb.Append("insert into ASPNET_GLOBALIZATION_RESOURCES " +
// "(resource_name ,resource_value," +
// "resource_object,culture_name ) ");
//sb.Append(" values (@resource_name ,@resource_value," +
// "@resource_object,@culture_name) ");
//com.CommandText = sb.ToString();
//com.Parameters.AddWithValue("@resource_name", resource_name);
//com.Parameters.AddWithValue("@resource_value", resource_name +
// " * DEFAULT * " +
// (String.IsNullOrEmpty(cultureName) ?
// string.Empty : cultureName));
//com.Parameters.AddWithValue("@culture_name", (String.IsNullOrEmpty(cultureName) ? SqlString.Null : cultureName));
//string resource_object = "UNKNOWN **ERROR**";
//if (!String.IsNullOrEmpty(virtualPath))
//{
// resource_object = virtualPath;
//}
//else if (!String.IsNullOrEmpty(className))
//{
// resource_object = className;
//}
//com.Parameters.AddWithValue("@resource_object", resource_object);
try
{
com.Connection = con;
con.Open();
com.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception(e.ToString());
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
}//End of AddResource
public static IDictionary AASearch(List<Dictionary<string, object>> testData, Dictionary<string, object> searchPattern)
{
return testData.FirstOrDefault(x => searchPattern.All(x.Contains));
}
}//End of Class SqlResourceHelper
}//End of Namespace Globalization_and_LocalizationV6