SaaS CMS has officially launched! Learn more now.

Programatically Adding [CultureSpecific] Attribute Wipes Away Content Value

Vote:
 

Hi all,

I am working with a custom block that has a URL Link property under it. The site I am working with is in both English and Spanish. Currently the link is editable in English but not in Spanish as there is no [CultureSpecific] attribute applied to the property. The Spanish value defaults to where the English is set to.

I am trying to make the Spanish link editable, but adding the [CultureSpecific] attribute wipes away whatever value is in the Spanish site. Is there a way to make it translatable, and have all of the links become auto translated on change? There are a lot of occurences of this link type in Spanish, and having to manually reset them all would be more overhead than a programmatic solution.

Thanks,

Calen

#323692
Jun 17, 2024 15:57
Vote:
 

As I know there is not available tool to do like that. Could have tool to translate whole page into anther language https://docs.developers.optimizely.com/content-management-system/docs/optimizely-languages .

But I do not see any available thing like translating only from specific property to property between existed content languages like that.

I suggest that you can make a tool by your own to do your business such as loading all pages of that page type and update this property value in Spanish by that property value in English if it is empty

#323693
Jun 17, 2024 16:54
Vote:
 

Hi Calen,

One way you could approach this would be to implement a fallback mechanism so that, if there's no Spanish version of a given property value, it will use the English version. You could modify your model so your property looks like this:

[Display(
    Name = "My URL",
    GroupName = SystemTabNames.Content,
    Order = 500)]
[CultureSpecific]
public virtual Url MyUrl {
    get
    {
        var url = this.GetPropertyValue(p => p.MyUrl);

        //If a value has been set, or we're on the master language branch, return the value
        if (IsMasterLanguageBranch || url is not null)
        {
            return url;
        }

        //Get the value from the master language
        return this.GetFallbackValue<Url>("MyUrl");
    }
    set => this.SetPropertyValue(p => p.MyUrl, value);
}

Where the GetFallbackValue extension method looks like this:

public static class LanguageFallbackExtensions
{
    public static T GetFallbackValue<T>(this IContent content, string propertyName)
    {
        var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
        var masterContent = contentLoader.Get<IContent>(content.ContentLink.ToReferenceWithoutVersion(), (content as ILocalizable).MasterLanguage);
        return masterContent.TryGetPropertyValue<T>(propertyName);
    }
}

This won't modify the content in the CMS but should allow you to make the property editable in Spanish without breaking anything if it's not localised.

#323748
Jun 18, 2024 13:15
Calen Sullivan - Jun 19, 2024 13:05
Hi Paul,

I do see how this makes it editable via the [CultureSpecific] attribute. Per your last comment of it not modifying the content in the CMS, that is what I feel I am trying to accomplish when I do make it translatable. Are we able to programmatically able to set the Spanish content links to the Spanish pages, based on the value of the English link? I would bet the Get/Set properties would still be applicable.

My experience with custom EPiServer material like this is limited so I appreciate your replies.

Thanks,
Calen
Paul Gruffydd - Jun 20, 2024 8:55
If the Spanish links are simply the Spanish language branch of the English content then the mechanism I've described should already point to the Spanish version if that's available (unless you've selected a specific language when setting the link). If you definitely need to set the content in the CMS, I'd suggest creating a scheduled job to go through page-by-page and copy the value across from the English version to the Spanish one. You'll probably also want to implement the fallback mechanism as I've described so that nothing breaks between deploying the changes and running the job.
* 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.