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:
On every restart the cache is cleared, and each user will do one additional DB call to get their CultureInfo build, again and again.
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
For logged out visitors: The Middleware checks whether they are logged in. They aren't, so it is skipped.
For non-custom-locale users: After confirming the login, it loads the cached HashSet of custom locale users, and they aren't in there. So it is skipped.
For custom-locale users: After loading the cached HashSet and finding the user, it loads the cached CultureInfo and overwrites the CurrentCulture. Then it is done.
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.
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, soUseAuthentication
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:
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