Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

internationalization - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...