ASP.NET 核心全球化和本地化 - 不加载值
ASP.NET Core Globalization and Localization - doesn't load the value
您好,我正在尝试为 ASP.NET Core 实现本地化,但我无法 return 翻译值 - 它总是 return "key" 值 - "Load".当我打印文化时 - 它 returns "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";
}
Resources/Controllers.ValuesController.fr.resx
...
<data name="Load" xml:space="preserve">
<value>Load this value!</value>
</data>
...
我认为问题出在与资源相关的文件夹结构上。您应该将资源存储在这个结构中:
资源 > 控制器 > YourController.fr.resx
资源 > 视图 > YourController > YourView.fr.resx
此外,我错过了 services.AddMvc().AddViewLocalization();在启动时本地化视图。
编辑
我刚刚对一个新项目进行了测试。这是 Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseRequestLocalization(BuildLocalizationOptions());
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
}
private RequestLocalizationOptions BuildLocalizationOptions()
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("fr")
};
var options = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("fr"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
};
// this will force the culture to be fr.
// It must be changed to allow multiple cultures, but you can use to test now
options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(context =>
{
var result = new ProviderCultureResult("fr", "fr");
return Task.FromResult(result);
}));
return options;
}
希望对您有所帮助。
此致,
您好,我正在尝试为 ASP.NET Core 实现本地化,但我无法 return 翻译值 - 它总是 return "key" 值 - "Load".当我打印文化时 - 它 returns "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";
}
Resources/Controllers.ValuesController.fr.resx
...
<data name="Load" xml:space="preserve">
<value>Load this value!</value>
</data>
...
我认为问题出在与资源相关的文件夹结构上。您应该将资源存储在这个结构中:
资源 > 控制器 > YourController.fr.resx
资源 > 视图 > YourController > YourView.fr.resx
此外,我错过了 services.AddMvc().AddViewLocalization();在启动时本地化视图。
编辑
我刚刚对一个新项目进行了测试。这是 Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseRequestLocalization(BuildLocalizationOptions());
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
}
private RequestLocalizationOptions BuildLocalizationOptions()
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("fr")
};
var options = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("fr"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
};
// this will force the culture to be fr.
// It must be changed to allow multiple cultures, but you can use to test now
options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(context =>
{
var result = new ProviderCultureResult("fr", "fr");
return Task.FromResult(result);
}));
return options;
}
希望对您有所帮助。
此致,