Think you have a localized asp.net mvc application; you also need to localize your attributes to completely satisfy your visitors. Yeah! Here is a simple class that you can add into your models folder in other to use a [GlobalizableAttributes] attribute over your models properties.
So, let’s see briefly what we’re going to see in detail :
To follow this lesson, you must:
If you are ready, let’s go !
The first thing we will do is creating a LocalizableAttribute class. An attribute class is a simple class that inherits from the dotnet Attribute class. But in this tutorial, our class will inherit from the DisplayNameAttribute class because what we want to do is not difference than what this class does, except the fact that we need our attributes to be localizable.
Here is that famous class
using System;
usingSystem.ComponentModel;
usingSystem.ComponentModel.DataAnnotations;
namespaceLocalizableAttributes.Models
{
publicclassGlobalizableAttributes : DisplayNameAttribute
{
privateDisplayAttribute display;
privatestring _resource;
publicGlobalizableAttributes(TyperesourceType, stringresourceName)
{
this.display = newDisplayAttribute() { ResourceType = resourceType, Name = resourceName };
this._resource = resourceName;
}
publicoverridestringDisplayName
{
get
{
returndisplay.GetName();
try
{
returndisplay.GetName();
}
catch (Exception ex)
{
return"[" + this._resource + "]";
}
}
}
}
}
We add using directive to import data annotations namespace.
Our GlobalizableAttributes class constructor has two properties, these properties help us to initializing the base class(DisplayNameAttribute).
The other new thing is the get magic method. First of all, we overridden the parent getter, and then we use the try statement to handle exceptions. In case the resource type or name does not exist, we return that resource name into ‘[]’. This avoids errors when a resource file is not ready or has been deleted.
Open your accountModels.csfile, we are going to use the logOnModel class. Change this section of code.
Before
publicclassLogOnModel
{
[Required]
[Display(Name = "User name")]
publicstringUserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
publicstring Password { get; set; }
[Display(Name = "Remember me?")]
publicboolRememberMe { get; set; }
}
After
publicclassLogOnModel
{
[Required]
[GlobalizableAttributes(typeof(logOnStrings), "UserNameString")]
publicstringUserName { get; set; }
[Required]
[DataType(DataType.Password)]
[GlobalizableAttributes(typeof(logOnStrings), "PasswordString")]
publicstring Password { get; set; }
[GlobalizableAttributes(typeof(logOnStrings), "RememberString")]
publicboolRememberMe { get; set; }
}
Notes:
You may need to add some other directive(s), to import your resource files. For example you may write using LocalizableAttributes.MyResourcesFiles.GlobalModels; the logOnStrings in this codes refers to a resource file in my application. So, you might have another name for your public resource files…
Now each time the user changes his language, the models will follow this language too. Have a look to the image bellow, I change the language of the application and I got this:

Notice the form fields are in Portuguese, this occurs when the user changes his language. Use the tutorial listed on the top of this lesson if you want to have a localizable application created by yourself.
Now you can write your models attributes with the ability to be localizable. But the question you can ask yourself is what is the difference between Globalization and Localization? This is a frequently asked question. I can’t explain it within this tutorial. If you are curious, the answer is on the msdn website. Thank you for reading me! Have a question? Ask in private or write comment. See you Next week!





See the prices for this post in Mr.Bool Credits System below: