Skip to content

Instantly share code, notes, and snippets.

@mikkelhm
Created June 1, 2017 12:01
Show Gist options
  • Save mikkelhm/85dc7c2d3b5cfd3114b733b3c76748d6 to your computer and use it in GitHub Desktop.
Save mikkelhm/85dc7c2d3b5cfd3114b733b3c76748d6 to your computer and use it in GitHub Desktop.
Umbraco Bypass ascii encoding if all fails
using System.Globalization;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Strings;
namespace Custom
{
public class CustomApplicationEventHandler : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
var shortStringHelper = new CustomShortStringHelper(UmbracoConfig.For.UmbracoSettings()).WithDefaultConfig();
ShortStringHelperResolver.Current.SetHelper(shortStringHelper);
base.ApplicationStarting(umbracoApplication, applicationContext);
}
}
}
using System.Globalization;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Strings;
namespace Custom
{
public class CustomShortStringHelper : DefaultShortStringHelper
{
public CustomShortStringHelper(IUmbracoSettingsSection umbracoSettings) : base(umbracoSettings) { }
public override string CleanStringForUrlSegment(string text)
{
var converted = this.CleanString(text, CleanStringType.UrlSegment);
if (string.IsNullOrWhiteSpace(converted))
{
return text;
}
return converted;
}
public override string CleanStringForUrlSegment(string text, CultureInfo culture)
{
var converted = this.CleanString(text, CleanStringType.UrlSegment, culture);
if (string.IsNullOrWhiteSpace(converted))
{
return text;
}
return converted;
}
}
}
@mikkelhm
Copy link
Author

mikkelhm commented Jun 1, 2017

Used if i.e. you need to have toAscii set to true, while some nodes might not be able to be translated into Ascii, like chinese.

This will simply not do any conversions on the strings, if they contain non ascii letters, i.e. the CleanString method returned empty, which it would if toAscii is on, and the node is in chinese.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment