Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 1, 2025

Enable derived classes to customize localization behavior by making key methods virtual and exposing internal state via protected properties.

Changes

JsonStringLocalizer

  • Make GetStringSafely method protected virtual

JsonStringLocalizerFactory

  • Add protected properties exposing internal state:
    • ResourceNamesCache
    • LocalizerCache
    • ResourcesRelativePath
    • ResourcesType
    • LoggerFactory

Example Usage

public class CustomJsonStringLocalizer : JsonStringLocalizer
{
    public CustomJsonStringLocalizer(JsonResourceManager resourceManager, 
        IResourceNamesCache cache, ILogger logger) 
        : base(resourceManager, cache, logger) { }

    protected override string GetStringSafely(string name, CultureInfo culture)
    {
        // Custom string resolution logic
        return base.GetStringSafely(name, culture);
    }
}

public class CustomLocalizerFactory : JsonStringLocalizerFactory
{
    public CustomLocalizerFactory(IOptions<JsonLocalizationOptions> options, 
        ILoggerFactory loggerFactory) 
        : base(options, loggerFactory) { }

    protected override JsonStringLocalizer CreateJsonStringLocalizer(
        string resourcesPath, string resourceName)
    {
        var resourceManager = ResourcesType == ResourcesType.TypeBased
            ? new JsonResourceManager(resourcesPath, resourceName)
            : new JsonResourceManager(resourcesPath);
        
        return new CustomJsonStringLocalizer(resourceManager, 
            ResourceNamesCache, LoggerFactory.CreateLogger<CustomJsonStringLocalizer>());
    }
}
Original prompt

This section details on the original issue you should resolve

<issue_title>Ability to override methods in JsonStringLocalizer and JsonStringLocalizerFactory</issue_title>
<issue_description>It would be nice to be able to override methods in in JsonStringLocalizer and JsonStringLocalizerFactory by making them virtual and at least using the protected modifier. E.g. if I'd like to change the functionality on how the cache is build, there is currently no way of overriding just that method (BuildResourcesCache), I'd have to make a new localizer by copying the content from JsonStringLocalizer.

Stumbled upon this when wanting to try to implement a temporary workaround to support the #39 functionality for the current release.</issue_description>

Comments on the Issue (you are @copilot in this section)

@hishamco > I'd like to change the functionality on how the cache is build, there is currently no way of overriding just that method (BuildResourcesCache),

This will may assist to fix #34

Could you please let me know what's the suggested changes that you like to made? </comment_new>
<comment_new>@hishamco
FYI some of the method couldn't be override able, let us think from many perspectives and choose the proper methods to make them virtual with Why question

Thanks for your suggestion</comment_new>
<comment_new>@hishamco
Do you have a repo for your fallback scenario? or I will provide a sample for such thing an probably I unit tests may be useful to ensure that nothing is broken</comment_new>
<comment_new>@hishamco
@thomkle I just created a sample in https://github.com/hishamco/My.Extensions.Localization.Json/tree/custom-localizer with minimal modification in the original JsonStringLocalizer, BTW you could notice that Yes will have the same value Oui in all the cultures, which is the thing that I told you about before, so again this should respect the default fallback culture

Hope this fix your issue</comment_new>
<comment_new>@hishamco
@thomkle I'd like to let you know I'm working on this now, but I need to from you what kind of extensibility do you need, I don't think adding protected virtual to every method is a correct choice</comment_new>
<comment_new>@hishamco
Add this for now 0a48303

I will try to work on improving the BuildResourceCache hopefully I will add an extensibility point there ...</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: hishamco <3237266+hishamco@users.noreply.github.com>
Copilot AI changed the title [WIP] Add ability to override methods in JsonStringLocalizer Add extensibility to JsonStringLocalizer and JsonStringLocalizerFactory Dec 1, 2025
Copilot AI requested a review from hishamco December 1, 2025 01:30
@hishamco hishamco marked this pull request as ready for review December 10, 2025 01:42
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.

Ability to override methods in JsonStringLocalizer and JsonStringLocalizerFactory Cache Mechanism

2 participants