Localization ASP.NET核心全球化和本地化-不加载值
您好,我正在尝试实现ASP.NET Core的本地化,但我无法返回转换后的值-它总是返回键值-Load。当我打印区域性时,它返回fr,因此区域性设置正确。老实说,我没有主意了 我将学习本教程: 再看看这个例子: 构建项目时,将编译资源文件并在以下位置复制: obj/Debug/netcoreapp1.1/BoilerPlate.Resources.Controllers.valuescocontroller.fr.Resources 及 bin/Debug/netcoreapp1.1/fr/BoilerPlate.resources.dll Startup.cs ValuesController.cs 资源/Controllers.ValuesController.fr.resxLocalization ASP.NET核心全球化和本地化-不加载值,localization,asp.net-core,globalization,Localization,Asp.net Core,Globalization,您好,我正在尝试实现ASP.NET Core的本地化,但我无法返回转换后的值-它总是返回键值-Load。当我打印区域性时,它返回fr,因此区域性设置正确。老实说,我没有主意了 我将学习本教程: 再看看这个例子: 构建项目时,将编译资源文件并在以下位置复制: obj/Debug/netcoreapp1.1/BoilerPlate.Resources.Controllers.valuescocontroller.fr.Resources 及 bin/Debug/netcoreapp1.1/fr/
我认为问题在于与资源相关的文件夹结构。您应该将资源存储在此结构中: 资源>控制器>YourController.fr.resx 资源>视图>控制器>视图.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;
}
希望这对你有帮助
您好,Hi@Tiago我尝试了两种结构。根据文档,我们可以使用以下结构:资源名称点或路径命名资源/控制器.HomeController.fr.resx点资源/控制器/HomeController.fr.resx路径,我不使用视图。我只是从控制器返回响应。这是WebAPI-未使用任何视图。@PetarIvanov,我根据刚创建的新项目更新了答案。它工作得很好。我只想补充一点,我使用我答案的文件夹结构进行了测试。奇怪的是。。。我在Windows上创建了一个新项目,复制/粘贴了我的代码,它成功了。将新创建的项目发送到*nix机器,运行它,它就工作了。从那时起,我的旧代码也开始工作了。唯一的区别是VS2017添加到csproj文件中的参考:ResXFileCodeGenerator我想知道这个问题是否已经解决,因为我怀疑这也是我的问题。
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();
}
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";
}
...
<data name="Load" xml:space="preserve">
<value>Load this value!</value>
</data>
...
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;
}