Привет, я пытаюсь реализовать локализацию для ASP.NET Core, но я не могу вернуть переведенное значение - оно всегда возвращает значение «ключа» - «Загрузить». Когда я печатаю культуру - она возвращает «fr», значит, культура установлена правильно. Честно говоря, у меня нет идей ...
Я следую этому руководству: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization
Смотрим и на этот образец: https://github.com/aspnet/Entropy/tree/dev/samples/Localization.StarterWeb
Когда я создаю проект, файл ресурсов компилируется и копируется по адресу: obj / Debug / netcoreapp1.1 / BoilerPlate.Resources.Controllers.ValuesController.fr.resources
и
bin / Debug / netcoreapp1.1 / fr / BoilerPlate.resources.dll
$ dotnet --info
.NET Command Line Tools (1.0.0-rc3-004530)
Product Information:
Version: 1.0.0-rc3-004530
Commit SHA-1 hash: 0de3338607
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("fr"),
};
// State what the default culture for your application is. This will be used if no specific culture
// can be determined for a given request.
options.DefaultRequestCulture = new RequestCulture(culture: "fr", uiCulture: "fr");
// You must explicitly state which cultures your application supports.
// These are the cultures the app supports for formatting numbers, dates, etc.
options.SupportedCultures = supportedCultures;
// These are the cultures the app supports for UI strings, i.e. we have localized resources for.
options.SupportedUICultures = supportedCultures;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseStatusCodePages();
app.UseDeveloperExceptionPage();
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
app.UseMvc();
}
ValuesController.cs
private readonly IStringLocalizer<ValuesController> _localizer;
public ValuesController(IStringLocalizer<ValuesController> localizer)
{
_localizer = localizer;
}
// GET api/values/5
[HttpGet("{id}")]
public string Get(int id)
{
var rqf = Request.HttpContext.Features.Get<IRequestCultureFeature>();
var culture = rqf.RequestCulture.Culture;
System.Console.WriteLine($"Culture: {culture}");
return _localizer["Load"];
// return "value";
}
Ресурсы / Controllers.ValuesController.fr.resx
...
<data name="Load" xml:space="preserve">
<value>Load this value!</value>
</data>
...