Introduction
Recently I has the need to localize some .ASPX files. I did that the standard way by using the "Generate local resources" tool bar button in the design view of an .ASPX file inside Visual Studio .NET 2008.
Among others, I used the ASP.NET HyperLink control for hyperlinks and the ASP.NET Localize control for literal text.
What bothered me was that I often had a sentence with a hyperlink inside. E.g. "To get further information, please visit the support pages on our web site".
In the above example, I would have to split the sentence into two Localize
controls and one HyperLink
control. Too much effort for a lazy developer. In addition, the "split position" could vary among the different languages and the hyperlink also could be different in each language.
Introducing the PartialHyperLink Control
To make the described scenario more easy to handle, I wrote myself a light-weight control named "PartialHyperLink
".
The control consists of one single .CS file that you can drop into your (web) project and use immediately.
It contains the following features:
- Mimics the most common features of a standard
HyperLink
control - Has a
NavigateUrl
property that is localizable - Has a
Text
property that can include a start and an end placeholder to indicate where you want the clickable hyperlink
Using the Code
To use the control, simply put the "PartialHyperLink.cs" file into the "App_Code" folder of your web project.
Next, register the control on each .ASPX page you want to use it, by writing the following line at the top of the page:
<%@ Register Namespace="App_Code" TagPrefix="zeta" %>
Last, create an instance of the control by writing code similar to the following one:
<zeta:PartialHyperLink
runat="server"
ID="MyHyperLink"
Text="This text that {0}includes a hyperlink{1} is inside a single control"
NavigateUrl="http://www.zeta-test.com"
meta:resourcekey="MyHyperLinkResource1" />
Use the placeholders "{0}
" and "{1}
" inside the Text
property to mark the start and end of the clickable hyperlink.
That's all!
I've included a small example project to demonstrate how to use the control. If you open and run the project's solution, try switching the languages of your browser to see the different texts and hyperlinks that are loaded from the resources. I've included the languages English and German to give you an idea.
Conclusion
In this article, I have shown you a small user control that tries to simplify the task of translating literal texts with hyperlinks.
Maybe I am on a totally wrong path and there is already a built-in way of doing things that I implemented in my control. If yes, please tell me, I'd love to know!
In addition, if you have any questions, comments, bug reports or suggestions, please write them below in the comments section of this article. Thank you!
History
- 2009-06-12
- First published on The Code Project