Skip to content

Data Annotations - Caching #52

Open

Description

@MathieuDR

I'm using ASP.NET Core 2 together with this adaptation of AspNetCoreLocalization. I'm however a bit 'stuck' on resetting the cache for my data annotation translations.

When I reset the cache, everything is updated (global translations (custom) and my resource translations) however, my data annotations refuse to be 'updated', even though they should be used as resource translations (stock behaviour).

const bool useTypeFullNames = false;
const bool useOnlyPropertyNames = false;
const bool returnOnlyKeyIfNotFound = false;
bool createNewRecord = env.IsDevelopment();
            

services.AddDapperLocalization(options => options.UseSettings(
       useTypeFullNames, useOnlyPropertyNames, returnOnlyKeyIfNotFound, createNewRecord
));

services.AddMvc()
       .AddViewLocalization()
       .AddDataAnnotationsLocalization()
       .AddRazorOptions(options => { options.PageViewLocationFormats.Add("/Pages/Partials/{0}.cshtml"); });

Is there any way I can also make the data annotation translations reset cache?

Activity

  1. velorium-star commented on Jan 23, 2019

    @velorium-star

    +1 Exactly this happens to me also.

  2. sihon82 commented on Jan 30, 2020

    @sihon82

    I had same problem... seems to be a problem with DataAnnotation resource that in some way continue to reference the first ISqlLocalizer object created, also after cleaning the internal cache dictionary.

    I solved in this way:

    SqlStringLocalizerFactory.cs :

    
    public void ResetCache()
            {
                lock (_context)
                {
                    _context.DetachAllEntities();
                }
    
                foreach (String localizerKey in _resourceLocalizations.Keys)
                {
                    SqlStringLocalizer localizer = _resourceLocalizations[localizerKey] as SqlStringLocalizer;
    
                    localizer.ReloadLocalizations(GetAllFromDatabaseForResource(localizerKey));
                }
            }
    
            public void ResetCache(Type resourceSource)
            {
                lock (_context)
                {
                    _context.DetachAllEntities();
                }
    
                IStringLocalizer returnValue;
                
                if (_resourceLocalizations.TryGetValue(resourceSource.FullName, out returnValue))
                {
                    SqlStringLocalizer localizer = _resourceLocalizations[resourceSource.FullName] as SqlStringLocalizer;
    
                    localizer.ReloadLocalizations(GetAllFromDatabaseForResource(resourceSource.FullName));
                }
    
                
            }
    
    

    SqlStringLocalizer.cs:

    private Dictionary<string, string> _localizations; //Removed readonly
    
    public void ReloadLocalizations(Dictionary<string, string> localizations)
            {
                if (_localizations != null)
                    _localizations.Clear();
    
                _localizations = localizations;
            }
    
    
  3. JoeGoodwin commented on May 21, 2021

    @JoeGoodwin

    Could this bug fix be implemented please?

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions