Quantcast
Channel: Optimizely Search & Navigation
Viewing all articles
Browse latest Browse all 6894

How to set culture specific alt text on an image

$
0
0

Hi guys, 

I have problem to change alt text property in the existing ImageFile class to culture specific.

I found this topic in episerver (https://world.episerver.com/forum/developer-forum/-Episerver-75-CMS/Thread-Container/2016/6/image-/)

I tried first to moidy the existing ImageFile and it didn't work. I created a new contenttype "LocalizedImageFile" and implement ILocalizable interface class instead.

[ContentType(GUID = "0A89E464-56D4-449F-AEA8-2BF774AB8745")]
    [MediaDescriptor(ExtensionString = "jpg,jpeg,jpe,ico,gif,bmp,png,pdf")]
    public class LocalizedImageFile : ImageData, ILocalizable
    {
        public IEnumerable<CultureInfo> ExistingLanguages { get; set; }
        public CultureInfo MasterLanguage { get; set; }
        [CultureSpecific(true)]
        public virtual string AlternateText { get; set; }
        public override Blob BinaryData
        {
            get
            {
                if (ContentReference.IsNullOrEmpty(this.ContentLink))
                {
                    return base.BinaryData;
                }
                if (this.Language.Name == this.MasterLanguage.Name)
                {
                    return base.BinaryData;
                }
                return this.LoadMasterContent<LocalizedImageFile>().BinaryData;
            }
            set
            {
                base.BinaryData = value;
            }
        }
        [ImageDescriptor(Height = 48, Width = 48)]
        public override Blob Thumbnail
        {
            get
            {
                if (ContentReference.IsNullOrEmpty(this.ContentLink))
                {
                    return null;
                }
                if (this.Language.Name == this.MasterLanguage.Name)
                {
                    return base.Thumbnail;
                }
                return this.LoadMasterContent<LocalizedImageFile>().Thumbnail;
            }
            set
            {
                base.Thumbnail = value;
            }
        }
    }
    public static class LocalizableMediaDataExtensions
    {
        public static T LoadMasterContent<T>(this LocalizedImageFile imageFile) where T : IContent
        {
            var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
            var content = contentRepository.Get<T>(imageFile.ContentLink.ToReferenceWithoutVersion(), new LanguageSelector(imageFile.MasterLanguage.Name));
            return content;
        }
    }

My question is what shall I do with the existing images which are "ImageFile" type?

I tried to upload a new picture and it still ImageFile type, not LocalizedImageFile.

Or any other tips to set culture specific alt text on an image?

Thanks,

ChiChing


Viewing all articles
Browse latest Browse all 6894

Trending Articles