Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get subdomain in MVC for internationalization

I am working on an MVC site that will have multiple translations. We are looking to do that through subdomains like http://en.domain.com or http://fr.domain.com. We also want to support the regular domain http://domain.com.

The translations are working provided that you change the subdomain manually, but I'm looking for a way to automate this and maintain the entire current URL to allow a user who finds http://en.domain.com/product to click the link and get another language version of the same page. It seems simple to just isolate the subdomain if it exists, strip it from the current url, and replace with the language version specified.

In essence:

http://en.domain.com/product (original)

http://domain.com/product (cleaned)

http://fr.domain.com/product or http://de.domain.com/product etc... (output)

I started out looking for built in functionality like Request.Url.Subdomain but have concluded there is no such magical creature. I then moved on to basic string manipulation, but it seemed really convuluted so I set off to find a regex solution.

I've tested this regex with some online regex testers that normally work for me and they are identifying correctly the subdomain when it exists, but fails to find a result when the code actually runs.

I only use regular expressions a little and I'm hoping there is something really obvious I'm doing wrong here. If there is a better solution I'm open to other implimentations.

C#

string url = Request.Url.AbsoluteUri; //http://en.domain.com/
Regex regex = new Regex(@"/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/", RegexOptions.IgnoreCase);
GroupCollection results = regex.Match(url).Groups;
Group result = results[0];

Here is the solution I have for now. Not as elegant as I would like, but for something that ate up way too much time it is now working as intended.

View

<a href="@Html.Action("ChangeLanguage", new { lang = "en" })">English</a>
<a href="@Html.Action("ChangeLanguage", new { lang = "fr" })">French</a>

Action

    public string ChangeLanguage(string controller, string lang)
    {
        string url = Request.Url.AbsoluteUri;

        Regex regex = new Regex(@"(?:https*://)?.*?\.(?=[^/]*\..{2,5})", RegexOptions.IgnoreCase);
        GroupCollection results = regex.Match(url).Groups;
        Group result = results[0];
        if (result.Success)
        {
            string[] resultParts = result.Value.Split('/');
            string newSubDomain = resultParts[0] + "//" + lang + ".";
            url = url.Replace(result.Value, newSubDomain);
        }
        else
        {
            string[] urlParts = url.Split('/');
            string oldParts = urlParts[0] + "//";
            string newParts = urlParts[0] + "//" + lang + ".";
            url = url.Replace(oldParts, newParts);
        }


        return url;
    }
like image 517
Joshua Morgan Avatar asked Dec 04 '15 21:12

Joshua Morgan


1 Answers

You can use custom routing to make it simple

routes.Add("LanguageRoute", new DomainRoute( 
"{language}.example.com/{controller}/{action}", // Domain with parameters 
"{controller}/{action}/{id}",    // URL with parameters 
new { controller = "Home", action = "Index", id = "" }  // Parameter defaults 

))

And get the language value on controller

  public ActionResult Index(string language)
    {
        return View();
    }

a useful link may help to you: http://benjii.me/2015/02/subdomain-routing-in-asp-net-mvc/

like image 70
c0demaster Avatar answered Oct 08 '22 09:10

c0demaster