In order to create multi-language application following are the requirements :
1. Resource files to store text for different languages. Each language will have its separate resource file.
2. A form to display text in different languages.
3. A custom abstract class which overrides ExecuteCore() method of controller class and inherits controller class.
4. Cookie, to store and pass language selected in dropdown from view to ExecuteCore() method.
Following are the steps explained :
First we are going to create a form and Register class file.
We are going to use the properties defined on the above image on the form. Lets see how view will look like.
The properties defined in register class are used in this form.
We are done with properties and form. Now we will se how to create resource files.
In order to hold resource files, we have created MultiResources Folder and added 4 resource files. One file is for english and other are for
Spanish, French and German. While naming these resource files, we need to follow naming convention as shown in the image. Lets see what these files will include.
The resource file above is for language english. It has xml tags. There is one data tag which holds key. This key is used to access the value mentioned in the value tag.
This key will be present in all the resource files, but with value in that particular language.
The above image is for french language resource file. As you can see it has same keys as mentioned in english resource file, but with different value.
We will see how to use these keys at view level.
In order to use resource keys, we need to add reference of Resource files. As shown in above image, we have to add reference of MultiResource folder. Then we can use those keys in our view as shown below.
As you can see in the above image we have use Multi keyword and accessed key from the resource files. The application depending on the current culture will pick the text from resource file matching the current culture. Lets see how we are going to store data in the cookie.
On Change event of Language dropdown, we are accessing the language selected and we create a cookie storing the language selected. The above code does the same. Now we will see how we gonna use this cookie on controller.
As shown in above image, we will create one helper folder and inside that folder will create one class. This class will hold the ExecuteCore() method as shown below.
We have to create an abstract class which will inherit controller class. Inside this class we will override ExecuteCore() virtual method. This method is called before any other ActionResult method.
As we can see in the above image, we are accessing the language value from cookie. Using switch loop, we are setting correct culture name depending upon language selected and retrieved from cookie.
And finally the current thread's culture is set. After execution of this method, our Index method is called as shown in image below.
As shown in above image the HomeController class inherits MultiBase class which we created and which inturn inherits controller class. After executing ExecuteCore() method Index() method is called and view is rendered wit current culture set to language selected in the dropdown.
You may also want to know :