简体   繁体   中英

Localization of aspx page programmatically from page load

I am working on a new project where I am trying to stick with some self-defined rules to see if I can solve old problems in a better way but I have few doubts and I would love some feedback from more experienced developers .

I am developing UI layer with bootstrap and jQuery, aspx is basically generating static html and doing a lot of ajax.

One old problem is localization in few different languages. What I am doing is wrapping localized fragments of the page in custom tags like below:

<localization culture="en" runat="server">
    <h3>Add everything...</h3>
    <p><i>Remember you can add also <a href="">by email</a> and <a href="">by gtalk</a>.</i></p>
</localization>
<localization culture="it" runat="server">
    <h3>Aggiungi tutto...</h3>
    <p><i>Ricorda che puoi farlo anche <a href="">via email</a> e <a href="">via gtalk</a>.</i></p>
</localization>

Then in my page_load I call a recursive function:

  protected void Page_Load(object sender, EventArgs e)
  {
     Localization(Page);
  }

Which do the following:

  protected void Localization(Control parent)
  {
     foreach (Control c in parent.Controls)
     {
        if (c.GetType() == typeof(HtmlGenericControl))
        {
           HtmlGenericControl hgc = (HtmlGenericControl)c;
           if( hgc.TagName == "localization")
           {
              if (hgc.Attributes["culture"] != null && hgc.Attributes["culture"] != lu.ui_culture)
              {
                 c.Visible = false;
              }
           }
        }

        Localization(c);
     }
  }

I can see few drawbacks in this approach: I can't have the translations in a separate xml file and I am processing the html with a recursive function.

But at the same time I like it because it make it easy to translate fragments with a lot of html markup, and I can also have everything in one place.

In the past I was using a different approach, rendering the html with xslt+xml, but was a nightmare too, even if you succeed in being purist having no inline javascript and no inline CSS is visually complex to manage xml and html in one file.


So my questions are:

  1. is that a stupid solution?
  2. performance wise is it too bad?
  3. how could I manage the most dynamic content in a way non-techie people could edit a translation file?

It's not a stupid solution, it looks much simplier than doing some horrible xslt mangling :)

Performance wise it'll be ok, but ultimately it all depends on how many requests are hitting your server and if it can keep up with the load. If you expect a light load (a few users) you'll be fine.

Another solution is to store all of the translations in the DB, write a nice front-end so the translators can maintain them, and upon each page request fetch all of the translations for that particular page. The table for this would look something like:

tblControlTranslation (PageName (nvarchar), ControlName (nvarchar), Translation (nvarchar)).

By default each control would have the english in it (so you know what's what), and then the code would loop through each control and put the translated text in.

This does not really seem to "fit" into the localization model of ASP.NET. Why do you need to define all your localizations in your ASPX? Have you considered using standard ASP.NET localization facilities? Consider reviewing http://www.codeproject.com/Articles/38907/ASP-NET-Localization-Quick-Reference

You can create an custom resource provider which reads data from XML. It is incredibly simple to do so - we just wrote an custom resource provider which reads data from DB - we need to all end users to update some of the values - so the choice of DB. It would be relatively easy for non-developers to change an XML file - or you can create a simple UI on top of it easily enough. This would also allow you to more easily manage change in CSS names.

The current approach you are taking would litter your ASPXs with translations from all languages and would increase your maintainability headaches over a period of time. This approach would also lead to violating seperation of concerns principle - to an extent - as your page would be overloaded with doing its normal work and having the responsibility of localizing itself.

You can refer to the excellent and detailed MSDN article at Extending the ASP.NET 2.0 Resource-Provider Model for more details. Basically, you need to implement the following classes/interfaces and link them in via Web.config. Then you can have either page specific or global XML files from which you can read the localized values. This would also open the door to sharing localized values across pages if needed.

ResourceProviderFactory:

namespace System.Web.Compilation
{
    public abstract class ResourceProviderFactory
    {
        public abstract IResourceProvider CreateGlobalResourceProvider(string classKey);
        public abstract IResourceProvider CreateLocalResourceProvider(string virtualPath);
    }
}

IResourceProvider:

namespace System.Web.Compilation
{
    public interface IResourceProvider
    {
        IResourceReader ResourceReader { get; }

        object GetObject(string resourceKey, CultureInfo culture);
    }
}

Web.config changes:

<system.web>
    <globalization resourceProviderFactoryType="MyCompany.Localization.CustomResourceProviderFactory" />
</system.web>

Sample XML structure for XML files - either for each page or for full application:

<LocalizedValues>
    <Value Key="FirstTextBlock">
        <LocalizedFor Culture="en-us">
            ...
        </LocalizedFor>
        <LocalizedFor Culture="it">
            ...
        </LocalizedFor>
    </Value>
</LocalizedValues>

I ran some simple test, using the recursive function above, and doing some xslt to replace the text in the html according to the language selected. With the recursive function above it tool 0,001349 seconds, using the xslt it took 0,006989. Nevertheless at the end I choose to use xslt and then cache the results varying by language selected, to improve performance and be able to maintain all the translations in a single xml file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM