Click here to Skip to main content
Click here to Skip to main content
Go to top

Better support for localization in ASP.NET

, 5 Jan 2013
Rate this:
Please Sign up or sign in to vote.
Support for localization in ASP.NET.

If you’ve tried the built in localization features in ASP.NET you’ll probably written cluttered view models like:

 public class UserViewModel
{
    [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resources.LocalizedStrings))]
    [LocalizedDisplayName(ErrorMessageResourceName = "UserId", ErrorMessageResourceType = typeof(Resources.LocalizedStrings))]
    [LocalizedDescription(ErrorMessageResourceName = "UserIdDescription", ErrorMessageResourceType = typeof(Resources.LocalizedStrings))]
    public int Id { get; set; }

    [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resources.LocalizedStrings))]
    [LocalizedDisplayName(ErrorMessageResourceName = "UserFirstName", ErrorMessageResourceType = typeof(Resources.LocalizedStrings))]
    [LocalizedDescription(ErrorMessageResourceName = "UserFirstNameDescription", ErrorMessageResourceType = typeof(Resources.LocalizedStrings))]
    public string FirstName { get; set; }

    [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resources.LocalizedStrings))]
    [LocalizedDisplayName(ErrorMessageResourceName = "UserLastName", ErrorMessageResourceType = typeof(Resources.LocalizedStrings))]
    [LocalizedDescription(ErrorMessageResourceName = "UserLastNameDescription", ErrorMessageResourceType = typeof(Resources.LocalizedStrings))]
    public string LastName { get; set; }
}

I’ve solved that with my Griffin.MvcContrib project. But the solution is more of an hack than a solid approach.

The problem is really that there is no way to inject your own custom strings into the localization process of the DataAnnotation attributes. They use their own string tables (resource files) and that’s it. Well, unless you use attribute properties as shown above.

A much cleaner approach would be if the attributes asked a central class about the strings. Something like:

public static class LocalizationStringProvider
{
    public static string GetValidationMessage(Type attributeType, string text)
    {
        // scan registered providers after the text
    }
    
    public static string GetModelText(Type model, string propertyName, string metadataName)
    {
        // scan registered providers after the text
    }
    
    // add a new one
    public void Register(ILocalizationStringProvider provider)
    {
    }
    
    // remove all providers
    public void Clear()
    {}
}

Which in turn will get it’s text from a registered provider:

public interface ILocalizationStringProvider
{
    string GetValidationMessage(Type attributeType, string text);
    string GetModelText(Type model, string propertyName, string metadataName);
}

In this way we can by ourself choose if we want to use string tables, a SQL database, flat files or any other source. It will also work for attributes like the ASP.NET MVC CompareAttribute which requires to get a localized version of the property name which it’s compared to.

Hence to customize the localization of model and validation strings would simply be:

LocalizationStringProvider.Clear();
LocalizationStringProvider.Add(new MySqlProvider());

Summary

Vote at uservoice.com if you like this idea and want it implemented in ASP.NET.

Disclaimer: The decision of using a static class in this example was chosen since it’s how the other extension points of ASP.NET is made.

The post Better support for localization in ASP.NET. appeared first on jgauffin's coding den.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

jgauffin
Founder Gauffin Interactive AB
Sweden Sweden
Founder of OneTrueError, a .NET service which captures, analyzes and provide possible solutions for exceptions.
 
blog | twitter
Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 5 Jan 2013
Article Copyright 2013 by jgauffin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid