Humanizr / Humanizer

Humanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities
Other
8.63k stars 959 forks source link

culture parameter being ignored in razor component (Blazor WebAssembly) #1089

Open cyl1d3 opened 3 years ago

cyl1d3 commented 3 years ago

I'm currently having the following issue:

I'm using Humanizer in a razor component running in a WebAssembly Blazor project. My component has a class Localizer which provides a CultureInfo. When passing a DateTime through the Humanizer, I always get the English result. Here's the code snippet:

<small class="pr-2">
    <abbr title="@item.Date.ToString(Localizer.GetCulture())">
        @item.Date.Humanize(culture: Localizer.GetCulture())
    </abbr>
</small>

The Localizer is not the problem, as the Date format in the tooltip is correctly changed when I change the culture:

Culture en-US: image

Culture de-DE: image

Some additional information: The same issue happens with TimeSpan, but non time related functions seem unaffected e.g. 5.ToWords(culture: Localizer.GetCulture()) works just as expected.

Upon further analysis, the problem seems to occur while loading the resource string: image

The culture has been correctly passed down, but loading the resource always returns the english string.

Velociapcior commented 3 years ago

hello @cyl1d3 I had the same problem, I solved it by adding boolean 'false' value before culture, as this parameter corresponds to bool? utcDate = null (from decompiling dll). It solved my issue, you could give it a try. I hope it will help you

Edit: It looks like this in my code: <p class="small text-muted ">@Date.Humanize(false, culture: CultureInfo.CurrentUICulture)</p>

cyl1d3 commented 3 years ago

@Velociapcior Thanks for the hint! Sadly, this did not work for me.

zuverza commented 3 years ago

@cyl1d3 I don't think that feature is implemented yet. I was looking at DateToOrdinalWordsConverterRegistry.cs and it uses the DefaultDateToOrdinalWordConverter() for 'de'.

riccardominato commented 1 year ago

I'm having exactly the same issue here.

I'm trying to humanize a TimeSpan.

Calling the same method with same parameters inside an Asp.Net Core (backend) project works perfectly, while in a Blazor project the Spanish language doesn't work and it shows the result in english. Oddly, Italian (my native language) works.

As for @cyl1d3, if I try something not related to TimeSpans it works in Spanish, too.

This is the instruction I'm trying to perform.

TimeSpan.FromMinutes(20).Humanize(culture: new CultureInfo("es"))

Using es-ES doesn't change the result.

Humanize package is installed in my project, so I should have all languages available.

@cyl1d3 I know it's been a long time since you opened this issue, but did you find a workaround?

Thank you.

cyl1d3 commented 1 year ago

@riccardominato sadly not. I've since left the project I wanted to use this in and haven't further tried to find a workaround, sorry!

riccardominato commented 1 year ago

I found out what was the problem! It seems that Humanizer in Blazor only supports English and the browser current language.

Since in my case it was set to Italian, it worked with Italian and English. I tried setting my browser language to Spanish and magically now it works for Spanish humanization (but of course not for Italian anymore).