Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Satellite Assembly - Multi Languages

0.00/5 (No votes)
18 Jul 2005 1  
Creating a multi language ASP.NET site using Satellite Assemblies.

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:

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

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

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:

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

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

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

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:

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:

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:

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.

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