1

I would like to get view localization based on the url instead of browser culture.

"/account/leadregistrationstep/de" Will display the view without translation. To get the translation the browser culture must match with the view (this works).

Is there a way to get the translation without having the browser culture need to match the resource file? I want the german view to display only german language

enter image description here

Startup ConfigureServices

            services.Configure<RequestLocalizationOptions>(
            options =>
            {
                var supportedCultures = new List<CultureInfo>
                {
                    new CultureInfo("en"),
                    new CultureInfo("de"),
                    new CultureInfo("nl"),
                    new CultureInfo("fr")
                };

                options.DefaultRequestCulture = new RequestCulture(culture: "nl", uiCulture: "nl");
                options.SupportedCultures = supportedCultures;
                options.SupportedUICultures = supportedCultures;

                options.RequestCultureProviders.Insert(0, new QueryStringRequestCultureProvider());
            });

Configure

            app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
            endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{language}/{id?}");
        });

1 Answer 1

2

There are three default RequestCultureProviders, among them is: AcceptLanguageHeaderRequestCultureProvider which use the browser headers for the culture info. You need to overwrite the list instead of inserting the QueryStringRequestCultureProvider into it, thus removing the AcceptLanguageHeaderRequestCultureProvider.

options =>
    {
        var supportedCultures = new List<CultureInfo>
        {
            new CultureInfo("en"),
            new CultureInfo("de"),
            new CultureInfo("nl"),
            new CultureInfo("fr")
        };

        options.DefaultRequestCulture = new RequestCulture(culture: "nl", uiCulture: "nl");
        options.SupportedCultures = supportedCultures;
        options.SupportedUICultures = supportedCultures;

        options.RequestCultureProviders = new List<IRequestCultureProvider>
        {
            new QueryStringRequestCultureProvider(),
            new CookieRequestCultureProvider()
        };
    }

That will use only a set cookie, or the querystring ?culture=fr ... to select the culture.

However if you need custom rules, you need to implement your own RequestCultureProvider. This is an example:

public class UrlRequestCultureProvider : RequestCultureProvider
{
    public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
    {
        var validCultures = new []{"en", "de", "fr", "nl"};
        if (httpContext == null)
        {
            throw new ArgumentNullException(nameof(httpContext));
        }

        var culture = httpContext.Request.Path.Value
                        .Split("/")
                        .FirstOrDefault( (p) => validCultures.IndexOf(p.ToLower()) >= 0);

        if (culture == null)
        {
            return Task.FromResult((ProviderCultureResult)null);
        }

        return Task.FromResult(new ProviderCultureResult(culture));
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

What is the correct way to do this ? Do i need a specific class that deals with this ?
If you need custom rules, then you need to implement your own RequestCultureProvider, but if you only want to use the QueryStringRequestCultureProvider - then the code I posted should suffice.
?culture=fr ... is the querystring way
Oh my bad. That worked with my code as well.. I'm looking for a solution where i dont need to pass the culture. The culture should be the language of the view so /de should only display german etc..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.