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

Create multilingual web pages very easily

, 23 Aug 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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

Second:

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

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.

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:

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

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

Share

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 Pinmembervlad.pitaru18-Dec-06 10:25 
GeneralRe: Atlas UpdatePanel problem PinmemberStephan Pilz18-Dec-06 21:37 
QuestionHow to enter data in particular language itself Pinmembertnsentil12-Oct-06 6:26 
AnswerRe: How to enter data in particular language itself PinmemberStephan Pilz12-Oct-06 22:01 
GeneralRe: How to enter data in particular language itself Pinmembertnsentil12-Oct-06 23:41 
GeneralRe: How to enter data in particular language itself PinmemberStephan Pilz13-Oct-06 0:20 
GeneralAnother Useful way for multilingual web pages Pinmembermis003324-Jul-06 22:42 
AnswerRe: Another Useful way for multilingual web pages PinmemberStephan Pilz25-Jul-06 0:08 
GeneralRe: Another Useful way for multilingual web pages Pinmembermis003325-Jul-06 1:09 
GeneralPlease help Pinmembersreejith ss nair18-Sep-05 22:11 

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 | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 24 Aug 2004
Article Copyright 2004 by Stephan Pilz
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid