Dailycode.info

Short solution for short problems

AngularJS: How to build your own translation service

Ruben Biesemans started a post on how to implement your own translation service. Why you might ask. For him mainly educational purpose, for us, it's an opportunity to have a guideline on how to build your own translation service. Because many might find the existing solutions to complex or just not flexible enough. Here's a link to the part 1 and looking forward to the next part.

How to – Build your own Angular translation service – Part 1

spikesapps.wordpress.com

Intro This will be a first in a series of posts in which I will build a translation service for Angular from the ground up. I want to point out up front that I know there are multiple angular trans… read more here


Translate your ASP.Net page

A really simple and quick solution is the one we use here for some websites that need to be multi langual and where the tranlations are loaded in the database.

All pages that need translation inherit from a custom Page control. In this control we browse iterative through the page's child controls. We check for certain types of controls and the take the delivered text and tranlate it. The text provided on the controls will be the ID that is used to get the translation from the database. I use a "_" before the text if it needs to be translated, just to keep it flexible.

Here the code of the custom page object:

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using DevExpress.Web.ASPxGridView;

using GalvaSFIMobile.GlobalFunctions;



namespace My.Controls

{

    public class MyPage : Page

    {

        protected override void OnPreRender(EventArgs e)

        {

            //TranslateControls

            TranslateControls(this);

            base.OnPreRender(e);

        }



        private void TranslateControls(Control cont)

        {

            foreach (Control c in cont.Controls)

            {

                //Check if the control is a text control.

                if (c.GetType() == typeof(Label))

                {

                    //translate label

                    if (((Label)c).Text.StartsWith("_"))

                    {

                        ((Label)c).Text = Globalfunctions.GetTranslation(((Label)c).Text);

                    }

                }

                else if (c.GetType() == typeof(DevExpress.Web.ASPxEditors.ASPxLabel))

                {

                    //translate button

                    if (((DevExpress.Web.ASPxEditors.ASPxLabel)c).Text.StartsWith("_"))

                    {

                        ((DevExpress.Web.ASPxEditors.ASPxLabel)c).Text = Globalfunctions.GetTranslation(((DevExpress.Web.ASPxEditors.ASPxLabel)c).Text);

                    }

                }

                else if (c.GetType() == typeof(DevExpress.Web.ASPxEditors.ASPxButton))

                {

                    //translate button

                    if (((DevExpress.Web.ASPxEditors.ASPxButton)c).Text.StartsWith("_"))

                    {

                        ((DevExpress.Web.ASPxEditors.ASPxButton)c).Text = Globalfunctions.GetTranslation(((DevExpress.Web.ASPxEditors.ASPxButton)c).Text);

                    }

                }

                else if (c.GetType() == typeof(Button))

                {

                    //translate button

                    if (((Button)c).Text.StartsWith("_"))

                    {

                        ((Button)c).Text = Globalfunctions.GetTranslation(((Button)c).Text);

                    }

                }

                else if (c.GetType() == typeof(RadioButtonList))

                {

                   //translate listitem

                   

                    foreach (ListItem a in  ((RadioButtonList )c).Items )

                    {

                        if (((ListItem)a).Text.StartsWith("_"))

                        {

                            ((ListItem)a).Text = Globalfunctions.GetTranslation(((ListItem)a).Text);

                        }

                    }

                }

                else if (c.GetType() == typeof(RequiredFieldValidator))

                {

                    if (((RequiredFieldValidator)c).ErrorMessage.StartsWith("_"))

                    {

                        ((RequiredFieldValidator)c).ErrorMessage = Globalfunctions.GetTranslation(((RequiredFieldValidator)c).ErrorMessage);

                    }

                }

                else if (c.GetType() == typeof(ValidationSummary))

                {

                    if (((ValidationSummary)c).HeaderText.StartsWith("_"))

                    {

                        ((ValidationSummary)c).HeaderText = Globalfunctions.GetTranslation(((ValidationSummary)c).HeaderText);

                    }

                }

                else if (c.GetType() == typeof(RegularExpressionValidator))

                {

                    if (((RegularExpressionValidator)c).ErrorMessage.StartsWith("_"))

                    {

                        ((RegularExpressionValidator)c).ErrorMessage = Globalfunctions.GetTranslation(((RegularExpressionValidator)c).ErrorMessage);

                    }

                }

                else if (c.GetType() == typeof(ASPxGridView))

                {

                    foreach(GridViewColumn col in ((ASPxGridView)c).Columns)

                    {

                        if (col.Caption.StartsWith("Grid."))

                        {

                            col.Caption = Globalfunctions.GetTranslation(col.Caption);

                        }

                    }

                }

                if (c.HasControls())

                {

                    TranslateControls(c);

                }

            }

        }

    }

}

The Globalfunctions.GetTranslation(string trans); function simply gets the translation from the database. Make sure that you cache your translations! Because else the translation of the page will result in a delay. I use the entity framework in combination with the singleton pattern to cache the translation entities. Its working very good.