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

发布于 2024-12-10 04:53:33 字数 85 浏览 0 评论 0原文

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

如果是的话,有没有相关的插件?如何进一步开发多语言应用程序?

I developed a web application and it is a dynamic site. Can I place a multilingual option for my site?

If yes, are there any plugins for that? How can I go further to develop a multilingual application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬花如无物 2024-12-17 04:53:33

我们有 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. 全球化架构
  2. [ASP.NET MVC 中使用的 jQuery 全球化技巧] 这在 ASP.NET 中也很有帮助。
  3. 我的问题定义了实现多语言代码的方式
  4. 如何创建和使用全球语言资源在 ASP.NET 中,

这些使您要使用的所有页面都从 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)
    {
    }
}

We have the DropdownList control. On its SelectedIndexChanged event, it postbacks and we then change the site culture, and it then loads the respective resource files...

DropDownList ASP.NET Code

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

Common class inherited by all of the web pages

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);
    }
}

Extension Method (just for sake of information)

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

Please review these link before starting to implement:

  1. Globalization Architecture
  2. [jQuery globalization tricks used in ASP.NET MVC] which can be helpful in ASP.NET also.
  3. My question here defines the way to implement multi-language code
  4. How to create and use Global language resources in ASP.NET

These make all of your pages to use inherit from the languagebase class as below:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default : languagebase
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文