GithubHelp home page GithubHelp logo

Comments (10)

Sandeep321 avatar Sandeep321 commented on June 21, 2024 3

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.

hishamco avatar hishamco commented on June 21, 2024 1

Thanks .. I appreciated!!

from my.extensions.localization.json.

Sandeep321 avatar Sandeep321 commented on June 21, 2024 1

@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.

hishamco avatar hishamco commented on June 21, 2024

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.

hishamco avatar hishamco commented on June 21, 2024

Hope you share your repo if it's public, so I can create a link for it

from my.extensions.localization.json.

Sandeep321 avatar Sandeep321 commented on June 21, 2024

I will create one and share soon. and provide the link here.

from my.extensions.localization.json.

hishamco avatar hishamco commented on June 21, 2024

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.

abarrak avatar abarrak commented on June 21, 2024

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.

abarrak avatar abarrak commented on June 21, 2024

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.

hishamco avatar hishamco commented on June 21, 2024

@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)

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.