Click here to Skip to main content
15,123,367 members
Articles / Web Development / ASP.NET
Posted 23 Aug 2004


34 bookmarked

Create multilingual web pages very easily

Rate me:
Please Sign up or sign in to vote.
3.64/5 (21 votes)
23 Aug 2004CPOL2 min read
Create multilingual web pages very easily, without using localization and resources.

Sample Image - The page in German

Sample Image - The page in english


This article describes, how you can write multilingual web pages in an easy way, without using resources or localizations.

This article doesn't describe how you can translate a word or sentence from any language to another!


I am developing a big web application in the company I work for and the application should be multilingual.

In the complete project, we have a COM-component that translates words or sentences from any language to another. The basic mechanism of this component is a big text file and a hashlist.

I my example project, there exists an instance of a translation class with example translations. In real life, this is the instance of the translation-COM-component! Here you should write your own solution!

The base and default language in my example project is German. I use a session-variable to store the actual language over the session, and global for the complete web application. The switch-language-dropdownlist can be placed in a menuframe. Server side code should only refresh all frames to switch language on the fly.

Using the code

First step is to take the class CMLPageControl into your project. Copy the file CMLPageControl.vb in the project directory, and add the following lines to the vbproj-file.

    RelPath = "CMLPageControl.vb"
    SubType = "ASPXCodeBehind"
    BuildAction = "Compile"


All your WebForms must be derived from this class:

Public Class WebForm1
 Inherits CMLPageControl
End Class

Third and last:

To translate texts, you should put between the translation tags <ML_TAG> and </ML_TAG>.

<asp:Label id="Label1" runat="server">
  <ML_TAG>Hallo, hier wird demonstriert</ML_TAG>
<asp:Label id="Label2" font-bold="True" runat="server">
  <ML_TAG>wie man on the fly die Sprache wechselt!</ML_TAG>
<asp:Button id="Button1" runat="server" Text="<ML_TAG>Drück mich</ML_TAG>">
<asp:CheckBox id="CheckBox1" runat="server" 
          Text="<ML_TAG>Klick mich</ML_TAG>">

The translation-tag is defined as a constant in the base class.

Imports System.ComponentModel
Imports System.Web.UI

