[英]Extending the ASP.NET Resource Provider
我正在擴展asp.net資源提供程序。 問題是擴展資源提供程序未檢測到頁面文化。 GetObject()方法中的CultureInfo始終為null。
要更改頁面文化,我使用列表框重寫InitializeCulture():
protected override void InitializeCulture()
{
if (Request.Form["ListBox1"] != null)
{
String selectedLanguage = Request.Form["ListBox1"];
UICulture = selectedLanguage;
Culture = selectedLanguage;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
}
base.InitializeCulture();
}
和GetObject()函數:
public object GetObject(string resourceKey, CultureInfo culture)
{
//Call of the data access layer function to retreave the resource value from the database
string resourceValue = m_dalc.GetResourceByCultureAndKey(culture, resourceKey);
return resourceValue;
}
這是資源提供者類:
public class DBResourceProvider : IResourceProvider
{
#region local variables
//We save the classKey (resourceType) in this variable
private string m_classKey;
//New instance of the data access layer
private StringResourcesDALC m_dalc;
#endregion
#region Constructors
/// <summary>
/// Constructor that creates a new instance of a resource provider using the resource type
/// </summary>
/// <param name="classKey">Resource type</param>
public DBResourceProvider(string classKey)
{
this.m_classKey = classKey;
m_dalc = new StringResourcesDALC(classKey);
}
#endregion
#region IResourceProvider Members
/// <summary>
/// Function that is called when we have explicit declarations of local and global resources
/// </summary>
/// <param name="resourceKey">Key of the resource</param>
/// <param name="culture">Culture code</param>
/// <returns>Resource value</returns>
public object GetObject(string resourceKey, CultureInfo culture)
{
//Call of the data access layer function to retreave the resource value from the database
string resourceValue = m_dalc.GetResourceByCultureAndKey(culture, resourceKey);
return resourceValue;
}
//Property that returns a new resource reader used to get local resources wich have been declared implicitly
public System.Resources.IResourceReader ResourceReader
{
get
{
//Call of the data access layer function that returns all resource keys and values for a single culture
ListDictionary resourceDictionary = this.m_dalc.GetResourcesByCulture(CultureInfo.InvariantCulture);
return new DBResourceReader(resourceDictionary);
}
}
#endregion
}
非常感謝您的寶貴時間。
對於GetObject()方法,無法保證ASP.NET提供當前區域性。 請嘗試以下方法:
public object GetObject(string resourceKey, CultureInfo culture) {
if (culture == null)
{
culture = CultureInfo.CurrentUICulture;
}
//Call of the data access layer function to retreave the resource value from the database
string resourceValue = m_dalc.GetResourceByCultureAndKey(culture, resourceKey);
return resourceValue;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.