Nezir Zahirović



In these days I had  request to make a new MVC web project which need to have localization on two different languages.

After spending some time by goggling on this topic and after trying few  solution I found what I am looking for (Thanks to Alex Adamyan).  Fast & Clean & Easy solution for localizing ASP MVC project.

1. I create basic Asp Mvc project (Visual Studio 2013 4 CTP,  MVC 5.2)
2. Added "res"  folder to project.
3. Added 2 Resource files in "res" folder Resource.resx &
4. Added two strings (Title & Text) into both resources files one for default english and one for bosnian language.
5.Important thing is that you need to set Access modifier on these two Resources files to "Public"
6.Also in properties of both files Custom Tool property add  "PublicResXFileCodeGenerator" value, which is required for compiling.
7. You can set any name in Custom Tool Namespace property I set "ViewRes"
8. In my Index.cshtml page I  place my res variables:

    <h1>@ViewRes.Resource.Title </h1>
    <p class="lead">@ViewRes.Resource.Text</p>

9. Inside _Layout.cshtml I add two menu Item:

                    <li>@Html.ActionLink("English", "ChangeCulture", "Home", new { lang = "en", returnUrl = this.Request.RawUrl }, null)</li>
                    <li>@Html.ActionLink("Bosanski", "ChangeCulture", "Home", new { lang = "ba", returnUrl = this.Request.RawUrl }, null)</li>

10. Inside HomeController add new ActionResult:

         public ActionResult ChangeCulture(string lang, string returnUrl)
            Session["Culture"] = new CultureInfo(lang);
                return Redirect(returnUrl);

11. In Global.asax added:

        protected void Application_AcquireRequestState(object sender, EventArgs e)
                //It's important to check whether session object is ready
              if (HttpContext.Current.Session != null)
                  CultureInfo ci = (CultureInfo)this.Session["Culture"];
                  //Checking first if there is no value in session 
                  //and set default language 
                  //this can happen for first user's request
                 if (ci == null)
                     //Sets default culture to english invariant
                     string langName = "en";
                     //Try to get values from Accept lang HTTP header
                     if (HttpContext.Current.Request.UserLanguages != null && HttpContext.Current.Request.UserLanguages.Length != 0)
                         //Gets accepted list 
                         langName = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2);
                     ci = new CultureInfo(langName);
                     this.Session["Culture"] = ci;
                 //Finally setting culture for each request
                 Thread.CurrentThread.CurrentUICulture = ci;
                 Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);

Sample of test asp mvc 5.2 localization project with two languages.

Creating "res" folder and adding Resources files and setting properties.

 Setting Access modifier in Resource files and adding some strings and values.

 Setting resource keys inside Index.cshtml file.

 Adding menu item for languages inside _Layout.cshtml file.

Adding ActionResult inside HomeController.

Finally adding code inside Global.asax for detecting current language option.

    Blogger Comment
    Facebook Comment


Post a Comment