Click here to Skip to main content
15,879,535 members
Articles / Web Development / ASP.NET
Article

Localized Resource in ASP.NET 2005

Rate me:
Please Sign up or sign in to vote.
3.57/5 (6 votes)
2 May 2007CPOL3 min read 56.9K   267   18   15
Working with localized resource (string and image) in ASP.NET 2005 and using DynamicImage to display image resource
Screenshot - Default.jpg

Introduction

Imagine that you are going to develop the site which supports multiple languages. The user can select a language to view the site or you can detect his/her browser setting to display your site in his/her mother language.

Background

I like the DynamicImage control which is not shipped with VisualStudio.NET 2005 release version. It is a demo on easy to use DynamicImage control to display a Bitmap object (a stream) gets from resource file. You need to have some background about Localized Resource.

Using the Code

In this demo, I will explain how to use Localized Resource in ASP.NET 2005 and DynamicImage control to display an image from a stream.

1. Working with Localized Resource

You need to create a default resource file first. Right click on the Web Project and select Add New Item, select Resource File in the Templates and name it as MyResource.resx. It will prompt to ask you to put the file in App_GlobalResources folder.

Enter resource text as below:

Screenshot - EditString.jpg

We will support English, Vietnamese, Korean, German, and French. Each localized resource file name needs to follow the format:

[DefaultResource].[LanguageCode]-[Region].resx 

For example, we have MyResource.resx as the default resource file for English-US. For Vietnamese resource files, we will have MyResource.vi-VN.resx where "vi" is Vietnamese language code and "VN" is region code. You can find all of the language and region codes here.

Here is the Project Explorer that we have:

Screenshot - ProjectExplorer.jpg

Enter Resource item for each language by double clicking on a resource file. The Name column is key and must be the same in every resource file. The Value column is a string in the specified language. Below is the resource file for Vietnamese language.

Screenshot - EditStringVI.jpg

Resource file can contain string, image, icon, audio, file, etc. In this demo, I will add flag image into each localized resource file to display country flag. Double click on MyResource.resx to open Resource Editor for default resource file, select Images:

Screenshot - AddImage.jpg

Then select Add Resource -> Add Existing File and select a flag image file.

Screenshot - AddFlagImage.jpg

Each resource file should contain an image resource call "Flag" which is the flag for each country.

We already set up for resource files. Now we need to design Default.aspx.

ASP.NET
<body>
    <form id="form1" runat="server">
        <asp:Label ID="helloLabel" runat="server" Text="Hello" ></asp:Label>
        <asp:Image ID="flagImage" runat="server" />
        <br />
        <asp:Label ID="selectLanguageLabel" runat="server" Text="Select a Language" >
        </asp:Label>
        <asp:DropDownList ID="languageDropDownList" runat="server" 
	AutoPostBack="True" 
	OnSelectedIndexChanged = "languageDropDownList_SelectedIndexChanged">
        </asp:DropDownList>
    </form>
</body>

Now in the Page_Load, we need to populate some languages in the dropdown list and get setting language from Request object and display the page in the selected language. Explore the code yourself as it is self-explained:

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    { 
        languageDropDownList.Items.Add(new ListItem("English", "en-US"));
        languageDropDownList.Items.Add(new ListItem("Vietnamese", "vi-VN"));
        languageDropDownList.Items.Add(new ListItem("French", "fr-FR"));
        languageDropDownList.Items.Add(new ListItem("Korean", "ko-KR"));
        languageDropDownList.Items.Add(new ListItem("German", "de-DE"));
            
        //get setting language from browser
        ListItem settingLanguage = languageDropDownList.Items.FindByValue
				(Request.UserLanguages[0]);
        if (settingLanguage != null)
        {
            settingLanguage.Selected = true;
        }
        else //not support setting language, get English as default
        {
            languageDropDownList.SelectedIndex = 0;
            languageDropDownList.AutoPostBack = true;
        }
    }
    LoadResource(languageDropDownList.SelectedItem.Value);
}

Working with localized resources in ASP.NET 2005 is much easier. We do not need to create an instant of ResourceManager to retrieve the resources. Remember we have a default resource file named MyResource.resx. You can use this name as an object. Take a look at the LoadResource function:

