Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to handle URLs in a multilingual site in ASP.net

I need to do a multilingual website, with urls like

www.domain.com/en/home.aspx for english
www.domain.com/es/home.aspx for spanish

In the past, I would set up two virtual directories in IIS, and then detect the URL in global.aspx and change the language according to the URL

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    Dim lang As String
    If HttpContext.Current.Request.Path.Contains("/en/") Then
        lang = "en"
    Else
        lang = "es"
    End If
    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang)
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang)


End Sub

The solution is more like a hack. I'm thinking about using Routing for a new website.

Do you know a better or more elegant way to do it?

edit: The question is about the URL handling, not about resources, etc.

like image 495
Eduardo Molteni Avatar asked Sep 17 '08 12:09

Eduardo Molteni


1 Answers

I decided to go with the new ASP.net Routing.
Why not urlRewriting? Because I don't want to change the clean URL that routing gives to you.

Here is the code:

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs on application startup
    RegisterRoutes(RouteTable.Routes)
End Sub


Public Sub RegisterRoutes(ByVal routes As RouteCollection)
    Dim reportRoute As Route
    Dim DefaultLang As String = "es"

    reportRoute = New Route("{lang}/{page}", New LangRouteHandler)
    '* if you want, you can contrain the values
    'reportRoute.Constraints = New RouteValueDictionary(New With {.lang = "[a-z]{2}"})
    reportRoute.Defaults = New RouteValueDictionary(New With {.lang = DefaultLang, .page = "home"})

    routes.Add(reportRoute)
End Sub

Then LangRouteHandler.vb class:

Public Class LangRouteHandler
     Implements IRouteHandler

  Public Function GetHttpHandler(ByVal requestContext As System.Web.Routing.RequestContext) As System.Web.IHttpHandler _
      Implements System.Web.Routing.IRouteHandler.GetHttpHandler

    'Fill the context with the route data, just in case some page needs it
    For Each value In requestContext.RouteData.Values
        HttpContext.Current.Items(value.Key) = value.Value
    Next

    Dim VirtualPath As String
    VirtualPath = "~/" + requestContext.RouteData.Values("page") + ".aspx"

    Dim redirectPage As IHttpHandler
    redirectPage = BuildManager.CreateInstanceFromVirtualPath(VirtualPath, GetType(Page))
    Return redirectPage

  End Function
End Class

Finally I use the default.aspx in the root to redirect to the default lang used in the browser list.
Maybe this can be done with the route.Defaults, but don't work inside Visual Studio (maybe it works in the server)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim DefaultLang As String = "es"
    Dim SupportedLangs As String() = {"en", "es"}
    Dim BrowserLang As String = Mid(Request.UserLanguages(0).ToString(), 1, 2).ToLower
    If SupportedLangs.Contains(BrowserLang) Then DefaultLang = BrowserLang

    Response.Redirect(DefaultLang + "/")
End Sub

Some sources:
* Mike Ormond's blog
* Chris Cavanagh’s Blog
* MSDN

like image 103
Eduardo Molteni Avatar answered Sep 17 '22 15:09

Eduardo Molteni