1

I use the following block of code to generate the links of media items in a specific language:

    Sitecore.Links.UrlOptions urlOptions = Sitecore.Links.LinkManager.GetDefaultUrlOptions();
    urlOptions.AlwaysIncludeServerUrl = true;
    urlOptions.Language = Language.Parse(item.Lang);
    string lnk = Sitecore.Links.LinkManager.GetItemUrl(pdfItem, urlOptions);

...
linkfield.Url = lnk;

The link generated looks like this:

http://cd5-int.global.com/-/media/Files/Mediaroom/700767.pdf

My problem is it keeps redirecting me to the last language version i clicked on. It seems to be a cache problem because when i click the link opening my media, if the pdf opened in a new window is in the wrong language, i just need to refresh the page and then the pdf appears in the right language.

Did i miss some useful options in my urlOptions object ?

1
  • Have you tried using the Sitecore.Resources.Media.MediaManager.GetMediaUrl() method to generate your media links instead? Commented Dec 6, 2018 at 15:04

2 Answers 2

2

For media items you should use the MediaManager to generate links instead of the LinkManager. The MediaManager will return a link to the actual media item.

var urlOptions = new MediaUrlOptions();
urlOptions.Language = Language.Parse(item.Lang)                
var lnk = MediaManager.GetMediaUrl(item);
0

Try including the LanguageEmbedding option in your UrlOptions:

urlOptions.LanguageEmbedding = LanguageEmbedding.Always;

That should result in a URL that has the language in the querystring, like so:

http://cd5-int.global.com/-/media/Files/Mediaroom/700767.pdf?lang=en

2
  • Thank you for your help. Unfortunately the language still does not appear in the querystring. Do you know what might cause this problem ?
    – Slrg
    Commented Dec 6, 2018 at 14:30
  • Do you have different settings specified for your link provider in the config? You can check by going to /sitecore/admin/showconfig.aspx and searching for Sitecore.Links.LinkProvider. The attribute to look for is languageEmbedding. Commented Dec 6, 2018 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.