gpt4 book ai didi

c# - ASP Core 2.0 本地化程序找不到资源文件

转载 作者:行者123 更新时间:2023-11-30 23:06:23 29 4
gpt4 key购买 nike

我有一个要本地化的 asp 核心应用程序。我马上开始。

在 startup.cs - ConfigureServices 中:

        services.AddLocalization(options => options.ResourcesPath = "Resources");

services.AddMvc()
.AddViewLocalization(opts =>
{ opts.ResourcesPath = "Resources"; })
.AddDataAnnotationsLocalization();

startup.cs - 配置:

   var supportedCultures = new[]
{
new CultureInfo("en"),
new CultureInfo("fr"),
};

app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en"),
// Formatting numbers, dates, etc.
SupportedCultures = supportedCultures,
// UI strings that we have localized.
SupportedUICultures = supportedCultures
});

我的资源文件是有序的。以fr为例(对于HomeControllerHome Action ):{root}/Resources/Views/Home/Home.fr.resx

这是一个不起作用的 View :

@using Microsoft.AspNetCore.Mvc.Localization

@inject IViewLocalizer Localizer

@{
ViewData["Title"] = @Localizer["test"];
}

<div class="row">
<div class="col-lg-6">
<div class="panel panel-default">
<div class="panel-heading">My Toolboxes</div>
<div class="panel-body">
</div>
</div>
</div>
<div class="col-lg-6">
<div class="panel panel-default">
<div class="panel-heading">Notices</div>
<div class="panel-body">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</div>
</div>
</div>

Title总是“test”,应该是“essai”。

当我检查 Localizer["test"] 时,IsResourceNotFound = true

我 100% 确定 chrome 正在请求 fr 语言环境:

GET /Home/Home HTTP/1.1
Host: localhost:62677
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: fr,en-US;q=0.9,en;q=0.8

最佳答案

我有同样的错误,在我的情况下,在转到项目设置并将 Assembly name 更改为与 Default namespace 完全相同的值后,问题得到了解决。 (我的程序集名称是 <MyProjectName> 而我的默认 namespace 设置为 <MyCompany.MyProjectName> )。在内部,LocalizerFactory 似乎创建了根据程序集名称查找资源的路径。如果您的资源的命名空间不以程序集名称开头,将找不到它们。

关于c# - ASP Core 2.0 本地化程序找不到资源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48236157/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com