Copied to clipboard

Flag this post as spam?

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


  • Fredrik 4 posts 116 karma points
    3 days ago
    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 4 posts 116 karma points
    3 days ago
    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 6168 posts 24148 karma points MVP 8x admin c-trib
    3 days ago
    Alex Skrypnyk
    0

    Thanks for sharing this!

Please Sign in or register to post replies

Write your reply to:

Draft