Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing diacritics in Silverlight (String.Normalize issue)

I did create a function that transforms diacritic characters into non-diacritic characters (based on this post)

Here’s the code:

Public Function RemoveDiacritics(ByVal searchInString As String) As String
    Dim returnValue As String = ""

    Dim formD As String = searchInString.Normalize(System.Text.NormalizationForm.FormD)
    Dim unicodeCategory As System.Globalization.UnicodeCategory = Nothing
    Dim stringBuilder As New System.Text.StringBuilder()


    For formScan As Integer = 0 To formD.Length - 1
        unicodeCategory = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(formD(formScan))
        If unicodeCategory <> System.Globalization.UnicodeCategory.NonSpacingMark Then
            stringBuilder.Append(formD(formScan))
        End If
    Next

    returnValue = stringBuilder.ToString().Normalize(System.Text.NormalizationForm.FormC)

    Return returnValue

End Function

Unfortunately, as the String.Normlize isn’t part of Silverlight, I need to find an other way to write this function.

The only solution I have found so far is to create a service on the server side that would call the String.Normalize function and then return it to the client side… but that would create a huge performance issue.

There must be a better alternative but right know I have no clue on how to fix this problem.

like image 826
The_Black_Smurf Avatar asked Nov 05 '22 07:11

The_Black_Smurf


1 Answers

Simon,

Here is a basic implementation of Normalize(), calling into a Normalization class:

public string Normalize ()
{
    return Normalization.Normalize (this, 0);
}

public string Normalize (NormalizationForm normalizationForm)
{
    switch (normalizationForm)
    {
        default:
            return Normalization.Normalize (this, 0);
        case NormalizationForm.FormD:
            return Normalization.Normalize (this, 1);
        case NormalizationForm.FormKC:
            return Normalization.Normalize (this, 2);
        case NormalizationForm.FormKD:
            return Normalization.Normalize (this, 3);
    }
}

And you can browse an implementation of the Normalization class from the Mono project on GitHub:

http://github.com/mono/mono/blob/mono-2.6.4/mcs/class/corlib/Mono.Globalization.Unicode/Normalization.cs

Good luck,
Jim McCurdy

like image 172
Jim McCurdy Avatar answered Nov 09 '22 16:11

Jim McCurdy