IRunicApplicationLocale interface

Namespace Runic.Application.Views in Runic.Application.Views 0.23.0

public interface IRunicApplicationLocale

The application's locale: the language that every Window shows unless it sets its own with IRunicWindowLocale, and that menu bars, status icons and notifications are built in. One IRunicApplicationLocale.SetLocaleAsync reaches every open Window and every Window opened later.

Remarks

RunicWindowServiceCollectionExtensions.AddRunicWindowService registers it as a singleton. Its locale is null until the application sets one, and each page then keeps its own language, as before application locales existed.

A switch runs in this order: the followers that IRunicApplicationLocale.Follow registered, such as the application's Translations context, prepare the new locale; then the locale changes, every Window that sets no locale of its own follows it (its pages switch, and its IRunicWindowLocale raises PropertyChanged); then IRunicApplicationLocale.LocaleChanged is raised, so a handler that rebuilds the menu bar or a status icon reads text that is already in the new language. A typical start, on the UI thread, so the handler runs there too:

var locale = services.GetRequiredService<IRunicApplicationLocale>();
locale.Follow(translations.SetLocaleAsync);           // a Runic Translations TranslationContext
locale.LocaleChanged += (_, _) => RebuildMenuBar();
await locale.SetLocaleAsync(settings.Language ?? Negotiate(DesktopLanguages.Preferred));

Threads: each Window follows in a turn of its own model context (WindowContentSession.ModelContext), as other changes of a Window reach its ViewModel, so its IRunicWindowLocale raises PropertyChanged there; a Window without a model context, or a scope whose Window has not opened, follows on the thread of the switch. Each IRunicApplicationLocale.LocaleChanged handler runs on the SynchronizationContext that was current when it subscribed, such as the UI thread or a ViewModel's model context, and on the thread of the switch when there was none.

In hosted mode (another Runic application shows this application's Windows, RunicDesktopHostOptions.Guest), the locale follows the host application's until this application sets one of its own with IRunicApplicationLocale.SetLocaleAsync; setting null follows the host's again.

Applications and tests may implement this interface. A minor release adds members to it only with a default implementation.

Properties

Locale

string? Locale { get; }

The application's locale, a BCP 47 tag such as de or pt-BR, or null while the application sets none.

Methods

Follow

IDisposable Follow(Func<string, CancellationToken, ValueTask> follower)

Registers a follower that prepares each new locale before the Windows and IRunicApplicationLocale.LocaleChanged see it, such as TranslationContext.SetLocaleAsync of Runic Translations: locale.Follow(context.SetLocaleAsync).

Parameters

follower

Receives the new locale and the switch's cancellation token.

Returns

A registration; dispose it to stop following.

Remarks

Followers run in the order they were registered, each awaited before the next, and only for a locale that is not null. Registering one does not call it: register it before the first IRunicApplicationLocale.SetLocaleAsync, or set its locale yourself.

SetLocaleAsync

ValueTask SetLocaleAsync(string? locale, CancellationToken cancellationToken = default)

Changes the application's locale: its followers first, then every Window without a locale of its own, then IRunicApplicationLocale.LocaleChanged. Switches run one at a time, in the order they were requested, and their events are raised in that order; setting the current locale does nothing, except for Windows that failed to follow it.

Parameters

locale

The new locale, or null.

cancellationToken

Stops waiting for an earlier switch, and is passed to the followers.

Exceptions

ArgumentException

The value is not 1 to 64 letters, digits and -, starting with a letter.

OperationCanceledException

cancellationToken was cancelled before the locale changed.

AggregateException

The locale changed, but a Window or a IRunicApplicationLocale.LocaleChanged handler failed.

InvalidOperationException

A follower or a Window's PropertyChanged handler of a running switch called it.

Remarks

null calls no follower, which keeps its locale, and tells the Windows that follow the application that it sets none: their pages keep the language they show. A follower that throws stops the switch: the locale, the Windows and the followers after it keep the old locale, followers before it keep the new one, and the exception is rethrown.

Once the followers succeeded, the switch completes: a Window whose update throws, for example because a PropertyChanged handler of its ViewModel throws, does not stop the other Windows or the event, and a IRunicApplicationLocale.LocaleChanged handler that throws does not stop the other handlers. After the event, the call throws an AggregateException with every such failure; the locale has changed. Calling it again with the same locale updates the Windows that failed again, and raises no event.

When switches overlap, IRunicApplicationLocale.LocaleChanged reports each change once, in order: a switch that a later switch followed before its event was raised raises none, and the next event's RunicApplicationLocaleChangedEventArgs.OldLocale is the locale the previous event reported. A IRunicApplicationLocale.LocaleChanged handler may switch again; that call returns when the Windows followed, and its event is raised after the current handlers. A follower, or a Window's PropertyChanged handler during a switch, must not: the switch it would wait for is its own, so the call throws InvalidOperationException.

Events

LocaleChanged

event EventHandler<RunicApplicationLocaleChangedEventArgs>? LocaleChanged

Raised after the locale changed and the Windows followed it. Rebuild the application's own text here: the menu bar, status icons and notifications.

Remarks

Each handler runs on the SynchronizationContext that was current when it subscribed: subscribe on the UI thread, and the handler runs there. A handler subscribed without one, as in a console start or a test, runs on the thread that completes the switch, usually a thread-pool thread, and marshals UI work itself. The handlers run one after another, in the order they subscribed, and IRunicApplicationLocale.SetLocaleAsync returns after they ran, so do not block a handler's thread waiting for that call.