在不同的子域名支持多语言网站的最佳实践 [英] Best practices for multi language site across different sub domains

查看:574
本文介绍了在不同的子域名支持多语言网站的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待建立一个ASP.NET网站。我需要它在法语和英语同域设置像这样:

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

en.mysite.com fr.mysite.com

我不希望重复code或必须将文件上传到如果可能的话两个域。

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

这将是理想的对www.mydomain.com的所有文件,然后使用资源文件翻译进行排序。

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

什么是ASP.NET中对此进行设置的最佳方式?

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

推荐答案

我的解决办法是在的Global.asax <设定郎在的BeginRequest / code>

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

不要忘了当用户点击www.mysite.com设置一个重定向,使用用户的浏览器语言preference

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

作为一个方面来看,我建议你使用 http://www.mysite.com/en,因为它是从SEO的角度来看更好(如果你的网站很重要)

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)

这篇关于在不同的子域名支持多语言网站的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