Multilingual MVC Application With Customized JS Bundles Based on User Language

About

Here, you will learn about multilingual MVC application implementation flow, the creation of customized JavaScript bundles with translated alert messages based on user language, custom HTML helper classes, how to set user language in a cookie based on his login, displaying resource translation values in a form and how to maintain names and values in resource files.

Scenario

You will implement an MVC application to be displayed in various languages based on the user selected language. The form content is being displayed from the resource file based on the name and respective language and it would need to display the alert kind of information from customized bundled JavaScript based on user language whenever the user has done a form submission or input validation and so on.

RoadMap

When implementing the previous sample MVC application, you will learn the following concepts.

  1. Resource File creation with key, value pair combination for required language.
  2. Resource Key value display in a form based on user language.
  3. Cookie implementation in MVC.
  4. Bundling and Minification.
  5. Custom HTML helper class.
  6. Customized Bundle creation.
  7. Translate the JavaScript alert messages into user selected language.
  8. Localized JavaScripts minified and bundled, It helps to increase the performance of form.

Step 1

Create a new project named “MultilingualMVCApp” and choose the template “MVC” then it creates an MVC template project.

Step 2

Add a new application folder named “App_GlobalResources” to maintain the resource files with key/value pair combinations for various languages. Right-click on the Project “MultiLingualMVCApp” and select on “Add” and then select “Add ASP.NET Folder” and then click on “App_GlobalResources” like the following.

 

Step 3

Create a resource file named “Notifications.resx” for the default language English by following the following screenshots. Right-click on the “App_GlobalResources” folder, select “Add” and then click on “Resource file” and specify the name as “Notifications” and then click on the “Ok” button.

 

Click on "Resources File" and provide the name as "Notifications" as shown in the following screenshot.
 
 

In the similar way, create another resource file named “Notifications.fr.resx” under the App_GlobalResources file. After completion of these aforesaid procedure, the Solution Explorer looks like the following.

 

Step 4

In this step, you will add sample resource keys with translation values for English and French languages. For that double click on the “Notifications.resx” file, then the displayed file has three columns named Name, Value and Comments.

Name: This name field is treated as the Resource Key value and it should be unique in the file and not be empty.

You can now enter the sample key and respective values as shown in the following screenshot for the default language (English).

 

In the same way, you can add the same key values, whichever were specified in the “Notifications.resx” with respective translated values for the French language in the “Notifications.fr.resx” file is as follows.

 

Note: Here, I have used the Google translator to translate the sample English content to French content.
 
In the same way, you can add various resource files for different languages as said and you can add multiple resource types to the resx files (like strings, images, icons and so on) based on your needs.


Step 5

You can now create one controller named “DemoController” with the following procedure.  Right-click on the “Controllers” folder and select the “Add” option and click on the “Controller” link.

Click on the "Controller.." link and select the option “MVC 5 Controller – Empty” and then click on the “Add” button.

Then, specify the name as “DemoController” then click on the “Add” button.

Now, the controller is created with the default action named “Index” like the following.

 

Step 6

Add a view to the Index action method by right-clicking on the View function and click on “Add View” and then click on the Ok button. The following screenshots help you do that.

 

Click on "Add View..." then it opens a popup like the following.


Now, click on the "Add" button and then it will create an Index.cshtml file.

Step 7

Replace the existing design with the following design format that contains a sample form to enter the user name, select the user preferred language and the submit button.

  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4.   
  5. <h2>Demo</h2>  
  6. <div>  
  7.     @using (Html.BeginForm("Index", "Demo", FormMethod.Post))  
  8.     {  
  9.         <label>Enter User Name</label>  
  10.         <input type="text" name="txtName" placeholder="Guest"   
  11.                class="form-control" />  
  12.         <br/>  
  13.         <label>Select Language:</label>  
  14.         <select name="ddlLanguage" class="form-control">  
  15.             <option value="en">English</option>  
  16.             <option value="fr">French</option>  
  17.         </select>  
  18.         <br />  
  19.         <input type="submit" name="Submit" class="form-control" />  
  20.     }  
  21. </div>  
