Recently we had an issue where versioned media items were always getting loaded from en language version instead of current context language(Sitecore 8.0) . We were unable to find the root cause of this and finally have to come up with a work around.
1. Create a custom media provider . Append language in the media url if media item template is versioned.
namespace MediaHandler
{
public class CustomMediaProvider:MediaProvider
{
public override string GetMediaUrl(MediaItem item, MediaUrlOptions options)
{
Assert.ArgumentNotNull(item, "item");
Assert.ArgumentNotNull(options, "options");
bool flag = options.Thumbnail || this.HasMediaContent(item);
bool flag2 = true;
if (!flag && item.InnerItem.Paths.Path.Length > 0)
{
if (!options.LowercaseUrls)
{
return item.InnerItem.Paths.Path;
}
return item.InnerItem.Paths.Path.ToLowerInvariant();
}
else if (options.UseDefaultIcon && !flag)
{
if (!options.LowercaseUrls)
{
return Themes.MapTheme(Settings.DefaultIcon);
}
return Themes.MapTheme(Settings.DefaultIcon).ToLowerInvariant();
}
else
{
Assert.IsTrue(this.Config.MediaPrefixes[0].Length > 0, "media prefixes are not configured properly.");
string text = this.MediaLinkPrefix;
if (options.AbsolutePath)
{
text = options.VirtualFolder + text;
}
else if (text.StartsWith("/", StringComparison.InvariantCulture))
{
text = StringUtil.Mid(text, 1);
}
string text2 = MainUtil.EncodePath(text, '/');
if (options.ToString().Length > 1 && !string.IsNullOrEmpty(StringUtil.ExtractParameter("la", options.ToString())))
{
flag2 = false;
}
Item item2 = Context.Database.GetItem(item.InnerItem.TemplateID);
if (flag2 && item2 != null && item2.Paths.FullPath.ToLower().Contains("/versioned/"))
{
text2 = "/" + Context.Language.CultureInfo.ToString() + text2;
}
if (options.AlwaysIncludeServerUrl)
{
text2 = FileUtil.MakePath(string.IsNullOrEmpty(options.MediaLinkServerUrl) ? WebUtil.GetServerUrl() : options.MediaLinkServerUrl, text2, '/');
}
string text3 = StringUtil.EnsurePrefix('.', StringUtil.GetString(new string[]
{
options.RequestExtension,
item.Extension,
"ashx"
}));
string text4 = options.ToString();
if (text4.Length > 0)
{
text3 = text3 + "?" + text4;
}
string text5 = "/sitecore/media library/";
string path = item.InnerItem.Paths.Path;
string str = MainUtil.EncodePath((!options.UseItemPath || !path.StartsWith(text5, StringComparison.OrdinalIgnoreCase)) ? item.ID.ToShortID().ToString() : StringUtil.Mid(path, text5.Length), '/');
string text6 = text2 + str + (options.IncludeExtension ? text3 : string.Empty);
if (!options.LowercaseUrls)
{
return text6;
}
return text6.ToLowerInvariant();
}
}
}
}
2. Replace Sitecore media provider with your custom media provider
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<mediaLibrary>
<mediaProvider>
<patch:attribute name="type">MediaHandler.CustomMediaProvider, [Assembly Name]</patch:attribute>
</mediaProvider>
</mediaLibrary>
</sitecore>
</configuration>
With above solution, Versioned media url will include language locale in its url and will always get loaded from current context language.