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

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 88.9K   1.1K   34   19
Create multilingual web pages very easily, without using localization and resources.

Sample Image - The page in German

Sample Image - The page in english

Introduction

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!

Background

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.

XML
<File
    RelPath = "CMLPageControl.vb"
    SubType = "ASPXCodeBehind"
    BuildAction = "Compile"
/>

Second:

All your WebForms must be derived from this class:

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

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

VB
Imports System.ComponentModel
Imports System.Web.UI

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

 Public Const MLPAGE_TRANSLATION_TAG As String = "ML_TAG"
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.

VB
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 _
                 System.IO.StringWriter(oStringBuilder))

  ' Render HTML-Code
  RenderChildren(oWriter)

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:

VB
List1.Items.Add(New ListItem("<ML_TAG>Deutsch</ML_TAG>", _
            Convert.ToString(CTranslation.MLPAGE_LANGUAGE_GERMAN)))
List1.Items.Add(New ListItem("<ML_TAG>Englisch</ML_TAG>", _
            Convert.ToString(CTranslation.MLPAGE_LANGUAGE_ENGLISH)))

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

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

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.NET
<asp:Label id="Label1" runat="server">
  <ML_xTAG>Hallo, hier wird demonstriert</ML_TAG>
</asp:Label><br>

Detecting mistakes in translation tags

History

Initial version.

License

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


Written By
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 9:25
vlad.pitaru18-Dec-06 9:25 
GeneralRe: Atlas UpdatePanel problem Pin
Stephan Pilz18-Dec-06 20:37
Stephan Pilz18-Dec-06 20:37 
QuestionHow to enter data in particular language itself Pin
tnsenthil12-Oct-06 5:26
tnsenthil12-Oct-06 5:26 
AnswerRe: How to enter data in particular language itself Pin
Stephan Pilz12-Oct-06 21:01
Stephan Pilz12-Oct-06 21:01 
GeneralRe: How to enter data in particular language itself Pin
tnsenthil12-Oct-06 22:41
tnsenthil12-Oct-06 22:41 
GeneralRe: How to enter data in particular language itself Pin
Stephan Pilz12-Oct-06 23:20
Stephan Pilz12-Oct-06 23:20 
GeneralAnother Useful way for multilingual web pages Pin
mis003324-Jul-06 21:42
mis003324-Jul-06 21:42 
AnswerRe: Another Useful way for multilingual web pages Pin
Stephan Pilz24-Jul-06 23:08
Stephan Pilz24-Jul-06 23:08 
GeneralRe: Another Useful way for multilingual web pages Pin
mis003325-Jul-06 0:09
mis003325-Jul-06 0:09 
GeneralPlease help Pin
sreejith ss nair18-Sep-05 21:11
sreejith ss nair18-Sep-05 21:11 
AnswerRe: Please help Pin
Stephan Pilz18-Sep-05 21:45
Stephan Pilz18-Sep-05 21:45 
GeneralRe: Please help Pin
sreejith ss nair20-Sep-05 19:38
sreejith ss nair20-Sep-05 19:38 
GeneralSIMPLE &amp; DIRECT Pin
helif22-Feb-05 7:42
helif22-Feb-05 7:42 
GeneralRe: SIMPLE &amp; DIRECT Pin
Stephan Pilz22-Feb-05 21:29
Stephan Pilz22-Feb-05 21:29 
GeneralRe: SIMPLE &amp; DIRECT Pin
Libin Chen22-May-07 15:08
Libin Chen22-May-07 15:08 
GeneralThis is quite brilliant! Pin
yrleu14-Jan-05 6:23
yrleu14-Jan-05 6:23 
GeneralRe: This is quite brilliant! Pin
Stephan Pilz14-Jan-05 10:32
Stephan Pilz14-Jan-05 10:32 
Generalgood method Pin
benoityip13-Sep-04 15:43
benoityip13-Sep-04 15:43 
GeneralRe: good method Pin
Stephan Pilz13-Sep-04 20:44
Stephan Pilz13-Sep-04 20:44 
This is precise that what I wanted to express with the article.

Thanks
Stephan

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.