Copied to clipboard

Flag this post as spam?

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


  • jtemperv 6 posts 26 karma points
    Jan 29, 2020 @ 17:21
    jtemperv
    0

    How to create a language switcher with DocTypeGrid editor and UmbracoViewPage<IPublishedElement>

    I'm quite new to Umbraco and want to implement a language switcher with a component approach. Therefor I used DocTypeGrid editor, and a doc type that is generated by the modelbuilder as being of type PublishedElement.

    Now I want to render a language switcher. On the guides that I could, find people instruct to use the Model.Url(...) method. Unfortunatly that is not available on my Model in my cshtml with @inherits UmbracoViewPage<IPublishedElement>.

    How would I be able to retrieve the url of the current content item in a specific culture? Can I access the content page that contains the component?

  • jtemperv 6 posts 26 karma points
    Jan 29, 2020 @ 17:57
    jtemperv
    0

    OK, typical ... hours of searching, nothing, 5 minutes after posting my question, I found a possible solution. Would you think this is correct?

    var languageService = Current.Services.LocalizationService;
    var assignedContentItem = Umbraco.AssignedContentItem;
    @foreach (var language in languageService.GetAllLanguages())
    {
    assignedContentItem.Url(language.CultureInfo.TwoLetterISOLanguageName)                }
    
  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Jan 29, 2020 @ 23:07
    Alex Skrypnyk
    0

    Hi

    I would say that the services are not a best way to use in page load. They are quite slow.

    Try to use UmbracoHelper.

    Thanks, Alex

  • jtemperv 6 posts 26 karma points
    Jan 30, 2020 @ 06:47
    jtemperv
    0

    Hi Alex,

    thank you for your advice. How would I be able to access the languages in my example using the UmbracoHelper? Can you give me an example please?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies