How to make asp.net mvc 5 application localized / translated

One of the thing I tried to is to make an working in other language other then English.

After trying a few thing I came up with a solution that only works by setting the desired language per page or view.

Suppose you want to see the phrase below in the Hebrew language, after clicking on it:

Click here to change the language to hebrew

So how do you achieve This magic? With a few steps

  1. Build a localization controller in a seperate file in your project, this controller will phrase the wanted language from the query string and return the language to set to the controller it came form.

    1. public class GlobalizationController : Controller
    2.  {
    3.      string pattern = @"(([?&]lang=\S*&)|([?&]lang=\S*$))";
    4.  
    5.      public ActionResult SetCulture(string url, string culture)
    6.      {
    7.          if (Url.IsLocalUrl(url))
    8.          {
    9.              Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
    10.              MatchCollection matches = rgx.Matches(url);
    11.              if (matches.Count > 0)
    12.              {
    13.                  string langValue = matches[0].Value.Substring(6);
    14.                  url = url.Replace(langValue, culture);
    15.                  return Redirect(url);
    16.              }
    17.              if (url.IndexOf('?') != -1)
    18.              {
    19.                  return Redirect(url + "&lang=" + culture);
    20.              }
    21.              return Redirect(url + "?lang=" + culture);
    22.          }
    23.          return RedirectToAction("Index", "Home");
    24.  
    25.      }
    26.  }
  2. ...

    Create 2 .resx files In your project

    ...

    one in the natural language (English)

    and the other in the secondary, my is Hebrew

  3. ...

    Open the secondary or the first language resource file and type the name and the value of what you want to translate

    The value must be the same for all the resource files

    ...

    Do the same in the other language but remember to use the same names

  4. The controller of the translated page must implement a way to get the translated language string (lang) from the localization controller from before

    The controller must also set the culture thread to the wanted language

    1. public ActionResult MakeAppLocalized(string lang = null)
    2.         {
    3.             if (lang != null)
    4.             {
    5.                 Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(lang);
    6.                 Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);
    7.             }
    8.             return View();
    9.         }
  5. In the view of this controller you can type the code that will access the wanted name form the resource that you want to translate

    In our case it is clickHereToSeeLocalization name

    Point to the Resource file by using razor

    Firt type you name space , my is AspDotNetMvcTraning

    Then to the directory of the resource , my is "Globalization"

    @{
        string translatedString = @AspDotNetMvcTraning.Globalization.Resource.clickHereToSeeLocalization;
    }
    @Html.ActionLink(translatedString, "SetCulture", "Globalization", new { url = Request.RawUrl, culture = "he-IL" }, null)
    

And that’s it , the page you want to translate will send the wanted language to the globalization controller and he will send back the phrased language to set

The controller that his view sent the translating request will get back the parsed language (lang) and he will set the culture thread to the wanted language