Globalization/localization issue for multi language support

19 views Asked by At

I am trying to add globalization/localization for multiple language support for my dotnet core application but it's not working, I am currently using dotnet core 8.0.

I have done few methods using the article available on the internet and official Microsoft website, but I am unable to do so.

I have tried setting various culture info, and made a middleware so that it will extract language option, and set culture based on that below is my code.

public class LanguageMiddleware : IMiddleware
  {
      public async Task InvokeAsync(HttpContext context, RequestDelegate next)
      {
          // Extract language from headers
          string language = context.Request.Headers["Accept-Language"];

      

    // Set the culture based on the language
          CultureInfo culture = language.ToLowerInvariant() switch
          {
              var lang when lang.StartsWith("eng") => new CultureInfo("en"),
              var lang when lang.StartsWith("guj") => new CultureInfo("gu"),
              _ => new CultureInfo("en"),// Default to English
          };

          // Set the current culture and UI culture
          System.Threading.Thread.CurrentThread.CurrentCulture = culture;
          System.Threading.Thread.CurrentThread.CurrentUICulture = culture;

          // Continue processing the request
          await next(context);
      }
  }

midleware is working fine, but culture info is not working as excepted, and always giving errors in English everytime.

0

There are 0 answers