Git Product home page Git Product logo

Comments (7)

LazZiya avatar LazZiya commented on May 12, 2024 1

glad to know it worked fine.

btw, this arrangement will not go for nothing, I will use it to improve the new system as well :)

from expresslocalization.

Gaudico avatar Gaudico commented on May 12, 2024

As a workaround I have created this Adapter injected as singleton and it seems to work fine:

    public class CustomValidationAttributeAdapterProvider : IValidationAttributeAdapterProvider
    {
        private IValidationAttributeAdapterProvider _baseProvider;
        private IValidationAttributeAdapterProvider BaseProvider
        {
            get
            {
                if (_baseProvider != null)
                    return _baseProvider;

                var providers = _services.GetServices<IValidationAttributeAdapterProvider>();

                _baseProvider = providers.FirstOrDefault(i => i.GetType().Name.StartsWith("ExpressValidationAttributeAdapterProvider"))
                                ?? new ValidationAttributeAdapterProvider();
                return _baseProvider;
            }
        }
        private readonly IServiceProvider _services;


        public CustomValidationAttributeAdapterProvider(IServiceProvider services)
        {
            _services = services;
        }
        
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            return attribute switch
            {
                RequireAtLeastOneOfGroupAttribute requireAtLeastOneOfGroupAttribute => new RequireAtLeastOneOfGroupAttributeAdapter(requireAtLeastOneOfGroupAttribute , stringLocalizer),
                _ => BaseProvider.GetAttributeAdapter(attribute, stringLocalizer)
            };
        }
    }

from expresslocalization.

LazZiya avatar LazZiya commented on May 12, 2024

Option 1 :

In startup file set UseExpressValidationAttributes to false:

services.AddRazorPages()
        .AddExpressLocalization<LocSource>(ops =>
        {
            ops.UseExpressValidationAttributes = false;
            // ...
        });

This will not register ExpressValidationAttributeAdapterProvider, so you will be able to create your own custom adapter, then register it and configure data annotation localization.

To do so, you can create an extension method as below:

/// <summary>
/// </summary>
/// <typeparam name="T">Type of localization resource</typeparam>
public static IMvcBuilder AddCustomDataAnnotationsLocalization<T>(this IMvcBuilder builder) where T : class
{
    builder.AddDataAnnotationsLocalization(ops =>
    {
        var t = typeof(T);
        ops.DataAnnotationLocalizerProvider = (type, factory) =>
            factory.Create(t.Name, t.Assembly.GetName().Name);
    });
    
    builder.Services.AddSingleton<IValidationAttributeAdapterProvider, CustomValidationAttributeAdapterProvider>();

    return builder;
}

Then you can simply call it from startup:

services.AddRazorPages()
         .AddExpressLocalization<LocSource>(ops =>
         {
             ops.UseExpressValidationAttributes = false;
             // ...
        })
        .AddCustomDataAnnotationsLocalization<LocSource>();

To keep using ExAttributes make sure that your have configured them as in ExpressValdiationAttributeAdapterProvider.

Option 2 :

Just saw your newly posted answer :)

Option 3 :

I've developed a newer localization project with much more powerful features (auto resource creating, online translation, etc.) and the valdiation attributes provider is configured as public class, that may give you more options to configure.

from expresslocalization.

LazZiya avatar LazZiya commented on May 12, 2024

@Gaudico, I think you are right since all Ex AttributeAdapters are internal classes! if converting them to public can solve your problem, I can publish a hotfix soon. is it okay?

from expresslocalization.

Gaudico avatar Gaudico commented on May 12, 2024

Yes, but for it to work, I would have to copy the implementation of IValidationAttributeAdapterProvider.GetAttributeAdapter made for ExpressValdiationAttributeAdapterProvider into my provider.
Maybe the best solution will be to take that piece of code to some public place with which I can invoke from my own adapter, so if in the future this implementation changes, I would not have to worry about updating the adapter code.

However, I am very interested in your new project and I will take a look at it and see if I switch to that system.
Thank you very much for responding so quickly, and excuse my English, it is not my native language and I asked Google for a little help :)

from expresslocalization.

LazZiya avatar LazZiya commented on May 12, 2024

Just published the hotfix for your issue,

  • First install the latest version 4.0.2
  • I've introduced a new class ExAttributeSwitch and moved all the code required for selecting an Ex attribute adapter to a static method that you can call from within your own adapter:
public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
{
    return ExAttributeSwitch.GetAttributeAdapter<T>(attribute, stringLocalizer) ?? base.GetAttributeAdapter(attribute, stringLocalizer);
}

So, you can continue with Option 1 in my previous reply.

Feel free to ask me if you need any further help.

English is not my native language as well :)

BR,
Ziya

from expresslocalization.

Gaudico avatar Gaudico commented on May 12, 2024

It works like a charm, thank you very much.

I really like what I have seen of your new project so I will surely move to that system. Which leads me to the fact that this arrangement we've made is not going to be used for too long... hahaha.

from expresslocalization.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.