Localization in asp.net mvc
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 :-|
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?
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
精彩评论