Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sam Marshallsay 35 posts 135 karma points
    Feb 14, 2022 @ 13:44
    Sam Marshallsay
    0

    How to return dictionary items in the correct language?

    Hello! Thanks for this great package!

    I am attempting to use the data list editor to let the user pick from a checkbox list of localised dictionary terms. In the back-end it still shows the names in English even though I have them set up for another language (Polish) as well. When I pull the items through onto the page it still shows in English!

    Thanks

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Feb 14, 2022 @ 16:51
    Lee Kelleher
    0

    Hi Sam,

    It turns out that the Umbraco Dictionary Items data-source hasn't been working as intended. It (currently) only shows the labels of whichever language has been set to default.

    I've been investigating with the code since you posted, and I can get the labels displaying in the other languages... but due to how the UI of switching between languages/cultures works, I've found that it's not always playing nicely with the Data List editor. e.g. sometimes it'll show the wrong language - which would be bad.

    I'll have a think of how it could work, but can't make any promises to have a solution/workaround for it.

    On the off chance that anyone else has any ideas - I'm open to suggestions - the source-code for the Dictionary data-source is here: https://github.com/leekelleher/umbraco-contentment/blob/develop/src/Umbraco.Community.Contentment/DataEditors/DataList/DataSources/UmbracoDictionaryDataListSource.cs#L75-L86

    Thanks,
    - Lee

  • Sam Marshallsay 35 posts 135 karma points
    Feb 14, 2022 @ 17:10
    Sam Marshallsay
    0

    Hi Lee, Thanks for getting back to me. I did pull the code and have a tinker, unfortunately I'm not great with the backoffice stuff. I couldn't for the life of me figure out how to get the current language selected for the tab

    I was messing around something like the below (UmbracoDictionaryDatalistSource.cs) but I think I may have been barking up the wrong tree. Either way I never got it to work. I'm not even sure if VariationContextAccessor is the right thing to be trying, documentation seems a bit sparse.

    //var cultureName = CultureInfo.CurrentCulture.Name;
    //var cultureName = _variationContextAccessor.VariationContext.Culture;
    var vca = Current.Factory.GetInstance<IVariationContextAccessor>();
    //var vca = Current.VariationContextAccessor;
    var cultureName = vca.VariationContext.Culture;
    
    return _localizationService
        .GetDictionaryItemChildren(parent.Key)
        .OrderBy(x => x.ItemKey)
        .Select(x => new DataListItem
        {
            Name = x.Translations.FirstOrDefault(t => t.Language.IsoCode.InvariantEquals(cultureName) == true || t.Language.IsDefault == true)?.Value ?? x.ItemKey,
            Value = x.Translations.FirstOrDefault(t => t.Language.IsoCode.InvariantEquals(cultureName) == true || t.Language.IsDefault == true)?.Value ?? x.ItemKey,
            Icon = this.Icon,
            Description = x.ItemKey
        });
    

    I guess alternatively you could add a language dropdown to the UI so you can choose which language you pull the dictionary items from?

    I did a workaround in the end and just did a dictionary lookup on the page using the English key value (our dictionary key names are the same as their English value)

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Feb 14, 2022 @ 17:28
    Lee Kelleher
    0

    Thanks Sam. It was the VariationContext I was exploring with too, like you say it didn't work (as I'd expect it to work).

    I had more success using the HttpContext, e.g.

    _httpContextAccessor.HttpContext.Request.ClientCulture()
    

    The culture code is passed through as a HTTP header (named X-UMB-CULTURE). It'll have the culture of whatever the content-tree is using at that point, which is promising - but if the culture is then switched with the in-page navigation (in the node name textfield), then it doesn't update.

    I think the proper solution for this is for a dedicated property-editor that can populate the checkbox list values on the client-side, e.g. making an fetch/AJAX request with the correct culture code.

    The issue with Data List is that it gets the data-source items in the initial payload, it doesn't do any client-side fetch/requests. Meaning that it isn't aware of any UI changes client-side.

    Hope this makes sense? (or some of it at least? feels like I'm brain dumping)

    Cheers,
    - Lee

Please Sign in or register to post replies

Write your reply to:

Draft