private string GetStringByExpressionInternal(string expression)
        {
            if (String.IsNullOrEmpty(expression))
                throw new ArgumentNullException("expression");

            expression = expression.Replace(" ", "");
            expression = expression.Replace(ResourceStartKey, "");
            expression = expression.Replace(ResourceEndKey, "");

            if (expression.Contains("Resources:"))
                expression = expression.Remove(expression.IndexOf("Resources:"), 10);

            var expressionFields = ResourceExpressionBuilder.ParseExpression(expression);
            if (expressionFields == null)
            {
                var context = HttpContext.Current;
                var msg = String.Format("{0} is not a valid string resource format.", expression);
                if (context == null)
                    throw new ApplicationException(msg);
                return String.Format(msg);
            }


            return GetString(expressionFields.ClassKey, expressionFields.ResourceKey);
        }
        protected string GetExplicitResourceString(string attributeName, string defaultValue, bool throwIfNotFound)
        {
            if (attributeName == null)
            {
                throw new ArgumentNullException("attributeName");
            }
            string globalResourceObject = null;

            if (this._resourceKeys != null)
            {
                string[] values = this._resourceKeys.GetValues(attributeName);
                if ((values == null) || (values.Length <= 1))
                {
                    return(globalResourceObject);
                }
                try
                {
                    globalResourceObject = ResourceExpressionBuilder.GetGlobalResourceObject(values[0], values[1]) as string;
                }
                catch (MissingManifestResourceException)
                {
                    if (defaultValue != null)
                    {
                        return(defaultValue);
                    }
                }
                if ((globalResourceObject == null) && throwIfNotFound)
                {
                    throw new InvalidOperationException(System.Web.SR.GetString("Res_not_found_with_class_and_key", new object[] { values[0], values[1] }));
                }
            }
            return(globalResourceObject);
        }
示例#3
0
        private static string GetResourceString(HttpContextBase httpContext,
                                                string expression,
                                                string virtualPath,
                                                object[] args)

        {
            ExpressionBuilderContext context = new ExpressionBuilderContext(virtualPath);

            ResourceExpressionBuilder builder = new ResourceExpressionBuilder();

            ResourceExpressionFields fields = (ResourceExpressionFields)builder
                                              .ParseExpression(expression,
                                                               typeof(string), context);


            if (!string.IsNullOrEmpty(fields.ClassKey))
            {
                return(string.Format((string)httpContext.GetGlobalResourceObject(
                                         fields.ClassKey,
                                         fields.ResourceKey,
                                         CultureInfo.CurrentUICulture),
                                     args));
            }


            return(string.Format((string)httpContext.GetLocalResourceObject(
                                     virtualPath,
                                     fields.ResourceKey,
                                     CultureInfo.CurrentUICulture),
                                 args));
        }
示例#4
0
        // Helpe method to retrieve localized string based on attribute name
        protected string GetExplicitResourceString(string attributeName, string defaultValue, bool throwIfNotFound)
        {
            if (attributeName == null)
            {
                throw new ArgumentNullException("attributeName");
            }

            string text = null;

            if (_resourceKeys != null)
            {
                string[] keys = _resourceKeys.GetValues(attributeName);
                if (keys != null && keys.Length > 1)
                {
                    try {
                        text = ResourceExpressionBuilder.GetGlobalResourceObject(keys[0], keys[1]) as string;
                    }
                    catch (MissingManifestResourceException) {
                        if (defaultValue != null)
                        {
                            return(defaultValue);
                        }
                    }

                    if (text == null && throwIfNotFound)
                    {
                        // throw if default value is not specified.
                        throw new InvalidOperationException(
                                  SR.GetString(SR.Res_not_found_with_class_and_key, keys[0], keys[1]));;
                    }
                }
            }

            return(text);
        }
示例#5
0
        /// <summary>
        /// Gets a resource fields.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <param name="virtualPath">The virtual path.</param>
        /// <returns>The resource fields.</returns>
        private static ResourceExpressionFields GetResourceFields(string expression, string virtualPath)
        {
            var context = new ExpressionBuilderContext(virtualPath);
            var builder = new ResourceExpressionBuilder();

            return((ResourceExpressionFields)builder.ParseExpression(expression, typeof(string), context));
        }
