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:
- is that a stupid solution?
- performance wise is it too bad?
- how could I manage the most dynamic content in a way non-techie people could edit a translation file?
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.