World is now on Opti ID! Learn more

Automatic soft hyphen ­ in property?

Vote:
 

Hi!

We have a block with a regular property:

public virtual string Title { get; set; }

If I have a really long title for example the made up word Allokeringsstrategi­sammanslutningsförbundet I think Optimizely automatic adds a soft hyphen.

The word is rendered like this: Allokeringsstrategi­­sammanslutningsförbundet

Is it Optimizely doing this or is there something else? If I debug the code the value coming back before rendering contains a soft hyphen.

Thanks!

/Kristoffer

#339392
Jun 13, 2025 8:31
Vote:
 

We usually handle this by instructing the editors to add three dashes where there's a possible line break.

E.g.

Allokerings---strategi­---sammanslutnings---förbundet 

public static string ReplaceDashesWithShy(this string text) {
    return string.IsNullOrEmpty(text) ? text : text.Replace("---", "­");
}

Add a shared displaytemplate in ~/Views/Shared/DisplayTemplates/String.cstml

@{
    var str = Model as string;
}
@if(str != null) {
    @( WebUtility.HtmlDecode(str.ReplaceDashesWithShy()) )
} else {
    @Html.DisplayFor(model => model)
}

That will handle any strings with ---. Or are you looking for some solution for xhtmlstrings as well?

#339394
Jun 13, 2025 11:08
Vote:
 

Regarding the automatic version of this you must use an external library, I've never used a c# version of this but found the Hyphenator.Net library 

E.g

using Hyphenator;

var hyphenator = new Hyphenator.Hyphenator("sv"); // "sv" for Swedish
string result = hyphenator.HyphenateText("avstavning");
Console.WriteLine(result);  // Outputs: av-stav-ning

I have used the Pyphen library in python and know that that works pretty well

import pyphen
dic = pyphen.Pyphen(lang='sv_SE')
print(dic.inserted('avstavning'))  # => av·stav·ning

If the Hyphenator.Net library doesn't work you can always wrap Pyphen in a container or so and call it during the publish event and parse your HTML that way.

#339408
Jun 13, 2025 22:12
Vote:
 

Found it! The editor had made copy/paste from the browser and you the get the soft hyphen included. Then when publishing the page it goes down to the database. So it was an editor issue. 

#339410
Jun 16, 2025 10:43
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.