views:

291

answers:

3

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?

A: 

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

sashaeve
A: 

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

lakhlaniprashant.blogspot.com
+1  A: 

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)

Eduardo Molteni
+1 - Very nice answer. The global.asax is definitely the right place to put the essential Culture information.
Mark Brittingham