1

I'm using dotnet core and running a couple os tests on customized string extensions inside a docker container and it fail always, but it works well on a windows machine.

i've tryed to enforce the portuguese culture but it does not work, what am i missing here?

As example, i'm trying to remove diacritics from a string using as follows:

public static string RemoveDiacritics(this string input)
{
    var normalizedString = input.Normalize(NormalizationForm.FormD);
    var stringBuilder = new StringBuilder();

    foreach (var c in normalizedString)
    {
        var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
        if (unicodeCategory != UnicodeCategory.NonSpacingMark)
        {
            stringBuilder.Append(c);
        }
    }

    return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
}

Here is the test example (NUnit)

        [Test]
        public void StringExtensions_RemoveDiacritics_SUCCESS()
        {
            string originalStr = "amahã deverá ser çábado";
            string cleanStr = originalStr.RemoveDiacritics();
            Assert.AreEqual("amaha devera ser cabado", cleanStr);
        }

Here is the dockerfile example:

FROM mcr.microsoft.com/dotnet/sdk:3.1-focal
WORKDIR /

ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
ENV TZ Europe/Lisbon

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN dpkg-reconfigure --frontend noninteractive tzdata

For better undertanding how to reproduce, i've created the following repo: https://github.com/FlavioCFOliveira/TestsOnStrings

Flavio CF Oliveira
  • 5,235
  • 13
  • 40
  • 63

1 Answers1

0

Take a look at the behaviour described here: https://github.com/dotnet/dotnet-docker/issues/1360

Basically the base .NET images run in a globalization invariant mode by default. You have to manually disable it by doing the following in your Dockerfile:

ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
RUN apk add --no-cache icu-libs

Ran into this issue myself when a .ToLower() of an "Ë" character didn't correctly transform it into "ë": it remained "Ë". Doing these Dockerfile modifications fixed it for my case.

Fluppe
  • 479
  • 7
  • 22