'Can the UseRequestLocalization from the RequestLocalizationMiddleware be used to set the CultureInfo in asp.net-core?

I am trying to use the RequestLocalizationMiddleware to access the culture which was requested by the frontend. I have added the UseRequestLocalization option in my startup and it works fine for resx files. But instead of calling the localizer I want to access the culture which was requested, to use it as a partition key for my NoSql database. Is there a way to tell the RequestLocalizationMiddleware to apply the culture as the CurrentCulture in CultureInfo? Or is there another way to access the culture somewhere else than through the localizer?



Solution 1:[1]

You can use something like this:

    public static void AddMultilanguagesConfig(this IServiceCollection services)
    {
        if (services == null)
        {
            throw new ArgumentNullException(nameof(services));
        }

        services.AddLocalization();

        services.Configure<RequestLocalizationOptions>(
           options =>
           {
               List<CultureInfo> supportedCultures = new List<CultureInfo>();
               PAAConstants.SYSTEM_LANGUAGES.ToList().ForEach(x => supportedCultures.Add(new CultureInfo(x)));
               options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
               options.SupportedCultures = supportedCultures;
               options.SupportedUICultures = supportedCultures;
               options.RequestCultureProviders = new[] { new RouteDataRequestCultureProvider { IndexOfCulture = 1, IndexofUICulture = 1 } };
           });

        services.Configure<RouteOptions>(options =>
        {
            options.ConstraintMap.Add("culture", typeof(LanguageRouteConstraint));
        });
    }

and the SYSTEM_LANGUAGES is a list like this:

public static readonly string[] SYSTEM_LANGUAGES = { "en-US" };

and a controller is defined as:

[Produces("application/json")]
[Route("{culture:culture}/manager/clients")]
[ApiController]
[Authorize]
public class ClientsController : Controller
{        
}

and in the Controller method you can read the culture like this:

var rqf = Request.HttpContext.Features.Get<IRequestCultureFeature>();
var name =  rqf.RequestCulture.Culture.Name;

Solution 2:[2]

The model:

y ~ DENS:GEN:FUNG + (1 | trials) + (1 | trials:block)

has the following features:

  1. A fixed effect for the 3-way interaction DENS:GEN:FUNG,

  2. Random intercepts for block varying within levels of trials

It is very rarely a good idea to fit a 3-way interaction as a fixed effect without the 2-way interactions and the main effects. See these for further discussion:

https://stats.stackexchange.com/questions/236113/are-lower-order-interactions-a-prequisite-for-three-way-interactions-in-regressi

https://stats.stackexchange.com/questions/27724/do-all-interactions-terms-need-their-individual-terms-in-regression-model

As for the random structure, then yes, based on the description, this seems to be appropriate, although you don't state how many trials there are - if this is very few then it may be better to fit trials as a fixed effect.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 D A
Solution 2