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

Satellite Assembly - Multi Languages

, 18 Jul 2005
Rate this:
Please Sign up or sign in to vote.
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

About the Author

Thang T.
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!
Follow on   Twitter

Comments and Discussions

 
Questiong. PinmemberFelipe Dias22-Jun-13 2:55 
Questiong. PinmemberFelipe Dias22-Jun-13 2:54 
GeneralChange culture but no effects on the resx file Pinmembergnafron18-Feb-09 4:46 
QuestionDo you know how to use the images in resource file? PinmemberGanesan S27-Sep-07 19:31 
GeneralHi Can you please help out in this one PinmemberGanesan S27-Sep-07 0:01 
AnswerRe: Hi Can you please help out in this one PinmemberGanesan S27-Sep-07 19:26 
GeneralThanks Pinmembervplus12-Jul-07 3:21 
QuestionHow to display the content in HTML. PinmemberKevin Bhavsar9-Dec-06 1:37 
QuestionHelp please... Pinmemberkitkits29-Jul-06 0:15 
AnswerRe: Help please... PinmemberNir Kovalio29-Jul-06 22:20 
GeneralRe: Help please... Pinmemberkitkits30-Jul-06 6:09 
GeneralAutomatically translated! PinmemberThang Q. Tran12-Jul-06 18:59 
GeneralThank you PinmemberEniac030-May-06 4:32 
QuestionHow to install Dll GAC PinmemberAnjum Rizwi22-May-06 20:03 
AnswerRe: How to install Dll GAC [modified] Pinmemberashushukla24-Jun-06 10:08 
GeneralCode for French Language Pinmemberswati_m15-May-06 19:23 
GeneralRe: Code for French Language PinmemberThang Q. Tran15-May-06 19:27 
GeneralRe: Code for French Language PinmemberEniac030-May-06 4:16 
GeneralRe: Code for French Language Pinmemberjaviersolis19836-Jul-06 1:26 
GeneralRedirect to other page!!! PinmemberRoyalVN15-Jan-06 22:34 
GeneralRe: Redirect to other page!!! PinmemberThang Q. Tran16-Jan-06 1:44 
GeneralSatelliteLanguages in .NET 2005 PinmemberRoyalVN13-Jan-06 16:04 
GeneralRe: SatelliteLanguages in .NET 2005 PinmemberThang Q. Tran13-Jan-06 18:41 
GeneralRe: localization in .NET 2005 Pinmembermeduza1-Mar-06 22:45 
GeneralRe: localization in .NET 2005 Pinmembermeduza1-Mar-06 22:45 

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.140709.1 | Last Updated 19 Jul 2005
Article Copyright 2005 by Thang T.
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid