如何在 .net 5 web 中添加语言本地化 API
How to add Language localization in .net 5 web API
我已尝试按照 this tutorial 的步骤在我的 API 级别应用语言本地化。
但是无论 HTTP 请求 Header 是什么,它总是带来默认语言。
这是启动代码,在 ConfigureServices
方法中:
#region Localiztion
services.AddLocalization();
services.AddRequestLocalization(x =>
{
x.DefaultRequestCulture = new RequestCulture("en");
x.ApplyCurrentCultureToResponseHeaders = true;
x.SupportedCultures = new List<CultureInfo> {
new("ar"),
new("en")
};
x.SupportedUICultures = new List<CultureInfo> {
new("ar"),
new("en")
};
});
#endregion
在Configure
方法:
app.UseRequestLocalization();
资源:
我已尝试按照教程中的说明添加 culture
header 或按照 Microsoft Docs 中的说明添加 Content-Language
header 但仍然,它仅无论默认值是什么,都会带来默认值。
经过一些研究,与本地化一起工作的 HTTP header 是 Accept-Language
。
我已尝试按照 this tutorial 的步骤在我的 API 级别应用语言本地化。
但是无论 HTTP 请求 Header 是什么,它总是带来默认语言。
这是启动代码,在 ConfigureServices
方法中:
#region Localiztion
services.AddLocalization();
services.AddRequestLocalization(x =>
{
x.DefaultRequestCulture = new RequestCulture("en");
x.ApplyCurrentCultureToResponseHeaders = true;
x.SupportedCultures = new List<CultureInfo> {
new("ar"),
new("en")
};
x.SupportedUICultures = new List<CultureInfo> {
new("ar"),
new("en")
};
});
#endregion
在Configure
方法:
app.UseRequestLocalization();
资源:
我已尝试按照教程中的说明添加 culture
header 或按照 Microsoft Docs 中的说明添加 Content-Language
header 但仍然,它仅无论默认值是什么,都会带来默认值。
经过一些研究,与本地化一起工作的 HTTP header 是 Accept-Language
。