Orchard CMS中的Localized DisplayName属性

问题描述 投票:0回答:3

我已经以其他形式看到了这个问题(主要与验证有关),但无法理解答案。

我有以下内容:

    [Required, DisplayName("First Name")]
    public virtual string GivenName { get; set; }

我想将DisplayName本地化。我已经尝试过创建一个RequiredAttribute扩展,就像其他人做的那样:

public class RequiredFieldAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute
    {
        public RequiredFieldAttribute()
        {
            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        public override string FormatErrorMessage(string name) {
            var field = T(name).Text;
            return T("Please fill in {0}.", field).Text;
        }
    }
}

返回的错误消息不断返回英文名称。但我确信有翻译,因为当我在视图中使用以下内容时,我会获得本地化名称:

@Html.LabelFor(m => register.GivenName, T("First Name"))

消息本身(请填写...)也是本地化的。除了字段名称不是。为什么fieldname的字符串在视图中请求时是本地化的,但在扩展中使用时则不然。

在查看有关codeplex的讨论时,我看到一些变更集修复了名称空间Orchard.MVC.DataAnnotations中的本地化数据注释问题。但我在现有模块中找不到任何关于如何使用它们的例子。

localization data-annotations orchardcms
3个回答
2
投票

这个问题可能已经过时,但我在Orchard CMS 1.8.1中问自己同样的事情。所以这就是我如何做到的。

假设你有一个看起来像的模型

public class Message
{
  [System.ComponentModel.DataAnnotations.Required(ErrorMessage="Please select a date."]
  public System.DateTime Date { get; set; }
  ...
}

并且您希望对所需的验证消息进行德语翻译,然后在模块中创建App_Data\Localization\de-DE\orchard.module.po文件并在其中放入以下段落

msgctxt "System.ComponentModel.DataAnnotations.RequiredAttribute"
msgid "Please select a date."
msgstr "Bitte wählen Sie ein Datum."

您甚至可以跳过msgctxt行(这是翻译的范围),因为它的工作也没有。可以在http://pology.nedohodnik.net/doc/user/en_US/ch-poformat.html找到.po文件格式的描述。

如果翻译不起作用,则以下方法是一个很好的调试入口点:

Orchard/Localization/Text.cs
public LocalizedString Get(string textHint, params object[] args) ...

Orchard/Localization/Services/DefaultLocalizedStringManager.cs
public string GetLocalizedString() ...

0
投票

我也遇到过这个问题。我想知道你是否解决了问题。

你提到你找不到Orchard.MVC.DataAnnotations的例子。它们位于(如果您正在使用Orchard来源(截至1.6))~/Orchard.Framework/Mvc/DataAnnotations

这就是LocalizedRequiredAttribute.cs的样子:

namespace Orchard.Mvc.DataAnnotations {
public class LocalizedRequiredAttribute : RequiredAttribute {
    public LocalizedRequiredAttribute(RequiredAttribute attribute, Localizer t) {
        AllowEmptyStrings = attribute.AllowEmptyStrings;

        if ( !String.IsNullOrEmpty(attribute.ErrorMessage) )
            ErrorMessage = attribute.ErrorMessage;

        T = t;
    }

    public Localizer T { get; set; }

    public override string FormatErrorMessage(string name) {
        return String.IsNullOrEmpty(ErrorMessage)
            ? T("The {0} field is required.", name).Text
            : T(ErrorMessage, name).Text;
    }
}

所以你必须用[LocalizedRequiredAttribute]装饰。 对不起,我现在的理解是,在这个例子中,你继续像以前一样使用[必需],Orchard负责为你改变它。但是,您必须手动创建.po文件,因为TranslationManager不会选择这些文件。

我自己在尝试本地化[Display(Name="")]属性时遇到了麻烦。令我困惑的一部分是在System.ComponentModelSystem.ComponentModel.DataAnnotations,因为它们都具有相似的属性(前者为DisplayNameAttribute,后者为DisplayAttibute)。


0
投票

您可以在orchard项目中使用普通验证相同的正常mvc项目您只需将这些设置添加到webconfig:

<appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>

结束

© www.soinside.com 2019 - 2024. All rights reserved.