4

I am extending the asp.net resource provider. The problem is the extended resource provider is not detecting the page culture. The CultureInfo in the GetObject() method is always null.

To change the page culture i am using a listbox an overriding 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();
        }

And the GetObject() function:

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

This is the Resource Provider class:

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

    }

Thank you very much for your time.

0

1 Answer 1

1

For the GetObject() method, it is not guaranteed that the current culture is provided by ASP.NET. Try the following:

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;
} 
2
  • Why it is not guaranteed that the current culture is provided by ASP.NET? Why he can read the resource key and not the culture??
    – user536158
    Jan 10, 2011 at 13:31
  • 1
    The docs over at msdn.microsoft.com/en-us/library/… state "The implementing class could retrieve the CurrentUICulture property if a value for the culture parameter is not passed in." Also check the sample there, they also do this check.
    – maartenba
    Jan 10, 2011 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.