Skip to content

Add Static Extractor Support#114

Open
MikeAlhayek wants to merge 1 commit into
OrchardCoreContrib:mainfrom
MikeAlhayek:ma/add-static-support
Open

Add Static Extractor Support#114
MikeAlhayek wants to merge 1 commit into
OrchardCoreContrib:mainfrom
MikeAlhayek:ma/add-static-support

Conversation

@MikeAlhayek

@MikeAlhayek MikeAlhayek commented Apr 28, 2026

Copy link
Copy Markdown

With this PR we can use static successors instead of having to inject localizer using DI.

public static class StaticLocalizers
{
    private static IStringLocalizerFactory _stringLocalizerFactory;

    public static void Configure(IStringLocalizerFactory stringLocalizerFactory) =>
        _stringLocalizerFactory = stringLocalizerFactory;

    public static TypedStringLocalizer<T> S<T>() => new(_stringLocalizerFactory.Create(typeof(T)));

    public static TypedStringLocalizer<T> T<T>() => new(_stringLocalizerFactory.Create(typeof(T)));
}

public readonly struct TypedStringLocalizer<T>(IStringLocalizer localizer)
{
    public LocalizedString this[string name] => localizer[name];

    public LocalizedString this[string name, params object[] arguments] => localizer[name, arguments];

    public LocalizedString Invoke(string name) => localizer[name];

    public LocalizedString Invoke(string name, params object[] arguments) => localizer[name, arguments];

    public LocalizedString Plural(int count, string singular, string plural) =>
        localizer[count == 1 ? singular : plural, count];
}

@hishamco

hishamco commented Apr 28, 2026

Copy link
Copy Markdown
Member

Why static over DI?

@MikeAlhayek

Copy link
Copy Markdown
Author

It'll make it so much easier to use and also allow us to localize static string like Permission in OC. but also won't force us into needing multiple constructor in cases like this https://github.com/OrchardCMS/OrchardCore/pull/19198/changes.

@hishamco

Copy link
Copy Markdown
Member

@sebastienros, while you approved the related PR, are you OK with this PR? It's an anti-pattern that reminds me of a service locator

@sebastienros

Copy link
Copy Markdown

Why do we need to model custom S/T static helpers instead of trying to detect something likw new LocalizedString("Foo") ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants