Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to localize a property description in c#?

I'm working on a class that is going to be used by some people from another countries. I have to localize every message, warning e.c. so that they can understand what we mean. In many cases i achieved my goal. But these property attributes like descriptions are such a pain in the ass.

Here`s what I have right now:

[Category("Editable Values"), Description("Sets the minimum select...")]
    public Ampere Simin
    {
        get
        {...}
        set
        {...}
    }

and

[Category("Editable Values"), Description(Localisation.Simin)] // "Localisation" here is the internal resource file that i wrote for messages, warnings, exceptions and -unfortunately- descriptions
        public Ampere Simin
        {
            get
            {...}
            set
            {...}
        }

That's what I'm trying to do. But it's not possible to use Localisations this way. Any Suggestions about something that I can use instead of it?

like image 828
Bedir Yilmaz Avatar asked Sep 13 '11 08:09

Bedir Yilmaz


3 Answers

Subclasses:

[STAThread]
static void Main()
{   // just some example code to show it working in winforms, but
    // anything using System.ComponentModel should see the change
    Application.EnableVisualStyles();        
    Application.Run(new Form {Controls = {new PropertyGrid {Dock = DockStyle.Fill, SelectedObject = new Foo()}}});
}

class Foo
{   // assume the following literals are keys, for example to a RESX
    [LocalizedCategory("cat")]
    [LocalizedDescription("desc")]
    [LocalizedDisplayName("disp name")]
    public string Bar { get; set; }
}
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Parameter | AttributeTargets.Delegate | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter)]
class LocalizedDescriptionAttribute : DescriptionAttribute
{
    static string Localize(string key)
    {
        // TODO: lookup from resx, perhaps with cache etc
        return "Something for " + key;
    }
    public LocalizedDescriptionAttribute(string key)
        : base(Localize(key))
    {
    }
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event)]
class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
    static string Localize(string key)
    {
        // TODO: lookup from resx, perhaps with cache etc
        return "Something for " + key;
    }
    public LocalizedDisplayNameAttribute(string key)
        : base(Localize(key))
    {
    }
}
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Parameter | AttributeTargets.Delegate | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter)]
class LocalizedCategoryAttribute : CategoryAttribute
{
    public LocalizedCategoryAttribute(string key) : base(key) { }
    protected override string  GetLocalizedString(string value)
    {
            // TODO: lookup from resx, perhaps with cache etc
        return "Something for " + value;
    }
}
like image 200
Marc Gravell Avatar answered Sep 19 '22 07:09

Marc Gravell


I've combined Marc Gravell and dsmith's answers:

First: create an Attribute class named as LocalizedDescriptionAttribute

public class LocalizedDescriptionAttribute : DescriptionAttribute
{
    static string Localize(string key)
    {
        return YourResourceClassName.ResourceManager.GetString(key);
    }

    public LocalizedDescriptionAttribute(string key)
        : base(Localize(key))
    {
    }
}

Use it as LocalizedDescription (the word "Attribute" is not necessary)

public class Foo
{   
    // myString is a key that exists in your Resources file
    [LocalizedDescription("myString")]
    public string Bar { get; set; }
}
like image 31
Zanon Avatar answered Sep 18 '22 07:09

Zanon


[Required(ErrorMessageResourceName = "LogOnModel_UserName_Required",     
      ErrorMessageResourceType = typeof(Resources.Global))]    
[Display(Name = "LogOnModel_UserName_Required",resourceType = typeof(Resources.Global))]  
public string UserName { get; set; }

see: http://geekswithblogs.net/shaunxu/archive/2010/05/06/localization-in-asp.net-mvc-ndash-3-days-investigation-1-day.aspx

like image 23
Patrick Avatar answered Sep 20 '22 07:09

Patrick