TASVideos / tasvideos

The code for the live TASVideos website
https://tasvideos.org/
GNU General Public License v3.0
63 stars 29 forks source link

implement custom localization #1784

Closed Masterjun3 closed 4 months ago

Masterjun3 commented 4 months ago

This PR needs to be checked properly.

Resolves #908 .

It implements 3 user settings to customize the date format, the time format, and the decimal number format (see images below).

Implementation Details

I build a custom CultureInfo, and then, in every single request overwrite CultureInfo.CurrentCulture as soon as possible. By using Middleware.

Middleware Location

Where to place the Middleware? We actually use the UseRequestLocalization() middleware to let .NET select the best culture initially. So I thought immediately afterwards would be a good place to place my overwrite middleware. But it turns out I need the User object, so UseAuthentication has to run first. That's why I placed it directly after that.

Caching

Caching is critical. We don't want to read the DB on every request just for the locale. The first idea was to just cache each user's CultureInfo. This alone would work, but there are two Problems:

  1. On every restart the cache is cleared, and each user will do one additional DB call to get their CultureInfo build, again and again.
  2. Most users don't use custom cultures, so the cache will be filled with non-modified objects.

That's why I decided to use a second cache. This second cache caches the HashSet of the users that actually use custom locales.

The HashSet cache and the user's own CultureInfo cache is cleared when they change their Profile Settings.

Hot Path


image

vadosnaprimer commented 4 months ago

I tested with a few language settings in the browser and it works well with obeying and overriding them. I'd only wish the settings to be near Time Zone settings in the GUI and not between avatar and signature.