Class LocalizeExtension

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

XAML markup extension that produces a binding to a localized string, re-resolving automatically on every change of RefreshTriggeringRevision of Current - a language switch, a new formatter, a new lookup-failure text builder or a change of the registered resource managers. Usage in XAML: {l:Localize {x:Static res:StringsKeys.Greeting}} for a typed key, {l:Localize Greeting} for a key-only lookup across all registered resource managers, or {l:Localize Key=Greeting, ResourceManager={x:Static res:StringsKeys.ResourceManager}} to scope the lookup to one file. A key chosen by the view model is bound: {l:Localize {Binding StatusKey}} or Key="{Binding StatusKey}"; bind a ResourceKey to keep the key compile-checked. When the resource value is a composite format string, supply its arguments as Argument child elements: <l:Localize Key="{x:Static res:StringsKeys.PeopleInvited}"><l:Argument Value="{Binding PeopleCount}" /></l:Localize>.

public sealed class LocalizeExtension : MarkupExtension
Inheritance
MarkupExtension
LocalizeExtension
Inherited Members

Constructors

LocalizeExtension()

Initializes a new instance of the LocalizeExtension class.

public LocalizeExtension()

LocalizeExtension(object)

Initializes a new instance of the LocalizeExtension class and sets Key: {l:Localize Greeting} is {l:Localize Key=Greeting}, and {l:Localize {Binding StatusKey}} is {l:Localize Key={Binding StatusKey}}. A single object-typed constructor is used instead of overloads so Avalonia's positional-argument resolution is never ambiguous, which also keeps the XAML previewer working.

public LocalizeExtension(object key)

Parameters

key object

The key: a string, a ResourceKey, or a binding that yields one of them.

Properties

Arguments

Gets the format arguments, in declaration order: the Argument child elements. The first argument is {0}, the second {1}, and so on, whether it has a name or not.

[Content]
public Collection<Argument> Arguments { get; }

Property Value

Collection<Argument>

Key

Gets or sets the key to resolve: a string, scoped to ResourceManager if that is set and otherwise searched across all registered resource managers, or a ResourceKey, which resolves through its own resource manager; or a binding, such as Key="{Binding StatusKey}", whose value is resolved by the same rules on every update. Prefer binding a ResourceKey chosen by the view model, which keeps the key compile-checked; a bound string key is an escape hatch where a typo silently becomes the failure text. A bound value that gives no key, such as null, an empty string or a value of another type, renders an empty string.

[AssignBinding]
public object? Key { get; set; }

Property Value

object

ResourceManager

Gets or sets the resource manager that scopes a string Key to a single .resx file. Ignored when Key is a ResourceKey; when unset, a string key is searched across all registered resource managers.

public ResourceManager? ResourceManager { get; set; }

Property Value

ResourceManager

Methods

ProvideValue(IServiceProvider)

Provides the value supplied to the target property: a binding whose value is the localized string for Key, formatted with Arguments, updated live on every change of the arguments and of RefreshTriggeringRevision. It needs no target element, so it also works in a style setter, in a resource and in a template.

public override object ProvideValue(IServiceProvider serviceProvider)

Parameters

serviceProvider IServiceProvider

The service provider supplied by the XAML loader; not used.

Returns

object

An Avalonia multi-binding that yields the localized string.

Exceptions

InvalidOperationException

Key is null, or a constant that is neither a string nor a ResourceKey; or an Argument has no Value, an empty Name, or the same name as another one.

ArgumentNullException

Key is a ResourceKey without a resource manager.