【问题标题】:DataAnnotation Localization Error when not translate未翻译时出现 DataAnnotation 本地化错误
【发布时间】:2018-05-31 09:21:40
【问题描述】:

我在 ASP.NET Core MVC 网站上工作,本地化在 Controller 和 View 中运行良好,但 DataAnnotation 的本地化出现错误:

如果是英语,我的默认语言和我的资源文件名如下:“Index.fr.resx”。
如果需要法语翻译,则使用翻译,否则返回字符串键(键为英文), 但是对于DataAnnotation,翻译效果很好,但是当需要英语时,网站会显示错误:

Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Intranet.Resources.ViewModels.News.Add.resources" was correctly embedded or linked into assembly

但是因为翻译工作找到了文件,并且对于Controller或View的本地化如果需要英文,则返回关键字符串并且工作正常,有什么想法吗?

Add.fr.resx:

Title => Titre

Add.cs

[Display(Name = nameof(Resources.ViewModels.News.Add.Title), ResourceType = typeof(Resources.ViewModels.News.Add))]
        public String Title { get; set; }

Startup.cs

services.AddMvc(
                config =>
                {
                    config.Filters.Add<ActionFilter>();
                })
                .AddViewLocalization(options => options.ResourcesPath = "Resources")
                .AddDataAnnotationsLocalization();

【问题讨论】:

    标签: c# asp.net-core localization asp.net-core-mvc data-annotations


    【解决方案1】:

    似乎无法在模型中对显示属性进行本地化,所以我只是在视图中进行了:

    placeholder="@Localizer[Html.DisplayNameFor(x => x.Title)]"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多