Ejemplo n.º 1
0
        /// <summary>
        /// Return the value associated with the key from the resource manager
        /// </summary>
        /// <returns>The value from the resources if possible otherwise the default value</returns>
        protected override object GetValue()
        {
            if (LogHelper.CanDebug())
            {
                LogHelper.Begin("LocalizationExtension.GetValue", "ResModul:{0}, Key:{1}, DefaultValue:{2}", ResModul, Key, DefaultValue);
            }

            if (string.IsNullOrEmpty(Key))
            {
                throw new ArgumentException("Key cannot be null");
            }

            object            result   = null;
            IResourceProvider provider = null;

            if (IsInDesignMode)
            {
                return(DefaultValue);
            }
            try
            {
                object resource = null;

                //allow resource trapping by calling the handler
                if (GetResource != null)
                {
                    resource = GetResource(ResModul, Key, CultureManager.Instance.UICulture);
                }

                if (resource == null)
                {
                    //get the provider
                    if (provider == null)
                    {
                        provider = CultureManager.Instance.Provider;
                    }

                    //get the localized resource
                    if (provider != null)
                    {
                        resource = provider.GetObject(this, CultureManager.Instance.UICulture);
                    }
                }

                //and then convert it to desired type
                result = provider.ConvertValue(this, resource);
            }
            catch (Exception err)
            {
                LogHelper.Manage("LocalizationExtension.GetValue", err);
            }

            try
            {
                // if it does not work, we ask the default value
                if (result == null)
                {
                    result = provider.GetDefaultValue(this, CultureManager.Instance.UICulture);
                }
            }
            catch (Exception err)
            {
                LogHelper.Manage("LocalizationExtension.GetValue", err);
            }

            LogHelper.End("LocalizationExtension.GetValue");

            return(result);
        }