Comments (7)
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.
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.
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.
@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.
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.
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.
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)
- Roadmap HOT 2
- DefaultRequestCulture not working when change to non-en culture in 3.1.3 HOT 3
- Can we create custom DataAnnotations to avoid re-typing ErrorMessage in every attribute HOT 1
- Case sensitivity mismatch HOT 2
- Null pointer exception HOT 5
- Change culture along with Area and extra parameter HOT 6
- Language Selection Dropdown not Working HOT 4
- NullReferenceException when RazorPages is not enabled for MVC project with .NET CORE 3.1 HOT 3
- Missing ExRequiredAttributeAdapter HOT 3
- RoutePatternException HOT 8
- DataAnnotation messages not being translated HOT 2
- Retrieve Value from .resx file HOT 6
- ExRange unexpected behavior as Required HOT 3
- ExpressLocalizationCore3 Field Validation Issue HOT 2
- Culture is not supported HOT 9
- Null Reference Error HOT 4
- Urls without language tag does not work. HOT 2
- Localization with query string HOT 3
- Breaking change in MS.NET.Test.Sdk HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from expresslocalization.