Class LocalizeExtension
- Namespace
- RentADeveloper.ResXLocalization.WPF
- Assembly
- ResXLocalization.WPF.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
-
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 WPF's positional-argument resolution is never ambiguous.
public LocalizeExtension(object key)
Parameters
keyobjectThe key: a string, a ResourceKey, or a plain 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.
public Collection<Argument> Arguments { get; }
Property Value
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 plain 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.
public object? Key { get; set; }
Property Value
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
Methods
ProvideValue(IServiceProvider)
Provides the value supplied to the target property: a multi-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
serviceProviderIServiceProviderThe service provider supplied by the XAML loader.
Returns
- object
The value of the WPF multi-binding for the target: the binding expression for a dependency property, the multi-binding itself for a
Setter. Inside a XAML designer that supplies the target property as a CLR property instead of a dependency property, such as Rider's WPF previewer, a string instead, without validation: the lookup of a constant Key without arguments, or an empty string when Key is null or a binding.
Exceptions
- InvalidOperationException
Key is null, a constant that is neither a string nor a ResourceKey, or a binding that is not a plain Binding; or an Argument has no Value, an empty Name, the same name as another one, or a binding that is not a plain Binding.
- ArgumentNullException
Key is a ResourceKey without a resource manager.