<DefaultProperty("Text"), ToolboxData("<{0}:CMLPageControl _
        runat="server"></{0}:CMLPageControl>")> _
        Public Class CMLPageControl _
        Inherits System.Web.UI.Page

End Class

The solution

The base class is a custom control to overload the Render method.

One problem is to agree the ready rendered HTML-code of the page including all sub user controls and placeholders and so on. I solved this problem while I creating my own HtmlTextWriter-Object and let the base class System.Web.UI.Page render the page in my writer. Now, I can get the HTML as string to translate all texts between the translation tags.

Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
  ' generate your own writer
  ' without this, you don't have a chance
  ' to get the rendered HTML-Code
  Dim oStringBuilder As New System.Text.StringBuilder
  Dim oWriter As New System.Web.UI.HtmlTextWriter(New _

  ' Render HTML-Code

Second problem was that the Render-methods of all .NET controls replace the brakes < and > from the translation-tags by &lt; and &gt;, if the text is set in code behind instead of the static presentation tier (aspx- or ascx-file). Example:

List1.Items.Add(New ListItem("<ML_TAG>Deutsch</ML_TAG>", _
List1.Items.Add(New ListItem("<ML_TAG>Englisch</ML_TAG>", _

To solve this, the translation routine is called twice. First to find all "normal" translation tags <ML_TAG>, second to find all modified translation tags &lt;ML_TAG&gt;.

' translate the page and write the result to the stream
output.Write(TranslatePage(TranslatePage(oStringBuilder.ToString(), _

The translation itself is a very simple search and replace. I think here is a great tuning potential. But in my real customer application, the translation of page with 200-300KB rendered HTML-code needs a duration of < 0.1 seconds. Most of the time is needed to find the text in the translation-component. The data behind this a 2MB text file.

Points of Interest

If you make mistakes while using the translation-tags (i.e., forget closing-tag or syntax error), the translation parser shows you the point of the mistake.

In the next screenshot, a start tag has a writing error:

<asp:Label id="Label1" runat="server">
  <ML_xTAG>Hallo, hier wird demonstriert</ML_TAG>

Detecting mistakes in translation tags


Initial version.


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


About the Author

Stephan Pilz
Web Developer
Germany Germany
I started 1987 when I was about 14 years old, with Basic and Pascal on 8Bit Atari with insanely 130KB of memory, later 16Bit Atari (4Megs). In study I learn C, C++, Modula-2, Assembler and other science languages (Prolog, ADA). I hold a german engineer's degree Dipl.-Ing. (FH) in "Technical Computer Science" from the University of Applied Sciences Mittweida, Germany (comparable with a Master Degree).

Now I develop PPS and Supply-Chain-Software for middle and great companies in Chemnitz/Germany with Web- and MFC-Frontends in C, C++, ASP.NET, ADO.NET, XML, HTML, JavaScript.

My main tasks are specify and write down solutions of problems and implement the first code snippets.

In my spare time I like to drive a (fast) motocycle, read many science fiction and fantasy books and I'm a enthusiastically shotokan karate fighter. In winter I falling down from hill with alpin ski on foots, because snowboards are only for drug sniffers Wink | ;-)

Comments and Discussions

GeneralAtlas UpdatePanel problem Pin
vlad.pitaru18-Dec-06 10:25
Membervlad.pitaru18-Dec-06 10:25 
GeneralRe: Atlas UpdatePanel problem Pin
Stephan Pilz18-Dec-06 21:37
MemberStephan Pilz18-Dec-06 21:37 
QuestionHow to enter data in particular language itself Pin
tnsenthil12-Oct-06 6:26
Membertnsenthil12-Oct-06 6:26 
AnswerRe: How to enter data in particular language itself Pin
Stephan Pilz12-Oct-06 22:01
MemberStephan Pilz12-Oct-06 22:01 
GeneralRe: How to enter data in particular language itself Pin
tnsenthil12-Oct-06 23:41
Membertnsenthil12-Oct-06 23:41 
GeneralRe: How to enter data in particular language itself Pin
Stephan Pilz13-Oct-06 0:20
MemberStephan Pilz13-Oct-06 0:20 
GeneralAnother Useful way for multilingual web pages Pin
mis003324-Jul-06 22:42
Membermis003324-Jul-06 22:42 
AnswerRe: Another Useful way for multilingual web pages Pin
Stephan Pilz25-Jul-06 0:08
MemberStephan Pilz25-Jul-06 0:08 
GeneralRe: Another Useful way for multilingual web pages Pin
mis003325-Jul-06 1:09
Membermis003325-Jul-06 1:09 
GeneralPlease help Pin
sreejith ss nair18-Sep-05 22:11
Membersreejith ss nair18-Sep-05 22:11 
AnswerRe: Please help Pin
Stephan Pilz18-Sep-05 22:45
MemberStephan Pilz18-Sep-05 22:45 
GeneralRe: Please help Pin
sreejith ss nair20-Sep-05 20:38
Membersreejith ss nair20-Sep-05 20:38 
GeneralSIMPLE &amp; DIRECT Pin
helif22-Feb-05 8:42
Memberhelif22-Feb-05 8:42 
GeneralRe: SIMPLE &amp; DIRECT Pin
Stephan Pilz22-Feb-05 22:29
MemberStephan Pilz22-Feb-05 22:29 
GeneralRe: SIMPLE &amp; DIRECT Pin
Libin Chen22-May-07 16:08
MemberLibin Chen22-May-07 16:08 
GeneralThis is quite brilliant! Pin
yrleu14-Jan-05 7:23
Memberyrleu14-Jan-05 7:23 
GeneralRe: This is quite brilliant! Pin
Stephan Pilz14-Jan-05 11:32
MemberStephan Pilz14-Jan-05 11:32 
Generalgood method Pin
benoityip13-Sep-04 16:43
Memberbenoityip13-Sep-04 16:43 
GeneralRe: good method Pin
Stephan Pilz13-Sep-04 21:44
MemberStephan Pilz13-Sep-04 21:44 

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.