ZIADIA Oussama
ZIADIA Oussama

Reputation: 149

Asp.Net Core 2.1 Localization

i'm new to asp.net core trying to implement localization to support multiple language , my configuration is like this

 services.AddLocalization(options => options.ResourcesPath = "Resources");
        services.Configure<RequestLocalizationOptions>(options =>
        {
            var supportedCultures = new[]
            {
                new CultureInfo("en-US"),
                new CultureInfo("fr-FR")
            };
            options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
            options.SupportedCultures = supportedCultures;
            options.SupportedUICultures = supportedCultures;

        });
        services.AddMvc().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix,opts=> {opts.ResourcesPath="Resources"; })
            .AddDataAnnotationsLocalization(o=> {
            o.DataAnnotationLocalizerProvider = (type, factory) =>
            {
                return factory.Create(typeof(SharedResource));
            };
        });

this is the standard configuration and its working fine , i create a method inside a controller

 [Route("api/setlanguage")]
    [HttpPost]
    public IActionResult SetLanguage (string culture)
    {

        Response.Cookies.Append(
            CookieRequestCultureProvider.DefaultCookieName,
            CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
            new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) });
        throw new Exception(_localizer["Hello",culture]);


    }

when i test with Postman like this : http://localhost:31563/api/SetLanguage?culture=en-US i'm getting a correct result but when i try to pass culture inside the body of request its not working , can anyone help me on this , thanks so much

Upvotes: 2

Views: 1732

Answers (2)

ZIADIA Oussama
ZIADIA Oussama

Reputation: 149

to make this work i just need to add

  System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
  System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);

to force my currentThread.CurrentCulture/UICulture to take the culture value that i'm passing from body

 public IActionResult SetLanguage([FromBody] string culture)
     {          

        System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
        System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);

       _localizer.WithCulture(new CultureInfo(culture));

    }

Upvotes: 1

Edward
Edward

Reputation: 29976

By default, Web API parameter will get value from query string if you use like (string culture).

If you want to get value from Body, you could add [FromBody] like below:

    public IActionResult SetLanguage([FromBody]string culture)

And then post your request like below:

enter image description here

Obviously, it will not work with Query String request if you add [FromBody].

Upvotes: 0

Related Questions