Search code examples
c#.net-corelocalizationasp.net-core-webapi.net-5

How to add Language localization in .net 5 web API


I have tried to follow the steps of this tutorial to Apply Language localization at my API level. but what ever the HTTP request Header is, it alwase brings the default Language. this is the startup Code, in ConfigureServices method:

  #region Localiztion
        services.AddLocalization();
        services.AddRequestLocalization(x =>
        {
            x.DefaultRequestCulture = new RequestCulture("en");

            x.ApplyCurrentCultureToResponseHeaders = true;

            x.SupportedCultures = new List<CultureInfo> {
                new("ar"),
                new("en") 
            };

             x.SupportedUICultures = new List<CultureInfo> {
                new("ar"),
                new("en")
            };

        });
        #endregion

at Configure method :

  app.UseRequestLocalization();

the resources :

enter image description here

I have tried to add culture header as mentioned in the tutorial or adding Content-Language header as mentioned on Microsoft Docs but still, it only brings the default whatever the default is.


Solution

  • After A bit of research, the HTTP header that works with the localization was Accept-Language.