Getting Friendly, International URLs
It’s generally best practice to display friendly URL’s to web visitors instead of the standard PageLink attribute that the system uses (A URL like http://www.somesite.com/en-us/MyProductCategory/MyProduct/ looks a lot nicer than http://www.somesite.com/PageTemplates/HomePageTemplate.aspx?id=643&epslanguage=en-US)
To that end, I often find myself using a procedure called GetFriendlyURL, which takes a PageData object and a Boolean value, and returns a friendly URL based on the UrlRewriteProvider.ConvertToExternal function. It goes something like this:
public static string GetFriendlyUrl(PageData pd, bool Absolute)
{
UrlBuilder url = new UrlBuilder(pd.LinkURL);
EPiServer.Global.UrlRewriteProvider.ConvertToExternal(url, pd.PageLink, UTF8Encoding.UTF8);
if (!Absolute)
{
return url.ToString();
}
else
{
return GetBaseUrl() + url.ToString();
}
}
Unfortunately, I’ve noticed that this procedure tends to always return the link in the form that the PageData object was originally created in… so if I’m on a different locale, I’ll often get links returned that include the /en-us/ (our base language) even though they should technically be returning links in the locale the user is currently in.
I address this issue with a simple string replacement procedure, as seen below:
public static string FetchInternationalLink(String InternationalLink, String LanguageVersion)
{
return InternationalLink.Replace("en-us", LanguageVersion.ToLower());
}
Voila – clean, friendly, internationalized URL’s in just a few lines of code.
This post seems to be a bit outdated, but anyway. I ran into similar issue and found that better approach is to use:
new UrlBuilder(UriSupport.AddLanguageSelection(pd.LinkURL, pd.LanguageBranch));
That will add/set correct language query parameter to the link and will workout fine during link transformation to friendly URL.