Ejemplo n.º 1
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));
        }
Ejemplo n.º 2
0
        public string GetResource(string strKey)
        {
            if (WebCaching.CurrentCulturInfo == null)
            {
                WebCaching.CurrentCulturInfo = new CultureInfo(WebCaching.CurrentLanguage);
            }
            if (strKey.Length == 0)
            {
                return(strKey);
            }
            try
            {
                ResourceExpressionFields fields = GetResourceFields(string.Format("Resource,{0}", strKey), "~/");
                object obj = System.Web.HttpContext.GetGlobalResourceObject(fields.ClassKey, fields.ResourceKey, WebCaching.CurrentCulturInfo);
                if (obj != null)
                {
                    return(obj.ToString());
                }
            }
            catch (Exception)
            {
            }

            return(strKey);
        }
Ejemplo n.º 3
0
 static string GetGlobalResource(ResourceExpressionFields fields, object[] args)
 {
     try
     {
         return(string.Format((string)HttpContext.GetGlobalResourceObject(fields.ClassKey, fields.ResourceKey, CultureInfo.CurrentUICulture), args));
     }
     catch { return((string)HttpContext.GetGlobalResourceObject(fields.ClassKey, fields.ResourceKey, CultureInfo.CurrentUICulture)); }
 }
 public ResourceExpressionEditorSheet(string expression, IServiceProvider serviceProvider) : base(serviceProvider)
 {
     if (!string.IsNullOrEmpty(expression))
     {
         ResourceExpressionFields fields = ParseExpressionInternal(expression);
         this.ClassKey    = fields.ClassKey;
         this.ResourceKey = fields.ResourceKey;
     }
 }
        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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Resolves the resource with the given expression.
        /// </summary>
        /// <param name="htmlHelper">The <see cref="HtmlHelper"/>.</param>
        /// <param name="expression">The expression.</param>
        /// <param name="args">An object array that contains zero or more objects to format.</param>
        /// <returns>The resource.</returns>
        public static string Resource(this HtmlHelper htmlHelper, string expression, params object[] args)
        {
            string path = (string)htmlHelper.ViewData[ViewPathKey];

            if (string.IsNullOrEmpty(path))
            {
                path = "~/";
            }

            ResourceExpressionFields fields = GetResourceFields(expression, path);

            if (!string.IsNullOrEmpty(fields.ClassKey))
            {
                return(GetGlobalResource(fields, args));
            }

            return(GetLocalResource(path, fields, args));
        }
Ejemplo n.º 8
0
        public static string GetKey(ResourceExpressionFields fields, string path)
        {
            if (fields == null)
            {
                return(String.Empty);
            }
            if (String.IsNullOrEmpty(fields.ClassKey))
            {
                if (String.IsNullOrEmpty(path))
                {
                    return(fields.ResourceKey);
                }

                path = ResourceHandler.EnsureAppRelative(path).TrimStart('~');

                return(path + ',' + fields.ResourceKey);
            }

            return(fields.ClassKey + ',' + fields.ResourceKey);
        }
        private static ResourceExpressionFields ParseExpressionInternal(string expression)
        {
            ResourceExpressionFields fields = new ResourceExpressionFields();
            int length = expression.Length;

            string[] strArray = expression.Split(new char[] { ',' });
            int      num      = strArray.Length;

            if (num > 2)
            {
                return(null);
            }
            if (num == 1)
            {
                fields.ResourceKey = strArray[0].Trim();
                return(fields);
            }
            fields.ClassKey    = strArray[0].Trim();
            fields.ResourceKey = strArray[1].Trim();
            return(fields);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Gets a local resource.
 /// </summary>
 /// <param name="path">The path.</param>
 /// <param name="fields">The fields.</param>
 /// <param name="args">The arguments.</param>
 /// <returns>The local resource.</returns>
 private static string GetLocalResource(string path, ResourceExpressionFields fields, object[] args)
 {
     return(string.Format(CultureInfo.CurrentCulture, (string)HttpContext.GetLocalResourceObject(path, fields.ResourceKey, CultureInfo.CurrentUICulture), args));
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Resolves the (global) resource with the given expression.
        /// </summary>
        /// <param name="controller">The controller.</param>
        /// <param name="expression">The expression.</param>
        /// <param name="args">An object array that contains zero or more objects to format.</param>
        /// <returns>The resource.</returns>
        public static string Resource(this Controller controller, string expression, params object[] args)
        {
            ResourceExpressionFields fields = GetResourceFields(expression, "~/");

            return(GetGlobalResource(fields, args));
        }
Ejemplo n.º 12
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);
 }
        public override object ParseExpression(string expression, Type propertyType, ExpressionBuilderContext context)
        {
            if (string.IsNullOrEmpty(expression))
            {
                throw new ArgumentException("Too few parameters");
            }

            ResourceExpressionFields fields = null;
            string classKey = null;
            string resourceKey = null;

            string[] expParams = expression.Split(new char[] { ',' });
            if (expParams.Length > 2)
            {
                throw new ArgumentException("Too many parameters");
            }
            if (expParams.Length == 1)
            {
                throw new ArgumentException("Too few parameters");
            }
            else
            {
                classKey = expParams[0].Trim();
                resourceKey = expParams[1].Trim();
            }

            fields = new ResourceExpressionFields(classKey, resourceKey);

            EnsureResourceProviderFactory();
            IResourceProvider rp = _resourceProviderFactory.CreateGlobalResourceProvider(fields.ClassKey);

            object res = rp.GetObject(fields.ResourceKey, CultureInfo.InvariantCulture);
            if (res == null)
            {
                throw new ArgumentException("Resource not found");
            }
            return fields;
        }