2011-01-10 12 views
4

asp.netリソースプロバイダを拡張しています。 問題は、拡張されたリソースプロバイダがページカルチャを検出していないことです。 GetObject()メソッドのCultureInfoは常にnullです。ASP.NETリソースプロバイダの拡張

ページ文化を変更するにはi)はリストボックスオーバーライド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 

    } 

ありがとうございます。

答えて

1

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; 
} 
+0

なぜ現在のカルチャがASP.NETによって提供されているとは限りませんか?なぜ彼は文化ではなくリソースキーを読むことができるのですか? –

+1

http://msdn.microsoft.com/en-us/library/system.web.compilation.iresourceprovider.getobject.aspxのドキュメント上のドキュメント "cultureパラメタの値が次の場合、実装クラスはCurrentUICultureプロパティを取得できます。渡されません。また、そこにサンプルをチェックし、彼らはまた、このチェックを行います。 – maartenba

+0

私は今理解しています。ありがとうございました。 –

関連する問題