Join Chris Woodruff for an in-depth discussion in this video ASP.NET Core DataAnnotations, part of ASP.NET Core: Internationalization.
- [Narrator] We're going to be looking at integrating your resource content into data annotations in your web project in this video. Go up to your exercise files, chapter three, video eight and grab base with annotations. Paste it to your desktop. Open it up and we're going to open up that folder with visuals to the O code. The first thing we need to do is take a look at startup.cs.
In addition to add view localization, we need to add localization for data annotations. Now that we've added localizations for data annotation, let's go out to our model for our login. Go under model, under account view models and open up login view model. You'll see that this view model has three properties. Email, password, and remember me.
Two are required, so let's look at how to localize these. So in addition to required, I want to have an error message. And that error message will just have the text required. So because we have a required on our password, I'm just going to replace that required with the one I just created. So in addition to required, email has a data annotation to make sure that it fits a certain format.
Now we're going to call that not valid email. And finally for this, we're going to set up a data annotation for display. And we're going to give the name, the text, your email. The password also needs to display. And we're just going to give that password for the name.
And since we want to have a different text based on the culture for remember me, I'm going to also set the display for this. And that text will be remember me. So let's save this. And take a look at our resources. Now I have created some resource files for you that incorporate into this project.
Let's go take a look at those. In exercise files, under resources you'll see that there's a folder called data annotations. Copy the files out of here. And let's put them into the resources under models, in the project that we are looking at. Let's bring visual studio code back up. We'll see that we do have our models incorporated into our project.
Let's make sure everything is saved. Let's bring up our terminal. Let's do a build. And we do have a bug. Now I mistyped one thing. So let's go to start up and instead of add data annotation, it should be data annotations. So let's save that. Rebuild, now let's run it.
So under login, I can see that because I'm in US English, I'm seeing the US English version of email, password, and remember me. Let's take a look at what French would look like on this view. As you could see, because of that view model, email, password, and remember me changed to the French equivalent of our English text. So that's just a little run through on how to update your view models to incorporate localization.
- Globalization and localization
- Internationalization considerations
- Creating the ASP.NET Core project
- Using the HTTP Accept-Language header
- Using the ASP.NET Core store project
- Displaying content with the controller
- Modifying the views
- Selecting a culture with cookies
- Systems of measurement formats