Comments (10)
Got it working after trying out a couple of approaches. I got it working it using just couple of lines as
services.AddMvc() .AddDataAnnotationsLocalization(o => { var factory = services.BuildServiceProvider().GetService<IStringLocalizerFactory>(); var localizer = factory.Create(typeof(Resource)); o.DataAnnotationLocalizerProvider = (t, f) => localizer; }) .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
Where Resource is your Resource Class name which matches with the json file name.
Now my model looks like:
`
public class LocalizationModel
{
[Required(ErrorMessage = "The {0} field is required.")]
[MaxLength(4, ErrorMessage = "The field {0} must be a string or array type with a maximum length of '{1}'.")]
public string Name { get; set; }
[Range(0, 10, ErrorMessage = "The field {0} must be between {1} and {2}.")]
public int Id { get; set; }
}
`
My Resource.en.json file looks like:
{ "The {0} field is required.": "The {0} field is required.", "The field {0} must be a string or array type with a maximum length of '{1}'.": "The field {0} must be a string or array type with a maximum length of '{1}'.", "The field {0} must be between {1} and {2}.": "The field {0} must be between {1} and {2}." }
You can use some more readable keys in json file and then do rest of the changes as required
from my.extensions.localization.json.
Thanks .. I appreciated!!
from my.extensions.localization.json.
@hishamco I have created one example with localization working with DataAnnotations also.
https://github.com/Sandeep321/asp.netcore-JsonResourceLocalizer
might be helpful
from my.extensions.localization.json.
Before I dig into, please check the LocalizationSample.Web, because my JsonLocalizer
should work as same as StringLocalizer
except it looks for the resources from the JSON files
I tried some ways as I found over StackOverflow etc but nothing seems right for this Json localizer
Can you point to some of those?
from my.extensions.localization.json.
Hope you share your repo if it's public, so I can create a link for it
from my.extensions.localization.json.
I will create one and share soon. and provide the link here.
from my.extensions.localization.json.
As I see the DataAnnotations issue that you talked about before isn't related directly to the JsonLocalizer
perhaps a setup issue, furthermore I expect that you 'll reference my package that's published in NuGet rather than copy paste the code again without adding a value
from my.extensions.localization.json.
Thank you @Sandeep321 for sharing.
This addresses an issue we had with DataAnnotation
, and confirmed a solution we expected to work.
[CC: @Muhammad-Altabba].
from my.extensions.localization.json.
I wonder @hishamco though if it's possible to add this setup it into the library.
For example, using extension method to MVC, e.g. AddJSONDataAnnotationsLocalization() maybe?
from my.extensions.localization.json.
@abarrak the DataAnnotation stuff is handled in the MVC. I will open a new issue with your suggestion and investigate on that
from my.extensions.localization.json.
Related Issues (20)
- Add Benchmark HOT 16
- Missing Localization Behavior
- Remove internal contructor in JsonStringLocalizer HOT 1
- Ability to override methods in JsonStringLocalizer and JsonStringLocalizerFactory HOT 9
- Registering the service one should provide absolute path, otherwise exception is thrown HOT 31
- Localization FallBack Considerations
- Support ResourceLocationAttribute HOT 1
- Support RootNamespaceAttribute
- assign culture in runtime HOT 3
- Have one resource file for project HOT 13
- Any plan to switch the project to a .NET standard class library ? HOT 10
- How to use with DataAnnotation? HOT 17
- Regression Bug HOT 1
- LocalizationSample project is not working HOT 3
- About NET5.0 Blazor WebAssembly Uses My.Extensions.Localization.Json HOT 14
- System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string. HOT 11
- Class based localisation only works when the class is in the root namespace HOT 5
- Can you provide an core api example with DataAnnotations? HOT 2
- when using xunit test(SUT),there is an exception throwed! HOT 7
- JsonResourceManager does not dispose of (unix) File System Watches HOT 2
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 my.extensions.localization.json.