示例#6
0
 protected object GetLocalResourceObject(string resourceKey, Type objType, string propName)
 {
     if (this._resourceProvider == null)
     {
         this._resourceProvider = ResourceExpressionBuilder.GetLocalResourceProvider(this);
     }
     return(ResourceExpressionBuilder.GetResourceObject(this._resourceProvider, resourceKey, null, objType, propName));
 }
        private static string GetResourceString(HttpContextBase httpContext, string expression, string virtualPath, object[] args)
        {
            ResourceExpressionFields fields = ResourceExpressionBuilder.ParseExpression(expression);

            object formatObj = !String.IsNullOrEmpty(fields.ClassKey) ?
                               httpContext.GetGlobalResourceObject(fields.ClassKey, fields.ResourceKey, CultureInfo.CurrentUICulture) :
                               httpContext.GetLocalResourceObject(virtualPath, fields.ResourceKey, CultureInfo.CurrentUICulture);

            return((formatObj is string) ? String.Format((string)formatObj, args) : fields.ResourceKey);
        }
示例#8
0
        /// <devdoc>
        /// Return a Page-level resource object
        /// </devdoc>
        protected object GetLocalResourceObject(string resourceKey)
        {
            // Cache the resource provider in the template control, so that if a Page needs to call
            // this multiple times, we don't need to call ResourceExpressionBuilder.GetLocalResourceProvider
            // every time.
            if (_resourceProvider == null)
            {
                _resourceProvider = ResourceExpressionBuilder.GetLocalResourceProvider(this);
            }

            return(ResourceExpressionBuilder.GetResourceObject(_resourceProvider, resourceKey, null /*culture*/));
        }
示例#9
0
        protected virtual IDictionary <string, object> GetResourceStrings(IEnumerable <string> keys, string path)
        {
            if (keys == null)
            {
                return(null);
            }

            Dictionary <string, object> res = new Dictionary <string, object>();

            foreach (string key in keys)
            {
                ResourceExpressionFields fields = ResourceExpressionBuilder.ParseExpression(key);
                if (fields == null)
                {
                    continue;
                }

                object value;
                try
                {
                    bool isLocal = String.IsNullOrEmpty(fields.ClassKey);
                    if (!isLocal && fields.ClassKey.IndexOf(Path.AltDirectorySeparatorChar) >= 0)
                    {
                        path    = fields.ClassKey;
                        isLocal = true;
                    }

                    if (isLocal)
                    {
                        string lookupPath = ResourceHandler.EnsureAppAbsolute(path).TrimStart('~');
                        value = HttpContext.GetLocalResourceObject(lookupPath, fields.ResourceKey);
                    }
                    else
                    {
                        value = HttpContext.GetGlobalResourceObject(fields.ClassKey, fields.ResourceKey);
                    }
                }
                catch (Exception ex)
                {
                    value = "$$" + ex.Message + "$$";
                }

                if (value == null)
                {
                    continue;
                }
                res[GetKey(fields, path)] = value;
            }

            return(res);
        }
        public override object EvaluateExpression(string expression, object parseTimeData, Type propertyType, IServiceProvider serviceProvider)
        {
            System.Web.Compilation.ResourceExpressionFields fields;
            IResourceProvider provider;

            if (parseTimeData is System.Web.Compilation.ResourceExpressionFields)
            {
                fields = (System.Web.Compilation.ResourceExpressionFields)parseTimeData;
            }
            else
            {
                fields = ResourceExpressionBuilder.ParseExpression(expression);
            }
            if (string.IsNullOrEmpty(fields.ResourceKey))
            {
                return(null);
            }
            object obj2 = null;
            DesignTimeResourceProviderFactory designTimeResourceProviderFactory = ControlDesigner.GetDesignTimeResourceProviderFactory(serviceProvider);

            if (string.IsNullOrEmpty(fields.ClassKey))
            {
                provider = designTimeResourceProviderFactory.CreateDesignTimeLocalResourceProvider(serviceProvider);
            }
            else
            {
                provider = designTimeResourceProviderFactory.CreateDesignTimeGlobalResourceProvider(serviceProvider, fields.ClassKey);
            }
            if (provider != null)
            {
                obj2 = provider.GetObject(fields.ResourceKey, CultureInfo.InvariantCulture);
            }
            if (obj2 != null)
            {
                Type c = obj2.GetType();
                if (!propertyType.IsAssignableFrom(c))
                {
                    TypeConverter converter = TypeDescriptor.GetConverter(propertyType);
                    if ((converter != null) && converter.CanConvertFrom(c))
                    {
                        return(converter.ConvertFrom(obj2));
                    }
                }
            }
            return(obj2);
        }
