0

Friends,

I'm working in asp.net mvc 2.0 and I'm stuck at the stage where I want to translate the site content (menu items, labels etc.) into predefined languages selected from the drop-down list. I want it to perform through asynchronous request (if possible). I have no prior experience in implementing globalization/localization in either web forms or asp.net mvc. So, few useful pointers (for beginners) are something what I require to accomplish this task at this stage.

Please help me out :(

Thanks in advance :-|

user383698
  • 85
  • 1
  • 3
  • 16

2 Answers2

0

I don't understand why you would ever use Ajax to localize an entire page (menus, labels, error messages, etc...) making all the ajax requests to fully localize a page will take longer than reloading the page with the correct culture and having asp.net do the localization for you.

That being said there are many ways to localize in asp.net mvc. It is not as simple as Webforms but there are some good blog posts and even some code you can copy to help you out.

check this out for the specifics How to localize ASP.NET MVC application?

Community
  • 1
  • 1
Sruly
  • 10,200
  • 6
  • 34
  • 39
  • Thanks for your reply Sruly ! I have a reason behind why I want localization behavior asynchronous. The pages I'm working on has most of the content already translated which comes from the database. This translated content is in different translations and is predefined into the database. I do not want this content to get overlapped with full page localization, but only labels etc. – user383698 Jul 30 '10 at 10:29
  • Then I guess you will have to implement something on your own. I would recommend making an action that returns collections of strings for all the labels. (JsonResult) You can store the strings in .resx files or in the DB. – Sruly Jul 31 '10 at 21:50
0

Globalization :The process of designing and developing that works across multiple cultures / locales. Localization :The process of customizing a particular language. ie, which is easy to use in the target country.

Open your Global.asax and put this code

           protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies["Language"];
        if (cookie != null && cookie.Value != null)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cookie.Value);
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cookie.Value);
        }
        else
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en");
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
        }
    }

Open your controller and put this code

  public ActionResult ChangeLanguage(string lan)
    {

        if (lan != null)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lan);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(lan);
            var cookie = new HttpCookie("Language");
            cookie.Value = lan;
            Response.Cookies.Add(cookie);
        }
        return RedirectToAction("Index","Home");
    }

This is your change language function..

open your view page and add this type of html code

<h1>Localization Demo Project</h1>

@Html.ActionLink("Local Language", "ChangeLanguage", "Home", new { selectedlanguage = "ne" }, new { @class = "btn btn-default" })
@Html.ActionLink("English Language", "ChangeLanguage", "Home", new { selectedlanguage = "en" }, new { @class = "btn btn-default" })

<div class="row">
    <label>@LocalizationDemo.Language.Localization.First_name</label>
    <br />
    <label>@LocalizationDemo.Language.Localization.Last_name</label>
    <br />
    <label>@LocalizationDemo.Language.Localization.Address</label>
</div>

Fore more details Step by step Click this link

http://www.findandsolve.com/articles/localization-in-asp.net-mvc-razor-step-by-step

Sundar
  • 142
  • 1
  • 15