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

Satellite Assembly - Multi Languages

Rate me:
Please Sign up or sign in to vote.
4.35/5 (20 votes)
18 Jul 20053 min read 242.1K   3.1K   69   51
Creating a multi language ASP.NET site using Satellite Assemblies.

Image 1

Introduction

As a professional developer, your clients might ask you to develop a website in which users can choose the language they want the site to be in. Somehow, this scenario can be done by developing several pages with several languages. Finally, you can satisfy the clients' requirements. However, it takes you a very long time, and it also wastes your money. There is a faster way to accomplish the requirement. Let's enjoy!

Using the code

First of all, we will take a look at the way a Satellite Assembly works. We won't make several pages in several languages. There is only one project with several resource files to save the translated strings. Let's do an example!

Create a new project in Microsoft Visual Studio .NET 2003. In the default web form, we start developing this project.

Notice: For the elements whose text you want to be displayed in many languages, you have to set the id and runat attributes. By default, the server-side controls have these attributes, but you have to add them to HTML elements.

For instance, if your site has the HTML code like this:

HTML
<body>
   <form id="Form1" method="post" runat="server">
      <p><b>Welcome to the Satellite Assembly...</b></p>
      <b>Select your language:</b>
        <asp:DropDownList 
         id="drpLanguages" runat="server"></asp:DropDownList>
        <asp:Button id="btnLanguages" runat="server" Text="Button"></asp:Button>
      <P>-----------------</P>
      <P> <STRONG>Thang Q. Tran</STRONG></P>
      <P>.NET Developer</P>
      <P><STRONG><a href="http://www.xtremebiz.biz">Xtreme 
                        Biz</a></STRONG></P>
   </form>
</body>

But when you want to develop with Satellite Assemblies, you have to add id and runat attributes to the HTML elements which you want to display in many languages. Then, the HTML code will be:

HTML
<body>
   <form id="Form1" method="post" runat="server">
      <p id = "p1" runat = "server"><b>Welcome to the 
                      Satellite Assembly...</b></p>
      <b id = "p2" runat = "server">Select your language:</b> 
      <asp:DropDownList id="drpLanguages" runat="server"></asp:DropDownList>
      <asp:Button id="btnLanguages" runat="server" Text="Button"></asp:Button></P>
      <P>-----------------</P>
      <P> <STRONG>Thang Q. Tran</STRONG></P>
      <P>.NET Developer</P>
      <P><STRONG><a href="http://www.xtremebiz.biz">Xtreme 
                            Biz</a></STRONG></P>
   </form>
</body>

Now, the user interface is okay! Let's make the resource files!

Create a fallback resource file containing the default strings to display in the user interface if the user's culture is not specified or not recognized. Name the fallback resource file <filename>.resx. For example: strings.resx.

Then, we create resource files containing the translated strings to display for each general language that your web application supports. Name the translated resource files <filename>.<languagecode>.resx. For example: strings.es.resx.

To create the fallback, from Project menu, choose Add > Add New Item and choose Assembly Resource File from the list of templates. Name it string.resx. And edit the file as you can like in the picture below:.

Image 2

The fallback resource file is done now. Let's create other resource files for other languages, in this case, German and Vietnamese.

This is the content of the file string.en-US.resx for English:

Image 3

And this is the content of the file string.de-DE.resx for German:

Image 4

Finally, this is the file string.vi-VN.resx for Vietnamese.

Image 5

Now, we are going to start to write some code for it. First, we have to Import some namespaces:

VB
Imports System.Resources
Imports System.Globalization
Imports System.Threading

Add the following code at the class level of the Web form to load the resources into the ResourceManager object:

VB
Protected gStrings As New _
  ResourceManager("SatelliteAssemblies.string", _
  GetType(WebForm1).Assembly)

Notice:

The bold text in the previous code is the name of the assembly.

This is the code for the Page_Load event:

