Veljko89
Veljko89

Reputation: 1953

Application Localization in asp.net

I am close to end of my ASP.net project, and now I am approaching to part where i need to translate my application.

My idea is to keep ID of control in my database table, on a page load I am simply taking "LanguageID" from Session variable that I stored earlier and make simply query to Database and end up with DataTable that looks like

ControlID     |     Text
--------------------------------
tabAutoMail   | E-MAIL PATTERN
tabUser       | LOG IN
lblSignIn     | Please sign in
lblinputEmail | Email address

But my problem here now is that, I don't know how to start next ... if i go with something like this

foreach (Control ctrl in Page.Controls)
{
    foreach (DataRow item in StaticTranslate.Rows)
    {
        if (ctrl.ID == item[0].ToString())
        {

        }
    }
}

Control doesn't have property of "Text", so I can't manually set it, if I go with something like this

foreach (DataRow item in StaticTranslate.Rows)
{ 
    var Test = this.Page.FindControl(item[0].ToString());
}

Again I end up with same problem, I don't know type of control and I can't cast "test" to proper control type

Can you please advice how to approach from here? Thank you

Upvotes: 3

Views: 96

Answers (1)

Batavia
Batavia

Reputation: 2497

As mentioned in the comments there are other ways of making translations work

If you want to make this work what you need to 2 is to store 3 properties

  1. The control name
  2. the property you want to localize
  3. the localized value

(and of course the language selection needs to be stored somehow/somewhere)

The reason being is that some controls can have localizations in other ways than just 'Text'. (I might want to localize an image source for example, or the maxlength of a postal code textbox can be language/country dependent)

To set a property you then might use code like this.

foreach (DataRow item in StaticTranslate.Rows)
{ 
    var control = this.Page.FindControl(item["ControlName"].ToString());
    var property = control .GetType().GetProperty(item["PropertyName"].ToString());
    property.SetValue(control, item["LocalizedValue"].ToString());
}

This only deals with string localizations now and if i did want to localize the maxlength then i'd need some more work.

I hope this convinced you to go the route NGPixel suggested and use a DB as a provider. If that's too complicated you could even allow your resx file to be updated shown here https://msdn.microsoft.com/en-us/library/gg418542(v=vs.110).aspx That way you could develop an admit tool to update your resx file and the rest of your site using 'standard' localization independently

Upvotes: 1

Related Questions