Click here to Skip to main content
Click here to Skip to main content

Localized Resource in ASP.NET 2005

, 2 May 2007
Rate this:
Please Sign up or sign in to vote.
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.

<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:

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:

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":

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:

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

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

<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:

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)

About the Author

DAT HAN

United States United States
No Biography provided

Comments and Discussions

 
Generalvietnamese font Pinmembermike53523-Aug-09 1:49 
GeneralRe: vietnamese font PinmemberDAT HAN14-Oct-09 15:35 
QuestionIs there any way to read the data from website? PinmemberGanesan Sankaran10-Oct-07 2:03 
QuestionHow to add the Image for Each Resource file PinmemberGanesan S28-Sep-07 2:36 
AnswerRe: How to add the Image for Each Resource file PinmemberDAT HAN1-Oct-07 3:40 
GeneralRe: How to add the Image for Each Resource file PinmemberGanesan Sankaran2-Oct-07 22:30 
GeneralRe: How to add the Image for Each Resource file PinmemberGanesan Sankaran2-Oct-07 22:50 
GeneralRe: How to add the Image for Each Resource file PinmemberDAT HAN3-Oct-07 4:46 
AnswerRe: How to add the Image for Each Resource file PinmemberGanesan Sankaran4-Oct-07 0:19 
GeneralRe: How to add the Image for Each Resource file PinmemberGanesan Sankaran4-Oct-07 18:44 
GeneralIt's working fine. PinmemberGanesan S27-Sep-07 21:33 
Questionerror PinmembermgtROC30-Aug-07 2:20 
AnswerRe: error PinmemberGanesan Sankaran4-Oct-07 2:20 
Questionusing system resource Pinmembersandeep_bca41-Aug-07 20:02 
GeneralNice Article....!!! PinmemberViReNdrA KuMaR KoHli28-May-07 20:49 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 3 May 2007
Article Copyright 2007 by DAT HAN
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid