Class TlumachMessageResolver
- Namespace
- Tlumach.DataAnnotations
- Assembly
- Tlumach.DataAnnotations.dll
Resolves the localized message of a validation attribute from a class created by Tlumach Generator.
An instance of this class is composed into every localized validation attribute of Tlumach. It is also available to an attribute that an application derives from TlumachValidationAttribute and that needs the message template itself.
public sealed class TlumachMessageResolver
- Inheritance
-
TlumachMessageResolver
- Inherited Members
Remarks
A resolved binding refers either to a translation unit or to a translation manager together with a key, and never holds text, so a change of the culture and a reload of a translation are picked up on the next call. Bindings are cached process-wide, which keeps validation free of reflection after the first message of each annotation has been formatted.
Constructors
TlumachMessageResolver(ITlumachLocalizedErrorMessage)
Initializes a new instance of the TlumachMessageResolver class for the given attribute.
public TlumachMessageResolver(ITlumachLocalizedErrorMessage owner)
Parameters
ownerITlumachLocalizedErrorMessageThe attribute that provides the configuration. Its properties are read lazily, so a resolver may be created in the constructor of an attribute, before the named properties of the annotation have been assigned.
Exceptions
- ArgumentNullException
Thrown when
owneris null.
Properties
Owner
Gets the attribute that provides the configuration.
public ITlumachLocalizedErrorMessage Owner { get; }
Property Value
Methods
Format(string, params object?[])
Formats the localized message the way FormatErrorMessage(string) does.
The placeholders of the template are positional, as in {0} and {1}. A named placeholder such as {userName}, which the rest of Tlumach accepts, raises a
FormatException here.
public string Format(string name, params object?[] arguments)
Parameters
namestringThe display name of the member being validated. It becomes the
{0}argument of the template.argumentsobject[]The remaining arguments of the template, which become
{1},{2}and so on. Pass them in the same order as the attribute that is being localized.
Returns
- string
The formatted message.
Exceptions
- InvalidOperationException
Thrown when the message cannot be resolved. See GetTemplate().
- FormatException
Thrown when the template contains a named placeholder, or refers to more arguments than were provided.
GetTemplate()
Returns the message template for the effective culture, without processing the placeholders it contains.
The placeholders of a validation message are positional, as in {0} and {1}, because the message is formatted with Format(IFormatProvider, string, params object[])
the way FormatErrorMessage(string) defines it. The named placeholders that the rest of Tlumach accepts are not resolved here and make the formatting fail.
public string GetTemplate()
Returns
- string
The message template as it appears in the translation.
Exceptions
- InvalidOperationException
Thrown when the attribute is not configured for Tlumach, when the configuration is invalid, or when the key provides no text for the effective culture.
IsConfigured()
Tells whether the attribute is configured to take its message from a Tlumach translation.
When this method returns false, the attribute carries no Tlumach configuration at all and must format its message the way it would without Tlumach. That is not an error: it makes a localized attribute behave exactly like the attribute it derives from.
public bool IsConfigured()
Returns
Exceptions
- InvalidOperationException
Thrown when only one of the localization class and the key is set, or when a Tlumach key is combined with a literal error message or with a resource name.