Pick up locale requested by the client
As a developer of a localizable web application, I would like my app to pick up the locale requested by the client, so that generated content can get localized accordingly.
Example
var localization = Localization.FromHeader() // default
.FromQuery("lang") // FromCookie(), From...()
.FromRequest(r => ...)
.Set(culture: true, uiCulture: true)
.Supports(culture => true) // allow to negoiate supported cultures and ignore non-supported ones
.Default(CultureInfo.GetCultureInfo("hi-IN"));
Acceptance criteria
- The functionality is implemented using a concern
- The functionality is either implemented in the basics or in a new module (such as
i18n) - The supported locales can be read from various places, such as query parameters or in a custom manner
- The concern can be configured to set the .NET properties for the current culture
- The concern can be configured to set a request property (so we do not need to rely on the .NET ones)
- The concern allows the app to negoiate the locale to be used by filtering the requested ones
- A default culture can be set (e.g. if none is passed by the client)
- The functionality is covered by acceptance tests
FYI when using docker to host the app and especially when using alpine linux, be aware that application is by default forced to run in globalization invariant mode, which causes any attempt to change the current culture in runtime to throw an expection. Furthermore the alpine base image does not contain ICU (International Components for Unicode) which is need for .net globalization to work.
The topic is described in detail on MS docker github and MS documentation.
Solutions (2 options):
-
Use different base image that contains ICU and does not disable globalization.
-
Force disable invariant globalization in .csproj file
<InvariantGlobalization>false</InvariantGlobalization>. Set following ENV variable in docker fileENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false(for some reason setting only one of the ENV or csproj option did not worked for me, had to set both). Install icu libs on top of alpine base image:
RUN apk add --no-cache \
icu-data-full \
icu-libs