Using Sitecore 8.2.current we're seeing la=en added to a number of our media library (unversioned) image usages (<img src="_.jpg?la=en" />).

In Sitecore version 7.something (7.2?) the recommendation was to override GetMediaUrl to work around Sitecore not having configuration options to cover this.

Is that still the best practice in Sitecore 8.x, or can we make a configuration change to workaround this?

In the master database we only have en in /sitecore/system/Languages.

Verifying with /sitecore/admin/showconfig.aspx, on linkManager we have <add name="sitecore" languageLocation="filePath" languageEmbedding="never" /> (only relevant attributes shown).

In case it mattered, I also added language="en" to the relevant <site> declaration, to no avail.

Thanks!

Update 1:

Thanks to some assistance in the comments, if we use Sitecore.Resources.Media.MediaManager.GetMediaUrl(imageField.MediaItem, mediaUrlOptions); and set mediaUrlOptions to new Sitecore.Resources.Media.MediaUrlOptions() or Sitecore.Resources.Media.MediaUrlOptions.Empty la=en is not added

If we use Sitecore.Links.LinkManager.ExpandDynamicLinks() with a Rich Text field type image sources are expanded, but include the language parameter.

  • So, even with languageEmbedding="never", the language still shows up in the image link? – Hishaam Namooya May 19 '17 at 17:58
  • That's correct. – James Skemp May 19 '17 at 18:09
  • How are you using the GetMediaUrl() method? Are you passing the MediaUrlOptions as parameter? – Hishaam Namooya May 19 '17 at 18:49
  • Depending upon the usage, it appears he has it using Sitecore.Links.LinkManager.ExpandDynamicLinks() when pulling Rich Text content fields or passing new Sitecore.Resources.Media.MediaUrlOptions(). Both are resulting in language being included. – James Skemp May 19 '17 at 20:10
  • If you provide the MediaUrlOption() as empty, it should not append the language. Example:` GetMediaUrl(item, MediaUrlOptions.Empty);` – Hishaam Namooya May 19 '17 at 20:14
up vote 0 down vote accepted

As a workaround, we ended up creating a custom media provider.

MediaProvider.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sitecore.Data.Items;
using Sitecore.Resources.Media;
using Sc = Sitecore;

namespace Wsb.Sitecore.Media
{
    public class MediaProvider : Sc.Resources.Media.MediaProvider
    {
        public override string GetMediaUrl(MediaItem item)
        {
            return base.GetMediaUrl(item)
                .Replace("la=en", "")
                .Replace("?&", "?")
                .Replace("??", "?")
                .TrimEnd('?')
                ;
        }

        public override string GetMediaUrl(MediaItem item, MediaUrlOptions options)
        {
            return base.GetMediaUrl(item, options)
                .Replace("la=en", "")
                .Replace("?&", "?")
                .Replace("??", "?")
                .TrimEnd('?')
                ;
        }
    }
}

Wsb.Sitecore.Media.config

Patch file:

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <mediaLibrary>
            <mediaProvider type="Sitecore.Resources.Media.MediaProvider, Sitecore.Kernel">
                <patch:attribute name="type">Wsb.Sitecore.Media.MediaProvider, Wsb.Sitecore.Media</patch:attribute>
            </mediaProvider>
        </mediaLibrary>
    </sitecore>
</configuration>

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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