我想在 dotnet core web api 中添加全球化和本地化,尽管我尝试使用以下链接。 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.1
,但是,我没有成功。
这是我的 Startup.cs 代码...
services.AddLocalization(options => options.ResourcesPath = "Resources/Localization");
services.AddControllers()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix, options => { options.ResourcesPath = "Resources/Localization"; })
.AddDataAnnotationsLocalization();
services.Configure<RequestLocalizationOptions>(options =>
{
var cultures = new []
{
new CultureInfo("en"),
new CultureInfo("fa")
};
options.DefaultRequestCulture = new RequestCulture("en");
options.SupportedCultures = cultures;
options.SupportedUICultures = cultures;
});
配置方法包含以下内容
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, Seed seeder)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// app.UseHttpsRedirection();
app.UseRouting();
//ADDING LOCATIONLIZATION
app.UseRequestLocalization(app.ApplicationServices.GetRequiredService<IOptions<RequestLocalizationOptions>>().Value);
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
这是带有 .resx 文件的 Resources/Localization 文件夹结构。
最后,当我使用dotnet build命令构建 dotnet api 时,它会在Visual Studio Code Terminal中提供以下错误。
/Users/naimdahee/Desktop/ERPEra/ERPEra.API/Resources/Localization/ERPEra.en.resx:错误 MSB3103:Resx 文件无效。System.NullReferenceException:对象引用未设置为对象的实例。[/Users/naimdahee/Desktop/ERPEra/ERPEra.API/ERPEra.API.csproj] /Users/naimdahee/Desktop/ERPEra/ERPEra.API/Resources/Localization/ERPEra.en.resx:错误MSB3103:在Microsoft.Build .Tasks.ResourceHandling.MSBuildResXReader.ReadResources(Stream s,字符串文件名,布尔路径RelativeToBasePath) [/Users/naimdahee/Desktop/ERPEra/ERPEra.API/ERPEra.API.csproj] /Users/naimdahee/Desktop/ERPEra/ERPEra.API /Resources/Localization/ERPEra.fa.resx:错误 MSB3103:Resx 文件无效。System.NullReferenceException:对象引用未设置为对象的实例。[/Users/naimdahee/Desktop/ERPEra/ERPEra.API/ERPEra.API。
构建失败。
/Users/naimdahee/Desktop/ERPEra/ERPEra.API/Resources/Localization/ERPEra.en.resx:错误 MSB3103:Resx 文件无效。System.NullReferenceException:对象引用未设置为对象的实例。[/Users/naimdahee/Desktop/ERPEra/ERPEra.API/ERPEra.API.csproj] /Users/naimdahee/Desktop/ERPEra/ERPEra.API/Resources/Localization/ERPEra.en.resx:错误MSB3103:在Microsoft.Build .Tasks.ResourceHandling.MSBuildResXReader.ReadResources(Stream s,字符串文件名,布尔路径RelativeToBasePath) [/Users/naimdahee/Desktop/ERPEra/ERPEra.API/ERPEra.API.csproj] /Users/naimdahee/Desktop/ERPEra/ERPEra.API /Resources/Localization/ERPEra.fa.resx:错误 MSB3103:Resx 文件无效。System.NullReferenceException:对象引用未设置为对象的实例。[/Users/naimdahee/Desktop/ERPEra/ERPEra.API/ERPEra.API。