VB
Private Sub Page_Load(ByVal sender As System.Object, _
         ByVal e As System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here

    If Not Page.IsPostBack Then
        drpLanguages.Items.Add(New _
          System.Web.UI.WebControls.ListItem("English", "en-US"))
        drpLanguages.Items.Add(New _
          System.Web.UI.WebControls.ListItem("Deutsch", "de-DE"))
        drpLanguages.Items.Add(New _
          System.Web.UI.WebControls.ListItem("Ti&#7871;ng Vi&#7879;t", "vi-VN"))

        Dim sLang As String = "en-US"
        SetCulture(sLang)
    End If
End Sub

In the Page_Load event, we call a method SetCulture() with one parameter:

VB
Private Sub SetCulture(ByVal sLang As String)
        Thread.CurrentThread.CurrentUICulture = New CultureInfo(sLang)
        Thread.CurrentThread.CurrentCulture = _
                 CultureInfo.CreateSpecificCulture(sLang)
        p1.InnerHtml = gStrings.GetString("p1")
        p2.InnerHtml = gStrings.GetString("p2")
        btnLanguages.Text = gStrings.GetString("btnLanguages")
End Sub

In this method, we receive one parameter as the code for the language. Then we set the CurrentCulture to this language. When the button is clicked, we get the value of the selected item from the drop down list and call the method SetCulture() with the parameter as this value.

VB
Private Sub btnLanguages_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles btnLanguages.Click
    Dim sLang As String = drpLanguages.SelectedItem.Value
    SetCulture(sLang)
End Sub

Now, everything is done! Why don't you test the project? Try to change the value in the drop down list and click the button to change the language. Does it work well?

You can develop a website with more and more languages with less time by using Satellite Assemblies! The only thing you have to do is create more resource files.

Sources:

  • Developing Web Applications with Microsoft Visual Basic .NET and Microsoft Visual C# .NET (Exams 70-305 and 70-315) e-Book

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Vietnam Vietnam
I've been working as a freelance iOS developer since the invention of the original iPhone. After numbers of projects, I gain more and more experience, get familiar with popular 3rd-party libraries which can save time and cost in the development.

With the experiences gained by working on projects, I proudly offer consultancy service which supports my customers to (1) get the best way to develop their apps; (2) start up iOS apps business professionally & effectively.

My day starts with the practices Japanese traditional Martial Arts: Kendo (http://bit.ly/18gJm5) & Iaido (http://bit.ly/atyeSB). Therefore, I always work with Samurai Spirit in the entire day, no FEAR and willing to take CHALLENGES!

If you need to get your iOS games or apps done, I'm available at oDesk!

Comments and Discussions

 
GeneralThanks so much ! Pin
marco bosk26-Nov-14 5:49
marco bosk26-Nov-14 5:49 
Questiong. Pin
Felipe Dias22-Jun-13 2:55
Felipe Dias22-Jun-13 2:55 
Questiong. Pin
Felipe Dias22-Jun-13 2:54
Felipe Dias22-Jun-13 2:54 
GeneralChange culture but no effects on the resx file Pin
gnafron18-Feb-09 4:46
gnafron18-Feb-09 4:46 
QuestionDo you know how to use the images in resource file? Pin
Ganesan Sankaran27-Sep-07 19:31
Ganesan Sankaran27-Sep-07 19:31 
GeneralHi Can you please help out in this one Pin
Ganesan Sankaran27-Sep-07 0:01
Ganesan Sankaran27-Sep-07 0:01 
AnswerRe: Hi Can you please help out in this one Pin
Ganesan Sankaran27-Sep-07 19:26
Ganesan Sankaran27-Sep-07 19:26 
GeneralThanks Pin
vplus12-Jul-07 3:21
vplus12-Jul-07 3:21 
QuestionHow to display the content in HTML. Pin
Kevin Bhavsar9-Dec-06 1:37
Kevin Bhavsar9-Dec-06 1:37 
QuestionHelp please... Pin
kitkits29-Jul-06 0:15
kitkits29-Jul-06 0:15 
AnswerRe: Help please... Pin
Nir Kovalio29-Jul-06 22:20
Nir Kovalio29-Jul-06 22:20 
GeneralRe: Help please... Pin
kitkits30-Jul-06 6:09
kitkits30-Jul-06 6:09 
GeneralAutomatically translated! Pin
Thang T.12-Jul-06 18:59
Thang T.12-Jul-06 18:59 
GeneralThank you Pin
Eniac030-May-06 4:32
Eniac030-May-06 4:32 
QuestionHow to install Dll GAC Pin
Anjum Rizwi22-May-06 20:03
Anjum Rizwi22-May-06 20:03 
AnswerRe: How to install Dll GAC [modified] Pin
ashutosh k. shukla24-Jun-06 10:08
ashutosh k. shukla24-Jun-06 10:08 
GeneralCode for French Language Pin
swati_m15-May-06 19:23
swati_m15-May-06 19:23 
GeneralRe: Code for French Language Pin
Thang T.15-May-06 19:27
Thang T.15-May-06 19:27 
GeneralRe: Code for French Language Pin
Eniac030-May-06 4:16
Eniac030-May-06 4:16 
GeneralRe: Code for French Language Pin
javiersolis19836-Jul-06 1:26
javiersolis19836-Jul-06 1:26 
GeneralRedirect to other page!!! Pin
RoyalVN15-Jan-06 22:34
RoyalVN15-Jan-06 22:34 
GeneralRe: Redirect to other page!!! Pin
Thang T.16-Jan-06 1:44
Thang T.16-Jan-06 1:44 
GeneralSatelliteLanguages in .NET 2005 Pin
RoyalVN13-Jan-06 16:04
RoyalVN13-Jan-06 16:04 
GeneralRe: SatelliteLanguages in .NET 2005 Pin
Thang T.13-Jan-06 18:41
Thang T.13-Jan-06 18:41 
GeneralRe: localization in .NET 2005 Pin
meduza1-Mar-06 22:45
meduza1-Mar-06 22:45 

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.