Click here to Skip to main content
15,878,945 members
Articles / Programming Languages / C#

Localize Your MVC App Based on a Subdomain

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
12 Apr 2013CPOL2 min read 17.7K   8   5
How to localize your MVC app based on a subdomain

Having an application in multiple languages is now a requirement in many projects. In ASP.NET, you can tell your application that the language that should be using corresponds to the one the browser is specifying. While this is a really nice feature in the ideal scenarios (since the user gets the applications in the proper language automatically), there are some scenarios where this might not be the expected behavior like:

  • If your user's computer locale is different than the one he or she prefers for using your application (like when he or she is using a different computer than his/her own)
  • When the browser settings have been modified to some value different than whatever the user prefers and he or she does not have the knowledge to adjust this setting on the browser.

In these cases, the user would rather to have a "fallback" mechanism so he or she can select his/her preferred language. One of the options you can use to achieve this is selecting the language/locale based on a subdomain. By this, you will give the users the following options:

Desired language URL address
English en.myapp.com
Spanish sp.myapp.com
Finnish fi.myapp.com

In order to support this, you will need to create an ActionFilterAttribute, something like this:

C#
public class LocalizationFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var locales = new Dictionary<string>();

            locales.Add("mx", "es-MX");
            locales.Add("sp", "es-ES");
            locales.Add("vi", "vi-VN");
            locales.Add("fi", "fi-FI");

            var subdomain = GetSubDomain();

            if (subdomain != string.Empty && locales.ContainsKey(subdomain))
            {
                Thread.CurrentThread.CurrentCulture = 
                       new System.Globalization.CultureInfo(locales[subdomain]);
                Thread.CurrentThread.CurrentUICulture = 
                       new System.Globalization.CultureInfo(locales[subdomain]);

                HttpContext.Current.Response.Write
                (String.Format("Culture: {0}", Thread.CurrentThread.CurrentCulture.Name));
            }
            else
            {
                HttpContext.Current.Response.Write("Culture: Default ");
            }
            base.OnActionExecuting(filterContext);
        }
        private string GetSubDomain()
        {
            var url = HttpContext.Current.Request.Headers["HOST"];
            var index = url.IndexOf(".");

            if (index < 0)
            {
                return string.Empty;
            }

            var subdomain = url.Split('.')[0];
            if (subdomain == "www" || subdomain == "localhost")
            {
                return string.Empty;
            }

            return subdomain;
        }
    }

As you may have already noticed, with this code, you define a list of locales that will be selected according to the provided subdomain. The next step would be registering this filter so it is used in all the views. You can do this in your Global.asax file:

C#
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new LocalizationFilterAttribute());
            filters.Add(new HandleErrorAttribute());
        }

Once you have a way to set the locale for the current thread, all you need to do is the localization process, which can be done as you already have it. In my case, I'm using resource files to have all the translations and have a fallback resource file if any requested text has no translation on any of the language-specific resource files.

By this, you can provide your users a simple and easy-to-remember way to get your application in their desired language.

This article was originally posted at http://pollirrata.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer e-nnovare
Mexico Mexico
Jorge is an enthusiast web developer who love to learn something new every day.

Comments and Discussions

 
QuestionSUBDOMAIN ROUTING Pin
Member 839529728-Oct-13 19:46
Member 839529728-Oct-13 19:46 
SuggestionReally nice solution Pin
Luiz Bicalho15-Apr-13 8:52
Luiz Bicalho15-Apr-13 8:52 
GeneralRe: Really nice solution Pin
pollirrata19-Apr-13 10:40
pollirrata19-Apr-13 10:40 
GeneralRe: Really nice solution Pin
Luiz Bicalho6-May-13 7:27
Luiz Bicalho6-May-13 7:27 
GeneralMy vote of 5 Pin
gicalle7515-Apr-13 1:02
professionalgicalle7515-Apr-13 1:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.