示例#11
0
        // Helper method to retrieve localized string based on attribute name
        protected string GetImplicitResourceString(string attributeName)
        {
            if (attributeName == null)
            {
                throw new ArgumentNullException("attributeName");
            }

            string text = null;

            if (!String.IsNullOrEmpty(_resourceKey))
            {
                try {
                    text = ResourceExpressionBuilder.GetGlobalResourceObject(Provider.ResourceKey, ResourceKey + "." + attributeName) as String;
                }
                catch { }
            }

            return(text);
        }
示例#12
0
        public void WebStringTest()
        {
            string systemString = ResourceExpressionBuilder.GetWebString("System.No");

            Assert.True(systemString.Equals("No"));

            // Check for an other culture
            var currentUiCulture = Thread.CurrentThread.CurrentUICulture;

            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfoByIetfLanguageTag("sv-se");
            systemString = ResourceExpressionBuilder.GetWebString("System.No");
            Assert.True(systemString.Equals("Nej"));

            // Check fallback
            Thread.CurrentThread.CurrentCulture = currentUiCulture;
            systemString = ResourceExpressionBuilder.GetWebString("System.Yes");
            Assert.True(systemString.Equals("Yes"));

            Thread.CurrentThread.CurrentUICulture = currentUiCulture;
        }
示例#13
0
        public static string GetStringByExpression(this SenseNetResourceManager resourceManager, string expression)
        {
            if (string.IsNullOrEmpty(expression))
            {
                return(expression);
            }

            if (!expression.StartsWith(SenseNetResourceManager.ResourceStartKey) ||
                !expression.EndsWith(SenseNetResourceManager.ResourceEndKey))
            {
                return(null);
            }

            expression = expression.Replace(" ", "");
            expression = expression.Replace(SenseNetResourceManager.ResourceStartKey, "");
            expression = expression.Replace(SenseNetResourceManager.ResourceEndKey, "");

            if (expression.Contains("Resources:"))
            {
                expression = expression.Remove(expression.IndexOf("Resources:", StringComparison.Ordinal), 10);
            }

            var expressionFields = ResourceExpressionBuilder.ParseExpression(expression);

            if (expressionFields == null)
            {
                var context = HttpContext.Current;
                var msg     = $"{expression} is not a valid string resource format.";
                if (context == null)
                {
                    throw new ApplicationException(msg);
                }
                return(string.Format(msg));
            }

            return(resourceManager.GetString(expressionFields.ClassKey, expressionFields.ResourceKey));
        }
        private static string GetResourceString(HttpContextBase httpContext, string expression, string virtualPath, object[] args)
        {
            var context = new ExpressionBuilderContext(virtualPath);
            var builder = new ResourceExpressionBuilder();
            var fields  = (ResourceExpressionFields)builder.ParseExpression("LanguageResource, " + expression, typeof(string), context);

            string text;

            if (!string.IsNullOrEmpty(fields.ClassKey))
            {
                text = (string)httpContext.GetGlobalResourceObject(fields.ClassKey, fields.ResourceKey, CultureInfo.CurrentUICulture);
            }
            else
            {
                text = (string)httpContext.GetLocalResourceObject(virtualPath, fields.ResourceKey, CultureInfo.CurrentUICulture);
            }

            if (String.IsNullOrEmpty(text))
            {
                return(String.Empty);
            }

            return(String.Format(text, args));
        }
示例#15
0
 protected object GetGlobalResourceObject(string className, string resourceKey)
 {
     return(ResourceExpressionBuilder.GetGlobalResourceObject(className, resourceKey, null, null, null));
 }
示例#16
0
 protected object GetGlobalResourceObject(string className, string resourceKey, Type objType, string propName)
 {
     return(ResourceExpressionBuilder.GetGlobalResourceObject(className, resourceKey, objType, propName, null));
 }
示例#17
0
 /// <summary>
 /// Ctor
 /// </summary>
 /// <param name="value"></param>
 /// <param name="path"></param>
 protected internal ResourceJbstExtension(string value, string path)
     : base(value, path)
 {
     this.ResKey = ResourceExpressionBuilder.ParseExpression(this.Value);
 }