I'm making a form in Umbraco 7.7.7 and I'll translate the labels. For this I use the Display-attribute and the UmbracoHelper class to get access to the dictionary.
public class ContactViewModel
{
private UmbracoHelper _helper = new UmbracoHelper();
[Required]
[Display(Name = _helper.Field("#First name"))] // ← error on this line.
public string FirstName { get; set; }
[Required]
[Display(Name = _helper.Field("#Last name"))] // ← error on this line.
public string LastName { get; set; }
[Required]
[EmailAddress]
[Display(Name = _helper.Field("#Email"))] // ← error on this line.
public string Email { get; set; }
[Required]
[Display(Name = _helper.Field("#Message"))] // ← error on this line.
public string Message { get; set; }
// Some other properties...
public ContactViewModel()
{
_helper = new UmbracoHelper(UmbracoContext.Current);
}
}
Attributes are metadata and must be know at compile time. _helper.Field() is a method and its results cannot be determined until runtime. You need to use a constant, for example [Display(Name = "First name")].
The value of Name must be a compile-time constant. It cannot be determined at runtime. Examples of a compile-time constant would be:
a string literal eg. "Foo"
A string declared using the const keyword eg. const string foo = "Foo";
What you can't use is a traditional variable - in other words, anything that can change value when Umbraco is running. At the moment you build your application the value of the Display(Name) attribute has to be known and immutable (fixed). From this point on it cannot change. So it cannot be a return value from a method, as you are using.
Translate labels to render inside the view
I'm making a form in Umbraco 7.7.7 and I'll translate the labels. For this I use the
Display
-attribute and theUmbracoHelper
class to get access to the dictionary.Here is my code in my view:
But I've got this error on the marked lines:
I've also tried with this code:
and convert the result of
_helper.Field()
to a string instead ofIHtmlString
.But code now this error also on the marked lines:
From Stack Overflow I know this:
P.S.: I'm following this documentation from our.umbraco.org.
Update: Is there a other way to do more or less the same thing?
I think you've already got your answer from SO. The issue is here:
The value of Name must be a compile-time constant. It cannot be determined at runtime. Examples of a compile-time constant would be:
"Foo"
const string foo = "Foo";
What you can't use is a traditional variable - in other words, anything that can change value when Umbraco is running. At the moment you build your application the value of the Display(Name) attribute has to be known and immutable (fixed). From this point on it cannot change. So it cannot be a return value from a method, as you are using.
Is there an other option to do more or less the same thing?
I've seen a few articles on this, though don't have any personal experience. But the places I'd look are:
https://creativewebspecialist.co.uk/2014/04/17/umbraco-mvc-regionalisation-of-error-messages/
https://24days.in/umbraco-cms/2017/multilingual-validation-messages/
However, you'd need to check these techniques work in latest Umbraco.
is working on a reply...