在C#.NET中切换语言对站点没有影响

时间:2015-07-22 05:59:22

标签: c# asp.net

我已经在我的ASP.NET C#中实现了多语言支持,然后是this教程,并将英语设置为我的默认语言,如下所示:

?lang=en is Default setting

切换到德语时没有任何反应: switched to german has no impact

在我的App_GlobalResource文件夹中我需要文件: de.language.resxen.language.resx

我的mls.cs文件(在名为BasePage.cs的教程中)包含以下代码:

public class mls : System.Web.UI.Page
{
    public void setLang() {
        InitializeCulture();
    }
    protected override void InitializeCulture()
    {
        if (!string.IsNullOrEmpty(Request["lang"]))
        {
            Session["lang"] = Request["lang"];
        }
        string lang = Convert.ToString(Session["lang"]);
        string culture = string.Empty;
         // In case, if you want to set vietnamese as default language, then removing this comment
        if (lang.ToLower().CompareTo("en") == 0 || string.IsNullOrEmpty(culture))
        {
            culture = "en-US";
        }
        if (lang.ToLower().CompareTo("en") == 0 || string.IsNullOrEmpty(culture))
        {
            culture = "en-US";
        }
        if (lang.ToLower().CompareTo("de") == 0)
        {
            culture = "de-AT";
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

        base.InitializeCulture();
    }
}

这是我的Login.aspx页面:

public partial class WebForm3 : mls 
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!string.IsNullOrEmpty(Convert.ToString(Session["lang"])))
        {
            if (Convert.ToString(Session["lang"]) == "en")
            {
                lbl_Debug.Text = "lang=en";
                Session["lang"] = null;
                Session["lang"] = "en";
            }
            else if(Convert.ToString(Session["lang"]) == "de")
            {
                lbl_Debug.Text = "lang=de";
                Session["lang"] = null;
                Session["lang"] = "de";
            }
        }
        else
        {
            lbl_Debug.Text = "nothing";
        }

    }
}

这是我的aspx代码:

<asp:Content ID="Content2" ContentPlaceHolderID="ph_RowMain" runat="server">
<div class="login-box">
    <div class="login-logo">
        <a href="Start.aspx"><b>
            <asp:Literal ID="lt_adminInterfaceHeader" runat="server" Text="<%$Resources:en.language, lt_adminHeader%>"></asp:Literal></b></a>
    </div>
    <!-- /.login-logo -->
    <div class="login-box-body">
        <p class="login-box-msg">
            <asp:Literal ID="lt_adminInterfaceBox" runat="server" Text="<%$Resources:en.language, lt_adminBox%>"></asp:Literal>
        </p>

        <div class="form-group has-feedback">
            <asp:TextBox ID="tb_email" runat="server" type="email" class="form-control" placeholder="<%$Resources:en.language,tb_email%>"></asp:TextBox>
            <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
        </div>
        <div class="form-group has-feedback">
            <asp:TextBox ID="tb_password" runat="server" type="password" class="form-control" placeholder="<%$Resources:en.language, tb_password%>"></asp:TextBox>
            <span class="glyphicon glyphicon-lock form-control-feedback"></span>
        </div>
        <div class="row">
            <div class="col-xs-12">
                <asp:Button ID="btn_signIn" runat="server" Text="<%$Resources:en.language, btn_signIn%>" type="submit" class="btn btn-primary btn-block btn-flat" />
            </div>
            <!-- /.col -->
        </div>
    </div>
    <!-- /.login-box-body -->
</div>
<!-- /.login-box -->

希望有人可以提供帮助。

2 个答案:

答案 0 :(得分:2)

您的文件名称错误。您有de.lanuage.rex但是,根据您提到的文章,它需要格式anyname.language.de.resx

答案 1 :(得分:0)

当您尝试本地化您的网站时,资源文件的名称很重要。 您的资源文件名称应遵循以下格式
 的 FileNameYouWant.language-culture.resx

如果您没有指定语言文化,即您的文件名是这样的 FileNameyouWant.resx 然后默认采用英语(language-culture =&#39; en&#39;)。

示例:德语语言资源文件名= FileNameYouWant.de.resx
法语语言资源文件名= FileNameYouWant.fr.resx