Bis*_*ich 7 asp.net-core-mvc asp.net-core asp.net-core-1.0 asp.net-core-localization
我一直在尝试按照microsofts文档实现我的asp .NET Core 1.0 RTM Web应用程序的本地化,但我无法让它工作.我遇到的问题是,无论我如何尝试设置文化,它总是显示英语资源文件中的字符串.
如果有人有5分钟的时间,我真的很感激你的意见.
这是我关于本地化的Startup.cs内容:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.SubFolder)
.AddDataAnnotationsLocalization();
services.AddScoped<LocalizationFilter>();
services.AddLocalization(options => options.ResourcesPath = "Resources");
var supportedCultures = new List<CultureInfo>{
new CultureInfo("en-US"),
new CultureInfo("sl-SI"),
new CultureInfo("de-DE"),
new CultureInfo("hr-HR")
};
services.Configure<RequestLocalizationOptions>(options =>
{
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.DefaultRequestCulture = new RequestCulture(new CultureInfo("en-US"),
new CultureInfo("en-US"));
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
...
var supportedCultures = new List<CultureInfo>{
new CultureInfo("en-US"),
new CultureInfo("sl-SI"),
new CultureInfo("de-DE"),
new CultureInfo("hr-HR")
};
var requestLocalizationOptions = new RequestLocalizationOptions
{
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures,
DefaultRequestCulture = new RequestCulture(new CultureInfo("en-US"),
new CultureInfo("en-US"))
};
app.UseRequestLocalization(requestLocalizationOptions);
}
Run Code Online (Sandbox Code Playgroud)
这就是我在ActionFilter中改变OnActionExecuting内部文化的方式
actionContext.HttpContext.Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) });
CultureInfo.CurrentCulture = culture;
CultureInfo.CurrentUICulture = culture;
Run Code Online (Sandbox Code Playgroud)
我也试过在我的控制器上做这个没有运气.
然后在我的视图中,我@inject IViewLocalizer Localizer
用来显示本地化的字符串@Localizer["Name"]
.
我的resoucres位于Resources/Views/ControllerName文件夹中,如下所示:
无论我如何尝试改变文化,显示的字符串始终来自.en资源文件.有什么我想念的吗?还有什么我应该做的吗?看来我遇到的问题是设置语言.基于您应该设置cookie的文档Response.Cookies.Append
,但我也尝试过,CultureInfo.CurrentCulture
因为Thread.CurrentThread.CurentCulture
不再可用.
我真的不知道自己错过了什么.有什么想法吗?
您可能会点击以下内容:https: //github.com/aspnet/Mvc/issues/4692
主要看一下评论:https://github.com/aspnet/Mvc/issues/4692#issuecomment-223671462
简介:您可以Resource
在MVC中创建一个过滤器来解决此问题:
public class CultureSettingResourceFilter : IResourceFilter, IOrderedFilter
{
public int Order
{
get
{
return int.MinValue;
}
}
public void OnResourceExecuted(ResourceExecutedContext context)
{
// By this time the response would already have been started, so do not try to modify the response
}
public void OnResourceExecuting(ResourceExecutingContext context)
{
var culture = httpContext.GetRouteValue("your-culture-key-name")?.ToString();
// set your culture here
CultureInfo.CurrentCulture = culture;
CultureInfo.CurrentUICulture = culture;
}
}
services.AddMvc(o =>
{
o.Filters.Add(new CultureSettingResourceFilter());
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1652 次 |
最近记录: |