I’m going to try how to develop an application which is support more than one language. I’m going to support two language in my example application. English and Turkish. The main language will be English. Its mean, application going to be English on guest’s first visit. Users can update their language from main page in this application. After changed language, I’m going to set a new cookie for selected language. Expired time is going to be 365 days for my example application. Let’s get start to code it!
In the first step, I’m going to create a new folder in project root for my language files. The folder’s name is Resources. After creating folder, I’m going to add two resources file for my languages. First one is Global. It’s for my main (default) language. Second one is Global.tr It’s for Turkish language. The screenshot for this step is as follows.
Now, I’m going to fill out Global and Global.tr files. I should define texts in this files which texts I want to use for multiple language. Name column should be same for all resources file because application will use this column as a key for texts. My example file’s screenshot is as follows.
Now, I’m going to use defined texts in my project files. For this, I’m going to add my resources’ namespace in my file. After added namespace, I can use texts in my file now. I’m going to use as follows syntax for call my values. This step is the hardest step for this example.
The example is as follows.
Everything is ready after defined all texts in the resources files. Let’s handle this in our application. How is going to application catch the language? How is going to change? I’m going to add as follows code block in the Global.asax file for detect the language.
What is going to do this code block? When application start, It’s going to read default language value from the web.config file and then It’s going to check cookie. (I said this at the beginning of the article.) If cookie’s value is not null or empty, It’s going to use user’s language. If It’s null, It’s going to use default language for the current user.
How am I going to change the language on the front end?
I’m going to add two image button for change the language. The screenshot is as follows.
If user click any flag, I’m going to set a value for the selected language in the language cookie. I’m going to use Jquery for this. The example code block is as follows.
Now, everything is ready for support to multiple languages. (English and Turkish) You can add a third language via same scenario. (Global.culture)
An extra information: I used as follows code for get/set cookie operations. You can use as follows too.
I added this to my current project. Because of that I can’t share any sample code for this example.
See you in the next post.
Thank you 🙂