强制本地化在特定控制器中使用特定语言
Force localization be in specific language in specific controller
我有一个 web-api 项目,它有几个控制器,我为它实现了本地化(英语和韩语),但想要一个或两个控制器 return 消息是英文的,即使语言是韩语(不想使用简单的英文信息,想阅读英文资源)
可能吗?
我像下面这样设置本地化:
services.Configure<RequestLocalizationOptions>(options =>
{
var cultures = new List<CultureInfo> {
new CultureInfo("en"),
new CultureInfo("kr")
};
options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en");
options.SupportedCultures = cultures;
options.SupportedUICultures = cultures;
});
services.AddMvc().AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) =>factory.Create(typeof(SharedTranslate));
});
services.AddLocalization(o =>
{
o.ResourcesPath = "Resources";
});
在构造函数中:
public AuthAdminController(IStringLocalizer<SharedTranslate> localizer,...
我在控制器的构造函数中使用了以下代码,但没有用
//First
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
//Second
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en");
在 AuthAdminController
中检索本地化字符串时使用以下代码
CultureInfo.CurrentUICulture = new CultureInfo("<<Your language Code>>");
var localizedString = localizer[resourceKey].Value;
CultureInfo.CurrentUICulture = new CultureInfo("<<Reset to default one>>");
return localizedString;
第一行设置 CurrentUICulture
和第三行重置为默认值。
我有一个 web-api 项目,它有几个控制器,我为它实现了本地化(英语和韩语),但想要一个或两个控制器 return 消息是英文的,即使语言是韩语(不想使用简单的英文信息,想阅读英文资源)
可能吗?
我像下面这样设置本地化:
services.Configure<RequestLocalizationOptions>(options =>
{
var cultures = new List<CultureInfo> {
new CultureInfo("en"),
new CultureInfo("kr")
};
options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en");
options.SupportedCultures = cultures;
options.SupportedUICultures = cultures;
});
services.AddMvc().AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) =>factory.Create(typeof(SharedTranslate));
});
services.AddLocalization(o =>
{
o.ResourcesPath = "Resources";
});
在构造函数中:
public AuthAdminController(IStringLocalizer<SharedTranslate> localizer,...
我在控制器的构造函数中使用了以下代码,但没有用
//First
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
//Second
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en");
在 AuthAdminController
CultureInfo.CurrentUICulture = new CultureInfo("<<Your language Code>>");
var localizedString = localizer[resourceKey].Value;
CultureInfo.CurrentUICulture = new CultureInfo("<<Reset to default one>>");
return localizedString;
第一行设置 CurrentUICulture
和第三行重置为默认值。