Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Localizable/Globalizable Attributes in ASP.NET

In this article youll see how to localize your attributes to completely satisfy your visitors in a mvc asp.net application.

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 :

  1. Creating a Localizable attribute class
  2. How to use our new created attribute
  3. Handling errors and exceptions as well

Pre-requisites

To follow this lesson, you must:

  • Have MS Visual Studio 2010
  • be good with data annotations; if not start here or learn more
  • Have a localized application. If you don’t know how to create a localizable application, follow this other tutorial. I appreciate the good job of this author, thx guy ;)

If you are ready, let’s go !

Creating a Localizable attribute class

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 + "]";
                }
            }
        }
    }
}

Explanations

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.

How to use our new created attribute class?

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:

modelAtributtes

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.

SUMMARY

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!



C# 4.5 Developer ASP.NET 4.5, Php 5, JavaScript (jQuery 1.6), Silverlight 4 (Sooner Beta5) xHtml, html5 Css 3 PhotoShop CS3 Java

What did you think of this post?
Services

Related Posts

[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

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

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login