0
\$\begingroup\$

What is the best way to make an ASP.NET MVC multi language website?

The following code is what I use, but I need to make sure from your real-world experience that my code is the best way for performance to set language in the Application_AcquireRequestState method because I noticed that the method get is called twice in every request.

RouteConfig:

public static void RegisterRoutes(RouteCollection routes)
{
 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 routes.MapRoute(
 name: "Default",
 url: "{language}/{controller}/{action}/{id}",
 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, language = "en-US" }
 );
}

Global.asax:

protected void Application_AcquireRequestState(Object sender, EventArgs e)
{
 if (HttpContext.Current.Request.RequestContext.RouteData.Values.ContainsKey("language"))
 {
 var language = (string)HttpContext.Current.Request.RequestContext.RouteData.Values["language"];
 if (language != null)
 {
 Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
 Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
 }
 }
}

Here are the action links that I use to pass the language parameter:

 @Html.ActionLink("English", ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { language = "en-US" }, null)
 @Html.ActionLink("French", ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(),new { language = "fr-FR" },null)
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked May 12, 2017 at 21:49
\$\endgroup\$
1
  • \$\begingroup\$ I would recommend using resource files. You can handle user specified languages, as well as the browser languages, without having to use multiple views for each. \$\endgroup\$ Commented Mar 2, 2018 at 16:19

1 Answer 1

2
\$\begingroup\$

I see the solution you proposed is very good, one small thing I want to recommend here, which is to validate the language value is a valid culture before using it in the Global.asax to avoid the application exception if someone manually modified the value on the URL to invalid culture value. In addition, you can simplify the URL to only use EN/FR instead of full culture code "en-US".

In our application, because we support Arabic language which requires to render the application from Right to Left, we found it is better to load totally different views for each culture, not just the resource files. So we changed the views folder to have EN & AR folders, each folder has its localized views, then customized the MVC view engine to dynamically load the right view based on the routing data.

answered May 19, 2017 at 20:02
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.