How to get value of specific key from resource file in ASPNET Core?

旧城冷巷雨未停 提交于 2020-02-06 07:44:31

问题


I wanna get value of a key in resource (.resx) file in .net core API. I followed the marked answer and implemented in my code. But When I am fetching value by key then only Key name is coming.

The linked is below which I followed :

How to get the .resx file strings in asp.net core

Please help me for this.

Thanks


回答1:


Here is a working example , you could refer to :

Startup.cs

public void ConfigureServices(IServiceCollection services)
    {
        var connection = @"Server=(localdb)\mssqllocaldb;Database=WebAPIDbContext;Trusted_Connection=True;ConnectRetryCount=0";
        services.AddDbContext<RestaurantContext>(options => options.UseSqlServer(connection));

        services.AddLocalization(o => o.ResourcesPath = "Resources");

        services.AddMvc()   
            .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
            .AddDataAnnotationsLocalization()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        //Request Localization
        //var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
        //app.UseRequestLocalization(options.Value);

        var supportedCultures = new[]
       {
            new CultureInfo("en-US"),
            new CultureInfo("zh-cn"),
        };

        app.UseRequestLocalization(new RequestLocalizationOptions
        {
            DefaultRequestCulture = new RequestCulture("en-US"),
            // Formatting numbers, dates, etc.
            SupportedCultures = supportedCultures,
            // UI strings that we have localized.
            SupportedUICultures = supportedCultures
        });

        app.UseHttpsRedirection();
        app.UseMvc();
    }

Controller

private readonly IStringLocalizer<SharedResource> _sharedLocalizer;
    public ValuesController(IStringLocalizer<SharedResource> sharedLocalizer)
    {
        _sharedLocalizer = sharedLocalizer;
    }

    // GET api/values/5
    [HttpGet("{id}")]
    public ActionResult<string> Get(int id)
    {
        var value = _sharedLocalizer["Title"];
        return "value";
    }

Resource file path

Note : the namespace of SharedResource.cs should be the root of application

namespace RestaurantReviewApi
{ 
  public class SharedResource
  {
  }
}

Request url : https://localhost:44318/api/values/3?culture=zh-cn

The screenshot of result :

Reference : https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.2



来源:https://stackoverflow.com/questions/57917490/how-to-get-value-of-specific-key-from-resource-file-in-aspnet-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!