Here is a snippet of code that can be used to dynamically localize content in a C# code-behind file:
Within the page's C# code:
protected override void Render(HtmlTextWriter writer)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter hWriter = new HtmlTextWriter(sw);
base.Render(hWriter);
writer.Write(this.Localize(sb.ToString()));
}
private const string ResourceFileName = "Resource";
private string Localize(string html)
{
MatchCollection matches = new Regex(@"Localize\(([^\))]*)\)", RegexOptions.Singleline | RegexOptions.Compiled).Matches(html);
foreach (System.Text.RegularExpressions.Match match in matches)
{
html = html.Replace(match.Value, GetGlobalResourceObject(ResourceFileName, match.Groups[1].Value).ToString());
}
return html;
}
Update an ImageButton with localization:
<asp:ImageButton
ID="divSection_btnAdd"
runat="server"
OnClientClick="return TConfirm(this,'Localize(Confirm)')"/>