Skip to content

Add Static Extractor Support#114

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

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

Conversation

@MikeAlhayek
Copy link
Copy Markdown

@MikeAlhayek MikeAlhayek commented Apr 28, 2026

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
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.

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.

2 participants