【问题标题】:How to get value of specific key from resource file in ASPNET Core?如何从 ASPNET Core 的资源文件中获取特定键的值?
【发布时间】:2019-09-13 04:56:34
【问题描述】:

我想在 .net 核心 API 中获取资源 (.resx) 文件中的键值。我遵循标记的答案并在我的代码中实现。但是当我通过键获取值时,只会出现键名。

下面是我关注的链接:

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

请帮帮我。

谢谢

【问题讨论】:

  • 你试过了吗? devtrends.co.uk/blog/…
  • @TAHASULTANTEMURI ,我也检查了它,但我在.Net Core 中工作,我需要调用任何单个键来获取值。

标签: asp.net-core-webapi


【解决方案1】:

这是一个工作示例,您可以参考:

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();
    }

控制器

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";
    }

资源文件路径

注意:SharedResource.cs 的命名空间应该是应用的根目录

namespace RestaurantReviewApi
{ 
  public class SharedResource
  {
  }
}

请求地址:https://localhost:44318/api/values/3?culture=zh-cn

结果截图:

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

【讨论】:

  • 感谢您的回答,但您没有检查我在问题中添加的附加链接。我也在做同样的事情。但是该语句仅返回密钥: string title=_sharedLocalizer["Title"];然后标题只有“标题”。这意味着我给出的键不是“标题”键的值。
  • @Deepak,我成功重现问题并解决了。请注意这两点。首先,必须特别支持CultureInfo() 中的culture name。二、当你想获取某个语言中特定key的值时,需要在请求中设置对应的语言,最简单的就是使用查询字符串(查看我编辑的),其他两种方式为CookieRequestCultureProviderAcceptLanguageHeaderRequestCultureProvider,您可以参考我回复中提供的链接了解更多详情。
猜你喜欢
  • 2015-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
相关资源
最近更新 更多