Try our conversational search powered by Generative AI!

How to set culture specific alt text on an image

Vote:
 

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

#200180
Jan 04, 2019 10:24
Vote:
 

Hi ChiChing,

You're going to have to do some kind of migration here.

Because they are so similar, I think the easiest way to do it is via SQL. Messing around with it a little, the following seems to work:

DECLARE @LanguageId int;
DECLARE @OldContentTypeId int;
DECLARE @NewContentTypeId int;
DECLARE @OldPropertyDefinitionId int;
DECLARE @NewPropertyDefinitionId int;

BEGIN
  SET @LanguageId = (SELECT TOP 1 pkID FROM dbo.tblLanguageBranch WHERE LanguageID = 'en')
  SET @OldContentTypeId = (SELECT TOP 1 pkID FROM dbo.tblContentType WHERE ModelType LIKE '%.ImageFile%')
  SET @NewContentTypeId = (SELECT TOP 1 pkID FROM dbo.tblContentType WHERE ModelType LIKE '%.LocalizedImageFile%')
  SET @OldPropertyDefinitionId = (SELECT TOP 1 pkID FROM dbo.tblPropertyDefinition WHERE fkContentTypeID = @OldContentTypeId AND Name = 'AlternateText')
  SET @NewPropertyDefinitionId = (SELECT TOP 1 pkID FROM dbo.tblPropertyDefinition WHERE fkContentTypeID = @NewContentTypeId AND Name = 'AlternateText')
END

PRINT CONCAT('Language ID.................. ', @LanguageId)
PRINT CONCAT('Prev Content Type ID......... ', @OldContentTypeId)
PRINT CONCAT('New Content Type ID.......... ', @NewContentTypeId)
PRINT CONCAT('Prev Property Definition ID.. ', @OldPropertyDefinitionId)
PRINT CONCAT('New Property Definition ID... ', @NewPropertyDefinitionId)

UPDATE
  dbo.tblWorkContent
SET
  fkLanguageBranchID = @LanguageId
WHERE
  fkContentID IN (SELECT pkID FROM dbo.tblContent WHERE fkContentTypeID = @OldContentTypeId)

UPDATE
  dbo.tblContentLanguage
SET
  fkLanguageBranchID = @LanguageId
WHERE
  fkContentID IN (SELECT pkID FROM dbo.tblContent WHERE fkContentTypeID = @OldContentTypeId)

UPDATE
  dbo.tblContentProperty
SET
  fkPropertyDefinitionID = @NewPropertyDefinitionId,
  fkLanguageBranchID = @LanguageId
WHERE
  fkPropertyDefinitionID = @OldPropertyDefinitionId

UPDATE
  dbo.tblWorkContentProperty
SET
  fkPropertyDefinitionID = @NewPropertyDefinitionId
WHERE
  fkPropertyDefinitionID = @OldPropertyDefinitionId

UPDATE
  dbo.tblContent
SET
  fkMasterLanguageBranchID = @LanguageId,
  fkContentTypeID = @NewContentTypeId
WHERE
  fkContentTypeID = @OldContentTypeId

You'll want to set the "en" near the top to whatever the master website language is (and whatever language your alt texts are in), there are a few other things there you may want to tweak based on your requirements.

Of course, no guarantees - but I'm sure you were gonna back up the database anyway 😊

#200204
Edited, Jan 04, 2019 17:41
Vote:
 

Thanks for this script Jake. It's really helpful

#230002
Oct 28, 2020 13:21
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.