Join Chris Woodruff for an in-depth discussion in this video Display content in the view, part of ASP.NET Core: Internationalization.
- Now we've seen how to use controllers to modify our user-interface with localization. Next, we're going to use views. Now I want to make it clear that views and controllers are two distinct paths. If you use controllers, don't use views. And vice versa. But let's look at views now. I'm going to go out to our exercise files. I'm going to go chapter three, 0305, and I'm going to copy BaseWithRESXViews onto my desktop.
Then open that up and I'm going to open that folder with Visual Studio Code. The first thing you'll notice is we don't have a resources folder. So lets create one. And we're going to go out to our exercise files and get those resource files that we need. Under resources folder, we have views. Open up the views folder and you'll see that we have a large amount of resource files for our views.
Just be aware that we had to create one for each view from the controller that we were using. So you're probably asking yourself "Why would I use this many resources when I only had "four for the controllers?" So there's a few things. One is, once these are created, these resource files can be modified and you won't have as much impact because they're only containing a small amount of information. Another item that's interesting about using views with localization is that you can store HTML content more efficiently in the resource files for views.
It's not that it actually gets stored in there differently, but what happens is it gets used differently. So if you're going to store and use a lot of HTML content in your resource files that you need to show in your user-interface, I'd might look at views for localization over controllers. Now let's copy all of our resource files. We're going to come back into our base project and enter our resources folder and paste them.
Let's go back in the Visual Studio Code and again we'll see that all of our resource files exist in our project. So, unlike our controller project that we didn't have to modify the startup file, because it was already there from previous videos that we've looked at, we do need one small modification to our startup file. And I'm going to show you right now. So for the services, we're saying AddMvc.
But I need to add some more information to this AddMvc. I need to AddViewLocalization and also add some further information about view localization. So next, I need to add some options.
And I need to tell it the resource path to my resource files, which will be "Resources" which is the name of the folder that holds all of our resource files. And that's it. Now let's go over and modify our views.
- 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