Get a subdomain in MVC for internationalization

I am working on an MVC site that will have multiple translations. We want to do this through subdomains such as http://en.domain.com or http://fr.domain.com . We also want to maintain a regular domain of http://domain.com .

Translations work 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 the user who finds http://en.domain.com/product to click on the link and get a different language version the same page. It seems simple to simply highlight the subdomain, if it exists, separate it from the current URL and replace the specified version of the language.

Essentially:

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

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

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

I started looking for built-in functions like Request.Url.Subdomain , but came to the conclusion that there is no such magical creature. Then I moved on to basic string manipulation, but it seemed really confusing, so I went off to look for a solution to regular expressions.

I tested this regex with some online testers that usually work for me, and they correctly identify the subdomain when it exists, but cannot find the result when the code really works.

I use regular expressions a bit and I hope there is something really obvious that I'm doing wrong here. If there is a better solution, I am open to other implications.

WITH#

 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 now. Not as elegant as we would like, but for something that ate too much time, now it works as intended.

View

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

Act

  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; } 
+5
source share
3 answers

You can use custom routing to simplify it.

 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 the controller

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

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

+3
source

Use something like the following (psuedocode - should add security checks, etc.):

 Uri myHost = new Uri("https://en.mydomain.com"); string hostname = myHost.Host; // returns en.mydomain.com string subdomain = string.split(".", hostname)[0]; // subdomain = "en" 

This will get the host name, which you can then split into ".". to the array and take the first element.

EDIT: link to MSDN docs on Uri.Host

https://msdn.microsoft.com/en-us/library/system.uri.host(v=vs.110).aspx

+2
source

We do something similar in one of my projects, and that’s how I get the current subdomain:

 string GetSubDomain(Uri url, string defaultValue) { string subdomain = defaultValue; if (url.HostNameType == UriHostNameType.Dns) { string host = url.Host; if (host.Split('.').Length > 2) { int index = host.IndexOf("."); int lastIndex = host.LastIndexOf("."); subdomain = index.Equals(lastIndex) ? defaultValue : host.Substring(0, index); } } return subdomain; } 

You would use it like this:

 var subdomain = GetSubDomain(HttpContext.Current.Request.Url, "en"); 

It is assumed that you want to get only the first subdomain in the current URL, so http://fr.example.com and http://fr.something.example.com will give fr , while http://example.com will give en (the default value in this example).

0
source

Source: https://habr.com/ru/post/1237430/


All Articles