locked
Parameterised Localization RRS feed

  • Question

  • User262162700 posted
    Hi,
    
    The closest thread thus far along these lines is http://forums.asp.net/t/1411493.aspx but it doesn't quite answer it.
    
    I'm using explicit expressions to localize :
      1 ) Tokens (like Name, Surname, ...)
      2 ) Error Messages (like Please Supply Your..)
    
    What I would like to do is define my error messages like :
      Supply = "Please supply your {0}"
    
    And the use a custom expression builder or something so that I can define
    the expression in my pages like so:
      <%$ ErrorMessage:Supply, Name %>
    
    And this must then evaluate to :
      "Please supply your <%$ Resources:Glossary, Name %>"
    
    or alternative interpret the Name token from the Resources and then insert 
    it like String.Format would
    
    Thanks
    
    Thursday, May 28, 2009 7:37 AM

Answers

  • User262162700 posted
    Using an ExpressionBuilder it's possible to achieve the above. Bear In mind it's normally better to localize
    entire strings as opposed to constructed strings - for my situation though this would suffice.
    
    Here's The ExpressionBuilder
    
     public class ErrorExpressionBuilder : ExpressionBuilder
        {
            // Create a method that will return the result 
            // set for the expression argument.
            public static object GetErrorMessage(string expression, Type target, string entry)
            {
                string[] args = expression.Split(',');
                string[] parm = new string[args.Length-1];
    
                ResourceManager Glossary = Resources.Glossary.ResourceManager;
                ResourceManager ErrorMessages = Resources.ErrorMessage.ResourceManager;
    
                for (int i = 1; i < args.Length; i++)
                {
                    parm[i - 1] = Glossary.GetString(args[i].Trim(), System.Threading.Thread.CurrentThread.CurrentCulture);
                }
    
                string msg = ErrorMessages.GetString(args[0].Trim(),System.Threading.Thread.CurrentThread.CurrentCulture);
                return String.Format(msg, parm);
            }
    
            public override object EvaluateExpression(object target, BoundPropertyEntry entry,
            object parsedData, ExpressionBuilderContext context)
            {
                return GetErrorMessage(entry.Expression, target.GetType(), entry.Name);
            }
    
            public override CodeExpression GetCodeExpression(BoundPropertyEntry entry,
            object parsedData, ExpressionBuilderContext context)
            {
                // CodeExpression Generation Parameters
                Type propertyType = entry.DeclaringType;
                PropertyDescriptor descriptor = TypeDescriptor.GetProperties(propertyType)[entry.PropertyInfo.Name];
                CodeExpression[] expressionArray = new CodeExpression[2];
    
                // Generate The Method Call To Resolve The Expression
                expressionArray[0] = new CodePrimitiveExpression(entry.Expression.Trim());
                expressionArray[1] = new CodeTypeOfExpression(propertyType);
                expressionArray[2] = new CodePrimitiveExpression(entry.Name);
    
                return new CodeCastExpression(descriptor.PropertyType, new CodeMethodInvokeExpression(new
               CodeTypeReferenceExpression(base.GetType()), "GetErrorMessage", expressionArray));
                
            }
    
            public override bool SupportsEvaluate
            {
                get { return true; }
            }
        }
    
    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 31, 2009 3:25 PM