C#
private void LoadResource(string lang)
{
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
    Thread.CurrentThread.CurrentCulture =
                 CultureInfo.CreateSpecificCulture(lang);

    helloLabel.Text = MyResource.Hello;
    selectLanguageLabel.Text = MyResource.SelectLanguage;

    MyResource.Flag.Save(Server.MapPath("") + \\flag.jpg, 
			System.Drawing.Imaging.ImageFormat.Jpeg);
    flagImage.ImageUrl = "~/flag.jpg";
}

To access MyResource, you call System.Resources.MyResource whereas System.Resources is the namespace you can "include":

C#
using System.Resources; 

You need to set Thread.CurrentThread.CurrentUICulture to locate the right resource file based on the input language. One more thing you need to be aware of is that image resource is a stream and you cannot set a stream to image control. You need to save the Flag resource to your local disk and set ImageUrl to this temp image file.

2. DynamicImage

You see the problem of using Image control that we cannot set a stream of image. Dynamic Image control can help you. If you need more information about DyanamicImage, please read this.

You can download the source code from that site, add DynamicImage project into our solution, add reference to this DynamicImageControl and try it. We will add Default2.aspx and set this as the Start Page. You have to Register the DynamicImage control using the Register directive:

ASP.NET
<%@ Register Assembly="DynamicImage" Namespace="MsdnMag" TagPrefix="dimage" %>

Here is the inside of the body tag of Default2.aspx:

ASP.NET
<form id="form1" runat="server">
        <asp:Label ID="helloLabel" runat="server" Text="Hello" ></asp:Label>
        <dimage:DynamicImage ID="flagImage" runat="server" />
        <br />
        <asp:Label ID="selectLanguageLabel" runat="server" Text="Select a Language" >
        </asp:Label>
        <asp:DropDownList ID="languageDropDownList" runat="server" 
        AutoPostBack="True" 
        OnSelectedIndexChanged="languageDropDownList_SelectedIndexChanged">
        </asp:DropDownList>
</form>

What you need to do with DynamicImage control is this line:

C#
flagImage.Image = MyResource.Flag; 

MyResourceFlag.Flag returns a stream (a Bitmap object) and you set it to flagImage control. That's it.

History

  • 2nd May, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalvietnamese font Pin
mike53523-Aug-09 1:49
mike53523-Aug-09 1:49 
GeneralRe: vietnamese font Pin
DAT HAN14-Oct-09 15:35
DAT HAN14-Oct-09 15:35 
QuestionIs there any way to read the data from website? Pin
Ganesan Sankaran10-Oct-07 2:03
Ganesan Sankaran10-Oct-07 2:03 
QuestionHow to add the Image for Each Resource file Pin
Ganesan Sankaran28-Sep-07 2:36
Ganesan Sankaran28-Sep-07 2:36 
AnswerRe: How to add the Image for Each Resource file Pin
DAT HAN1-Oct-07 3:40
DAT HAN1-Oct-07 3:40 
GeneralRe: How to add the Image for Each Resource file Pin
Ganesan Sankaran2-Oct-07 22:30
Ganesan Sankaran2-Oct-07 22:30 
GeneralRe: How to add the Image for Each Resource file Pin
Ganesan Sankaran2-Oct-07 22:50
Ganesan Sankaran2-Oct-07 22:50 
Hi,

If i'm using normal ASP image control it's working fine.

But it's not working for DynamicImage control.

Do i need to anyother thing to be done for using the DynamicImage Control. Inform to me.




With Regards
Ganesan.S
Software Engineer

GeneralRe: How to add the Image for Each Resource file Pin
DAT HAN3-Oct-07 4:46
DAT HAN3-Oct-07 4:46 
AnswerRe: How to add the Image for Each Resource file Pin
Ganesan Sankaran4-Oct-07 0:19
Ganesan Sankaran4-Oct-07 0:19 
GeneralRe: How to add the Image for Each Resource file Pin
Ganesan Sankaran4-Oct-07 18:44
Ganesan Sankaran4-Oct-07 18:44 
GeneralIt's working fine. Pin
Ganesan Sankaran27-Sep-07 21:33
Ganesan Sankaran27-Sep-07 21:33 
Questionerror Pin
mgtROC30-Aug-07 2:20
mgtROC30-Aug-07 2:20 
AnswerRe: error Pin
Ganesan Sankaran4-Oct-07 2:20
Ganesan Sankaran4-Oct-07 2:20 
Questionusing system resource Pin
sandeep_bca41-Aug-07 20:02
sandeep_bca41-Aug-07 20:02 
GeneralNice Article....!!! Pin
Virendrak28-May-07 20:49
Virendrak28-May-07 20:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.