In one of our project I had to refactor a code responsible for generating XML feed. The feed contains information like title, description and URL.

There are three languages enabled for the site: English, Swedish and Norwegian. All pages was first created in English and some page translations are missing for Norwegian and Swedish. That’s why language fallback behavior is enabled. Missing translations should be displayed in English.


fallback settings

The title and description for Norwegian pages that were fallbacked to English were generated as I expected. But there was a problem with the URL. The URL resolving code was like:

While I was debugging it turns out that all language related properties (Language, LanguageId, LanguageBranch, MasterLanguageBranch, MasterLanguage and MasterLanguageBranch) were set to “en”. This was reasonable because the fallback was used. Interesting was LinkURL value: “/link/69ca54f4309d432197a669996f57d3c8.aspx?epslanguage=sv”. After I used UrlRewriteProvider.ConvertToExternal it was converted to friendly URL “/sv/page-segment/”.

The Swedish URL version appeared because, when I was debugging, I used Swedish domain in browser window. The LinkURL property is created based on ContentLanguage.PreferredCulture.Name value. It use Context (and it was HttpContext) to obtain the current language. It doesn’t matter how the LanguageLoaderOption was set during resolving ContentReference.

fallback context language settings

The PrefferedCulture works fine when you are in web context and when all links should keep current domain.

To solve the problem with friendly URLs I used UrlResolver instead of UrlRewriteProvider. The UrlResolver has GetUrl method that could be executed with language parameter. This parameter forces the creation of a URL in a specific culture. Now the item is generated with Norwegian URL – “/no/page-segment/”. The final code looks like: