Best practices for multi language site across different sub domains

☆樱花仙子☆ 提交于 2019-12-19 08:33:44

问题


I am looking to build a site in ASP.NET. I need it to be in french and english with the domains setup like so:

en.mysite.com fr.mysite.com

I do not want to duplicate code or have to upload the files to both domains if possible.

It would be ideal to have all the files on www.mydomain.com and then use resource files to sort the translations.

What is the best way to set this up in ASP.NET?


回答1:


My solution was setting the lang in the BeginRequest in global.asax

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    Dim lang As String = "es" ''//default
    If Request.Url.ToString.ToLower.StartsWith("http://es.") 
        lang = "es"
    ElseIf Request.Url.ToString.ToLower.StartsWith("http://en.") Then
        lang = "en"
    End If
    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang)
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang)

    Site.Idioma = lang ''//static variable that I use in other parts of the site
End Sub

Do not forget to set a redirect when the user hits www.mysite.com, using the user's browser language preference

Imports System.Globalization
Partial Class redirect_Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal s As Object, ByVal e As System.EventArgs) _
                                                                 Handles Me.Load

        Select Case Mid(Request.UserLanguages(0).ToString(), 1, 2).ToLower
            Case "en"
                Response.Redirect("http://en.mysite.com")
            Case Else
                Response.Redirect("http://es.mysite.com")
        End Select

    End Sub
End Class

As a side point, I recommend you to use http://www.mysite.com/en because it is better from the SEO perspective (If it is important to your site)




回答2:


As I understand correctly you want to place all files in the root directory but use subdomains for different languages.

I think that en.mysite.com and fr.mysite.com must be just aliases and should tell asp.net application what language to use. You can change culture settings with code. It is well-described here.

But from my point of view it is a better way to provide language settings on the main domain with some default language and ability to swith between languages. And if user advice to change language - he will just click one link. Language settings шт this case can ne stored anywhere (user profile, cookie, session, database if registered user etc.).




回答3:


If I'd be implementing this, I'd do this: map the en.mysie.com to www.mysite.com/page?lang=en map the fr.mysite.com to www.mysite.com/page?lang=fr

and then in master page, set your language according to the param using globalization, and asp.net pages will automatically served in that language if they find correct app_localresource file with the same language code.

Hope this helps



来源:https://stackoverflow.com/questions/2272748/best-practices-for-multi-language-site-across-different-sub-domains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!