Copied to clipboard

Flag this post as spam?

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


  • Fredrik 6 posts 138 karma points
    Nov 29, 2024 @ 07:49
    Fredrik
    0

    Swedish characters in url

    Some of the links in my Umbraco site have swedish characters in the url. I wonder if there is an easy way to control how these characters show up in the links? For now ö -> oe, å -> aa and ä -> ae. I would like ö -> o, å -> a, ä -> a.

    I can use Replace to change the url:s but then the links won´t work. Is it possible to do some setting in Umbraco to get this to work?

  • Fredrik 6 posts 138 karma points
    Nov 29, 2024 @ 11:06
    Fredrik
    102

    I solved this problem with a CustomUrlSegmentProvider.

    using Umbraco.Cms.Core.Models;
    

    using Umbraco.Cms.Core.Strings;

    namespace RoutingDocs.SegmentProviders;

    public class CustomUrlSegmentProvider : IUrlSegmentProvider { private readonly IUrlSegmentProvider _provider;

    public CustomUrlSegmentProvider(IShortStringHelper stringHelper)
    {
        _provider = new DefaultUrlSegmentProvider(stringHelper);
    }
    
    public string? GetUrlSegment(IContentBase content, string? culture = null)
    {
        var updatedName = content.Name?
         .Replace("ö", "o")
         .Replace("å", "a")
         .Replace("ä", "a")
         .Replace("Ö", "O")
         .Replace("Å", "A")
         .Replace("Ä", "A");
    
        var segment = _provider.GetUrlSegment(content, culture);
    
        return segment?.ToLowerInvariant();
    }
    

    }


    using Umbraco.Cms.Core.Composing;
    

    namespace RoutingDocs.SegmentProviders; public class CustomUrlSegmentComposer : IComposer { public void Compose(IUmbracoBuilder builder) { builder.UrlSegmentProviders().Insert


    In Program.cs:

    builder.Services.AddSingleton<IComposer, RoutingDocs.SegmentProviders.CustomUrlSegmentComposer>();
    

    Remember to refresh the Umbraco-cache and re-publish all content.

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Nov 29, 2024 @ 11:56
    Alex Skrypnyk
    0

    Thanks for sharing this!

  • 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