Now, build the application (F6) and run the application (F5) with the following URL (http://localhost:49624/Demo/Index) then the displayed form looks like the following. 

 

Step 8
 
In this step, write a piece of code for the post action of the submit button to set the user language code in the cookie, the user name in the TempData object (that helps to send data from one controller request to another) and then redirects to the home page.
  1. [HttpPost]  
  2.  public ActionResult Index(FormCollection collection)  
  3.  {  
  4.      TempData.Add("LoggedInUser", collection["txtName"]);  
  5.      string language = collection["ddlLanguage"];  
  6.      SetCulture(language);  
  7.   
  8.      return RedirectToAction("Home");  
  9.  }  
  10.   
  11.  /// <summary>  
  12.  /// Set the culture based on culture code and set the   
  13.  /// value in cookie  
  14.  /// </summary>  
  15.  /// <param name="cultureCode"></param>  
  16.  private void SetCulture(string cultureCode)  
  17.  {  
  18.   
  19.      var cookieCultureLanguage = new HttpCookie("UserLanguage")  
  20.      {  
  21.          Value = cultureCode  
  22.      };  
  23.   
  24.      Response.Cookies.Set(cookieCultureLanguage);  
  25.   
  26.      //Sets  Culture for Current thread  
  27.      Thread.CurrentThread.CurrentCulture =  
  28.      System.Globalization.CultureInfo.CreateSpecificCulture("en");  
  29.   
  30.      //Ui Culture for Localized text in the UI  
  31.      Thread.CurrentThread.CurrentUICulture =  
  32.      new System.Globalization.CultureInfo(cultureCode);  
  33.   
  34.  }  

The cookie that you have set in the previous code using the method “SetCulture” helps to display the respective user-selected language translated values in a form from the resource file.

Step 9

In this step, add one controller action method named “Home” to the "DemoController.cs" file with the respective view named “Home.cshtml” file.

  1. /// <summary>  
  2. /// Home Action Controller Method  
  3. /// </summary>  
  4. /// <returns></returns>  
  5. public ActionResult Home()  
  6. {  
  7.     return View();  

Then, right-click on the View() method in the Home action and then click on "Add View..". After clicking on “Add View” it opens a template then click on the “Add” button then it creates a “Home.cshtml” page with default design. You can leave the design as it is, in later steps you can add the respective design changes.

Step 10

Add a method named Application_AcquireRequestState to the Global.asax.cs file that helps to set the culture based on the user-selected language if the cookie exists else it sets the default culture to English.

  1.   /// <summary>  
  2.  /// Application AcquireRequestState  
  3.  /// </summary>  
  4.  /// <param name="sender"></param>  
  5.  /// <param name="e"></param>  
  6.  protected void Application_AcquireRequestState(object sender,   
  7. EventArgs e)  
  8.  {  
  9.   
  10.      string culture;  
  11.      HttpCookie cookie = Request.Cookies["UserLanguage"];  
  12.      if (cookie != null && cookie.Value != null   
  13.         && cookie.Value.Trim() != string.Empty)  
  14.          culture = cookie.Value;  
  15.      else  
  16.          culture = "en";  
  17.   
  18.      //Default Language/Culture for all number, Date format  
  19.      System.Threading.Thread.CurrentThread.CurrentCulture =  
  20.      System.Globalization.CultureInfo.CreateSpecificCulture("en");  
  21.   
  22.      //Ui Culture for Localized text in the UI  
  23.      System.Threading.Thread.CurrentThread.CurrentUICulture =  
  24.      new System.Globalization.CultureInfo(culture);  
  25.  }  
Step 11

In this step, you will implement a customized JavaScript bundle translator class. It helps to bundle the JavaScript with the respective user-selected language translated messages if any exists in the JavaScript file like alert, input validation messages and so on.

So, first create a new folder named “ResourceHandler”. For that right-click on the project “MultiLingualMVCApp” then select “Add” and click on “Add New Folder” and provide the name as “ResourceHandler” and then add a class file “JSTranslator” under the newly created folder "ResourceHandler".

Replace the “JSTranslator.cs” file code with the following code.
  1. using System.Text.RegularExpressions;  
  2. using System.Web;  
  3. using System.Web.Optimization;  
  4.   
  5. namespace MultiLingualMVCApp.ResourceHandler  
  6. {  
  7.     public class JSTranslator : IBundleTransform  
  8.     {  
  9.         #region IBundleTransform  
  10.   
  11.         /// <summary>  
  12.         /// IBundleTransform Process method  
  13.         /// </summary>  
  14.         /// <param name="context">Bundle Context</param>  
  15.         /// <param name="response">Bundle Response</param>  
  16.         public void Process(BundleContext context, BundleResponse response)  
  17.         {  
  18.             string translated = ScriptTranslator(response.Content);  
  19.             response.Content = translated;  
  20.         }  
  21.  
  22.         #endregion  
  23.  
  24.         #region Localization Of Js Bundle Flow  
  25.   
  26.         private static readonly Regex Regex =   
  27.             new Regex(@"GetResourceValue\(([^\))]*)\)",  
  28.                            RegexOptions.Singleline | RegexOptions.Compiled);  
  29.   
  30.         /// <summary>  
  31.         /// Translated script based on JavaScript content  
  32.         /// </summary>  
  33.         /// <param name="text"></param>  
  34.         /// <returns></returns>  
  35.         private string ScriptTranslator(string text)  
  36.         {  
  37.             MatchCollection matches = Regex.Matches(text);  
  38.             foreach (Match match in matches)  
  39.             {  
  40.                 object obj = HttpContext.GetGlobalResourceObject("Notifications",   
  41.                     match.Groups[1].Value);  
  42.                 if (obj != null)  
  43.                     text = text.Replace(match.Value, CleanText(obj.ToString()));  
  44.             }  
  45.   
  46.             return text;  
  47.         }  
  48.   
  49.         /// <summary>  
  50.         /// Format the text as Clean while displaying  
  51.         /// in JavaScript notifications  
  52.         /// </summary>  
  53.         /// <param name="text"></param>  
  54.         /// <returns></returns>  
  55.         private static string CleanText(string text)  
  56.         {  
  57.             text = text.Replace("'""");  
  58.             return text;  
  59.         }  
  60.  
  61.         #endregion  
  62.     }  
  63. }  

About JSTranslator.cs code

  • The JsTranslator class is inherited from the “IbundleTransform”.
  • The Process method is implemented, as it just gets the BundleResponse content and translates the JavaScript with user-selected language alerts, if any, using the ScriptTranslator method and will be assigned back to that content to the BundleResonse.
  • The Regex expression is the key term we used in script files, based on that formatted the regex expression.
  • The Regex Key Term example, you used in scripts is: GetResourceValue(ResourceKeyName)
  • The ScriptTranslator method first gets the MatchCollection object based on regex expression and then based on the Match collection key name get the translated value using the method “GetGlobalResourceObject”
  • The GetGlobalResourceObject takes the parameters of Resource file name and Resource Key name.
  • The CleanText method helps to clean if any characters that creates a problem when displaying in JavaScript.

So, you have now successfully added a custom JavaScript bundle class.

Step 12

In this step, you will add two JavaScript files with sample functions under the “Scripts” folder.

The first JavaScript file named “UserNotifications” file. It contains the following function.
  1. function UserTermsNotification() {  
  2.     alert('GetResourceValue(UserTermsDisplayNotification)');  

The second JavaScript file is named “UserOperations”. It contains the following functions.

  1. function AddUserMessage() {  
  2.     alert('GetResourceValue(UserAdded)');  
  3. }  
  4.   
  5. function DeleteUserMessage() {  
  6.     alert('GetResourceValue(UserDeleted)');  

Step 13

In this step, you will bundle the previously created JavaScript files in BundleConfig.cs (that exists in the App_Start folder) using a custom JavaScript bundle that you have created in the JSTranslator.cs file. So, add the following code block to the RegisterBundles method and also add the required namespaces; those are:

using System.Collections.Generic;
using MultiLingualMVCApp.ResourceHandler;

  1. var lstLangCultures = new List<string>() { "en""fr" };  
  2.  //Loops through available languages to add language specific   
  3.  //JavaScript minified bundles  
  4.  foreach (var cultureName in lstLangCultures)  
  5.  {  
  6.      var extJsNotifications = new Bundle(string.Format("~/Scripts  
  7.                              /Notifications-{0}", cultureName))  
  8.         .Include("~/Scripts/UserNotifications.js")  
  9.         .Include("~/Scripts/UserOperations.js");  
  10.      extJsNotifications.Transforms.Clear();  
  11.      extJsNotifications.Transforms.Add(new JSTranslator());  
  12.      extJsNotifications.Transforms.Add(new JsMinify());  
  13.      bundles.Add(extJsNotifications);  
  14.  } 
Step 14

In this step, you will implement the custom HTML helper class that helps to create a custom JavaScript bundle based on your needs. So, add a new class file named HtmlHelpers under the App_Start folder and then replace that with the following code.

  1. using System.Web;  
  2.   
  3. namespace MultiLingualMVCApp  
  4. {  
  5.     public class HtmlHelpers  
  6.     {  
  7.         /// <summary>  
  8.         /// Localized JavaScript bundle   
  9.         /// </summary>  
  10.         /// <param name="fileName"></param>  
  11.         /// <returns></returns>  
  12.         public static HtmlString LocalizedJsBundle(string fileName)  
  13.         {  
  14.             string culture;  
  15.             var cookie = HttpContext.Current.Request.Cookies["UserLanguage"];  
  16.             if (cookie != null && cookie.Value != null   
  17.                 && cookie.Value.Trim() != string.Empty)  
  18.                 culture = cookie.Value;  
  19.             else  
  20.                 culture = "en";  
  21.             fileName = string.Concat(fileName, "-", culture);  
  22.             var output = (HtmlString)System.Web.Optimization.Scripts  
  23.                 .Render(fileName);  
  24.             return output;  
  25.         }  
  26.     }  
  27. }  

In the aforesaid Steps 13 and 14, you have included the custom JavaScript bundle in the BundleConfig.cs file and created a custom HTML helper class to use that specified language bundle based on the user-selected language.

The Bundling and Minification concept is very helpful to reduce the network traffic and file size while loading the form since it minifies the multiple JavaScript files into one file and the file is being cached. If you change the file content then it automatically gets the fresh content with a new version number. So it helps to increase the page performance in all aspects.

Step 15

In this step, you update the “Home.cshtml” form design with the following design.



You are done with the multi-language MVC application implementation process with user language-specific JavaScript bundled minified files. So, build the application (F6) and hit the (F5) key to run the application with the following URL (http://localhost:49624/Demo/Index).

Enter the user name and select the preferred language as English then the form will be displayed like the following.


Now, click on the Submit button then it navigates to the home page like the following.


The preceding page is self-explanatory about the implemented functionality. In the similar way when you click on the "Add User" and "Delete User" buttons then it displays the alerts in the English language.

Step 16

In this step, again you navigate to this URL (http://localhost:49624/Demo/Index) and select the user language as French and check the respective outputs as shown below.

 

Now, click on the "Submit" button, then it navigates to the home page.
 

The preceding page is self-explanatory about the implemented functionality. In the similar way when you click on the "Add User" and "Delete User" buttons then it displays the alerts in the French language.

Conclusion
 
I hope this article provides an idea of the implementation of multilingual MVC application flow, creation of customized JavaScript bundles with translated alert messages based on user language, custom HTML helper classes, how to set user language in a cookie based on his login, Displaying Resource translation values in a form and how to maintain names and values in resource files.