【发布时间】:2020-06-23 12:50:40
【问题描述】:
我在我的应用程序中配置了一个全球化,问题是当我调用一个正在进行服务调用的控制器方法时,它不起作用(实际上是返回键)。当我删除服务调用并且方法仍然为空时,它工作得很好。
public async Task<IActionResult> Index(int id)
{
var item = _mapper.Map<ItemViewModel>(await _itemService.GetById(id));
item.ItemImages = await _itemImageService.GetImageByItemId(id);
return View(null);
}
剃刀页面
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
@{
ViewData["Title"] = "Index";
}
<div class="container" style="margin:100px 0 0 0">
<h1>@Localizer["Category"]</h1>
</div>
Startup.cs
services.AddLocalization(opts => opts.ResourcesPath = "Resources");
services.AddMvc()
.AddViewLocalization(
LanguageViewLocationExpanderFormat.Suffix,
opts => opts.ResourcesPath = "Resources")
.AddDataAnnotationsLocalization();
services.AddControllersWithViews();
services.AddRazorPages();
配置
var supportedCultures = new[]
{
new CultureInfo("am-AM"),
new CultureInfo("en-US"),
new CultureInfo("ru-RU")
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
【问题讨论】:
-
IViewLocalizer的工作方式是,如果找不到该键的值,它将返回该键。所以也许你只是错过了翻译。 -
澄清一下——我指的是上面默认的 IViewLocalizer 实现行为。
-
不,它工作正常,所以我找到了一个解决方法,只是将我的方法从异步更改为正常,现在工作正常,但我不知道为什么会这样。
-
这很有趣。你不应该那样做。你能分享一个托管在 GitHub 上的小型复制品吗?我可以看看。
-
@Artak 这是我的 GitHub 存储库github.com/h2002d/PetFinder.git
标签: .net asp.net-core cultureinfo asp.net-core-localization