Fat*_*rol 6 c# localization asp.net-core
我创建了一个空项目.
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(s => s.ResourcesPath = "Resources");
var supportedCultures = new CultureInfo[]
{
new CultureInfo("de-CH"),
new CultureInfo("en-GB"),
};
services.Configure<RequestLocalizationOptions>(s =>
{
s.SupportedCultures = supportedCultures;
s.SupportedUICultures = supportedCultures;
s.DefaultRequestCulture = new RequestCulture(culture: "de-CH", uiCulture: "de-CH");
});
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseStaticFiles();
// Using localization
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud)
文件夹结构
Resources
|
|--Controllers
| HomeController.de.resx
| HomeController.en.resx
| HomeController.resx
Run Code Online (Sandbox Code Playgroud)
调节器
public class HomeController : Controller
{
private readonly IStringLocalizer<HomeController> _stringLocalizer;
public HomeController(IStringLocalizer<HomeController> stringLocalizer)
{
_stringLocalizer = stringLocalizer;
}
public IActionResult Index()
{
string testValue = _stringLocalizer["Test"];
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
我是关于asp.net核心的新手,我只是想了解,为什么testValue总是返回Test,这有点令人困惑.我做错了什么?如果你帮助我,我会很高兴的.
小智 10
只需添加Microsoft.Extensions.Localization包.
完成后,它可以工作.
ResourcePath是可选的,如果将其保留为null,则资源文件组织样式与经典Asp.Net应用程序(在目标类的相同位置)相同.
归档时间: |
|
查看次数: |
4651 次 |
最近记录: |