.NET Core 本地化全球化
.NET Core localization globalization
谁能帮我确认这段代码是改变应用程序所有用户的文化,还是只改变当前用户的文化?
var cultureInfo = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
我想显示应用程序接受的不同文化的组合,并在组合中 select 更改文化,但是如果我打开应用程序,例如 Chrome和Firefox,当我换一个的时候,好像换另一个的文化就变了,这很可怕。
默认这设置为机器的文化,所以它自动适用于所有用户。
如果您打算允许用户在他们的浏览器中设置他们自己的文化,您打算使用查询字符串来定义文化,或者您打算做一个自定义请求文化提供者(在下一节中概述) ) 允许代码根据其他参数设置自定义区域性,则需要提供支持的区域性列表
public void ConfigureServices(IServiceCollection services)
{
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-US");
//By default the below will be set to whatever the server culture is.
options.SupportedCultures = new List<CultureInfo> { new CultureInfo("en-US"),new CultureInfo("en-NZ") };
options.RequestCultureProviders = new List<IRequestCultureProvider>();
});
services.AddMvc();
}
详细信息:-refer_this_document
您可以使用下面的代码
Startup.ConfigureServices
CultureInfo[] supportedCultures = new[]
{
new CultureInfo("ar"),
new CultureInfo("fa"),
new CultureInfo("en")
};
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new RequestCulture("ar");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders = new List<IRequestCultureProvider>
{
new QueryStringRequestCultureProvider(),
new CookieRequestCultureProvider()
};
});
Startup.Configure
app.UseRequestLocalization();
更改语言:
[HttpPost]
public IActionResult SetLanguage(string culture, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);
return LocalRedirect(returnUrl);
}
更多详情:
谁能帮我确认这段代码是改变应用程序所有用户的文化,还是只改变当前用户的文化?
var cultureInfo = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
我想显示应用程序接受的不同文化的组合,并在组合中 select 更改文化,但是如果我打开应用程序,例如 Chrome和Firefox,当我换一个的时候,好像换另一个的文化就变了,这很可怕。
默认这设置为机器的文化,所以它自动适用于所有用户。
如果您打算允许用户在他们的浏览器中设置他们自己的文化,您打算使用查询字符串来定义文化,或者您打算做一个自定义请求文化提供者(在下一节中概述) ) 允许代码根据其他参数设置自定义区域性,则需要提供支持的区域性列表
public void ConfigureServices(IServiceCollection services)
{
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-US");
//By default the below will be set to whatever the server culture is.
options.SupportedCultures = new List<CultureInfo> { new CultureInfo("en-US"),new CultureInfo("en-NZ") };
options.RequestCultureProviders = new List<IRequestCultureProvider>();
});
services.AddMvc();
}
详细信息:-refer_this_document
您可以使用下面的代码
Startup.ConfigureServices
CultureInfo[] supportedCultures = new[]
{
new CultureInfo("ar"),
new CultureInfo("fa"),
new CultureInfo("en")
};
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new RequestCulture("ar");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders = new List<IRequestCultureProvider>
{
new QueryStringRequestCultureProvider(),
new CookieRequestCultureProvider()
};
});
Startup.Configure
app.UseRequestLocalization();
更改语言:
[HttpPost]
public IActionResult SetLanguage(string culture, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);
return LocalRedirect(returnUrl);
}
更多详情: