多语言应用程序,Microsoft Visual Studio?

时间:2011-10-18 04:34:51

标签: .net asp.net multilingual

我开发了一个Web应用程序,它是一个动态站点。我可以为我的网站添加多语言选项吗?

如果是,是否有任何插件?如何进一步开发多语言应用程序?

1 个答案:

答案 0 :(得分:0)

我们有DropdownList控件。在其SelectedIndexChanged事件中,它回发,然后我们更改网站文化,然后加载相应的资源文件......

DropDownList ASP.NET代码

<asp:DropDownList ID="ddlLanguage" runat="server" CssClass="select-language" AutoPostBack="true">
    <asp:ListItem Value="en-US" Text="English" title="/images/Flag_USA.gif"></asp:ListItem>
    <asp:ListItem Value="it-IT" Text="Italiano" title="/images/Flag_Italian.gif"></asp:ListItem>
    <asp:ListItem Value="fr-FR" Text="Française" title="/images/Flag_French.gif"></asp:ListItem>
</asp:DropDownList>

所有网页继承的公共类

using System;
using System.Web;
using System.Threading;
using System.Globalization;

public class languagebase : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        try
        {
            string LanguageCode = Request["ctl00$ucMenu$ddlLanguage"]; // Language Drop Down Control in Front End
            if (!LanguageCode.IsNullOrEmpty())
            {
                setCulture(LanguageCode);  // Set Culture language from drop down
                Request.Cookies["LanguageCode"].Value = LanguageCode; // Update REQUEST Cookie language from drop down
                SetCookies(LanguageCode); // Set Cookie language from drop down
            }
        }
        catch(Exception ex)
        {
            setCulture("en-US"); // Set default language
            Request.Cookies["LanguageCode"].Value = "en-US"; // Update REQUEST Cookie language to default
            SetCookies("en-US"); // Set default language
        }
        base.InitializeCulture();
    }

    private static void setCulture(string LanguageValue)
    {
       Thread.CurrentThread.CurrentUICulture = new CultureInfo(LanguageValue);
       Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(LanguageValue);
    }

    public void SetCookies(string strLanguage)
    {
        System.Web.HttpContext.Current.Response.Cookies["LanguageCode"].Value = strLanguage;
        System.Web.HttpContext.Current.Response.Cookies["LanguageCode"].Expires = DateTime.Now.AddDays(15);
    }
}

扩展方法(仅供参考)

public static Boolean IsNullOrEmpty(this String original)
{
    return string.IsNullOrEmpty(original);
}

请在开始实施前查看以下链接:

  1. Globalization Architecture
  2. [ASP.NET MVC中使用的jQuery全球化技巧]也可以在ASP.NET中提供帮助。
  3. My question here defines the way to implement multi-language code
  4. How to create and use Global language resources in ASP.NET
  5. 这些会使您使用的所有网页都继承自languagebase类,如下所示:

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Default : languagebase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }