Class LocalizeEnumConverter

Namespace
RentADeveloper.ResXLocalization.Avalonia
Assembly
ResXLocalization.Avalonia.dll

Multi-value converter that localizes an enumeration value in a hand-written MultiBinding, for places where a markup extension such as LocalizeEnumExtension is not accepted, for example a Binding-typed property. The children of the MultiBinding are: 0, the refresh trigger <Binding Path="RefreshTriggeringRevision" Source="{x:Static core:Localizer.Current}" />, which re-runs the conversion on every change of RefreshTriggeringRevision; 1, the enumeration value; 2 and up, the format arguments, in order. The value is mapped to a resource key by EnumKeyBuilder, by default {KeyPrefix}{EnumTypeName}_{Value}.

public sealed class LocalizeEnumConverter : IMultiValueConverter
Inheritance
LocalizeEnumConverter
Implements
IMultiValueConverter
Inherited Members

Remarks

A MultiBinding whose first child is the enumeration value, followed by CurrentCulture, renders an empty string; the enumeration value must be child 1, after the refresh trigger.

Constructors

LocalizeEnumConverter()

Initializes a new instance of the LocalizeEnumConverter class.

public LocalizeEnumConverter()

Properties

ArgumentNames

Gets or sets the names of the format arguments, as one comma-separated string: entry i names the argument at child 2 + i. For example, ArgumentNames="Count,Total" names child 2 Count and child 3 Total. Entries are trimmed; an empty entry leaves that argument unnamed (",Total" names only the second). null (the default) or an empty string means no names. Every argument keeps its position whether it is named or not, and names matter only to a formatter that understands them, such as ICU MessageFormat; the default formatter ignores them. Extra names are ignored, and arguments without an entry are unnamed.

public string? ArgumentNames { get; set; }

Property Value

string

Exceptions

ArgumentException

Two entries have the same name, compared ordinally.

InvalidOperationException

The converter is the shared Default instance, which is read-only.

Default

Gets the shared, search-all converter instance with default settings. Reference it from XAML as {x:Static l:LocalizeEnumConverter.Default} when no file scoping is required. The shared instance is read-only - create your own converter to customize KeyPrefix, ResourceManager or ArgumentNames.

public static LocalizeEnumConverter Default { get; }

Property Value

LocalizeEnumConverter

KeyPrefix

Gets or sets the key prefix passed to EnumKeyBuilder. null (the default) means "the EnumKeyBuilder decides", which by default is Enum_; a value overrides the prefix for this converter.

public string? KeyPrefix { get; set; }

Property Value

string

Exceptions

InvalidOperationException

The converter is the shared Default instance, which is read-only.

ResourceManager

Gets or sets the resource manager that scopes the lookup to a single .resx file. When unset, the generated key is searched across all registered resource managers.

public ResourceManager? ResourceManager { get; set; }

Property Value

ResourceManager

Exceptions

InvalidOperationException

The converter is the shared Default instance, which is read-only.

Methods

Convert(IList<object?>, Type, object?, CultureInfo)

Converts the enumeration value to its localized, formatted string.

public object Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)

Parameters

values IList<object>

The bound values: 0, the refresh trigger, whose value is ignored; 1, the Enum value to localize; 2 and up, the format arguments, named by ArgumentNames.

targetType Type

The type of the binding target property.

parameter object

An optional converter parameter; not used.

culture CultureInfo

The culture supplied by the binding; not used (the active culture is taken from Current).

Returns

object

The localized string for the enumeration value, or Empty when there are fewer than two values or value 1 is not an Enum.