本地化资源不起作用

时间:2017-02-08 15:47:06

标签: c# asp.net webforms

我在Visual Studio上的ASP.NET Webforms上的本地化资源有问题。我的网站只加载中性文化。我有一个中性和一个fr-CA本地化文件。即使我更改了CurrentUICulture(或CurrentCulture),它也无法正常工作。我在谷歌搜索,但我什么都没发现。请注意,我自动生成资源(使用工具>生成本地资源)。

请注意,我的目标是在网站上动态更改语言(通过页面上的链接切换到法语和英语)。

我的项目结构:

Image

ASP



<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="_layout.master.cs" Inherits="ChoixResto2.Shared._layout" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <link type="text/css" rel="stylesheet" href="~/Content/resto.css" />
</head>

<body runat="server">
    <h2>Bienvenue sur restoSondage,
        <asp:Label ID="lblPrenom" runat="server"></asp:Label></h2>
        <div id="menu">
            <ul>
                <%if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
                  { %>
                <li><a href="../Login/Index.aspx">Connexion</a></li>
                <li><a href="../Login/creerCompte.aspx">Créer un compte</a></li>
                <%}
                  else
                  { %>
                <li><a href="../Login/Index.aspx?action=deconecte">DÉCONNEXION</a></li>
                <li><a href="../Sondage/gestionSondage.aspx">Principal</a></li>
                <li><a href="../Restaurant/listeRestaurants.aspx">Gestion des restos</a></li>
                <li><a ID="lnkLangue" runat="server" onserverclick="lnkLangue_ServerClick"><asp:Label ID="lblLangue" runat="server" /></a></li>
                <%} %>
            </ul>
        </div>
        <br />
        <br />
    <asp:ContentPlaceHolder ID="body" runat="server">
    </asp:ContentPlaceHolder>
    <br />


    © 2017 - Site de sondage pour apprendre le ASP.NET MVC. Pour toutes plaintes, veuillez nous les écrire sur <a href="http://www.cleverbot.com">notre site web</a>.
</body>
</html>
&#13;
&#13;
&#13;

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using System.Diagnostics;
using ChoixResto2.Classes;

namespace ChoixResto2.Shared
{
    public partial class _layout : System.Web.UI.MasterPage
    {
        public const string LANG_FR = "Français";
        public const string LANG_EN = "English";

        protected void Page_Load(object sender, EventArgs e)
        {
            int? id = Common.getAuthentificationCookie();

            if (id.HasValue)
                lblPrenom.Text = Utilisateur.getPseudo((int)id);

            if (Session["lang"] == null)
                Session["lang"] = "fr-CA";


            if (!IsPostBack)
            {
                if (CultureInfo.CurrentUICulture.Name.Equals("fr-CA"))
                    lblLangue.Text = LANG_EN;
                else
                    lblLangue.Text = LANG_FR;
            }

            Debug.WriteLine("LAYOUT culture : " + Thread.CurrentThread.CurrentCulture.Name);
            Debug.WriteLine("LAYOUT Uiculture : " + Thread.CurrentThread.CurrentUICulture.Name);
        } 

        protected void lnkLangue_ServerClick(object sender, EventArgs e)
        {
            Debug.WriteLine("Dans le lnkLangage");
            if (Session["lang"].ToString().Equals("en-CA"))
            {
                Session["lang"] = "fr-CA";
                lblLangue.Text = LANG_EN;
            }
            else
            {
                Session["lang"] = "en-CA";
                lblLangue.Text = LANG_FR;
            }

            Common.setCulture(Session["lang"].ToString());
        }
    }
}

GestionSondage

ASP

&#13;
&#13;
<%@ Page Title="" Language="C#" MasterPageFile="~/Shared/_layout.Master" AutoEventWireup="true" CodeBehind="gestionSondage.aspx.cs" Inherits="ChoixResto2.Site.Sondage.gestionSondage" meta:resourcekey="PageResource1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="body" runat="server">
        <form runat="server">
        <h3><asp:Label ID="lblDisPretChoisir" runat="server" meta:resourcekey="lblDisPretChoisirResource1" text="Prêt à choisir un resto?"/></h3>
        <asp:DropDownList ID="ddlSondages" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlSondages_SelectedItemChanged" meta:resourcekey="ddlSondagesResource1"> </asp:DropDownList>
        <asp:Calendar ID="calFiltre" runat="server" OnSelectionChanged="calFiltre_SelectionChanged"></asp:Calendar>

        <table border="1">
            <tr>
                <td><asp:Label ID="lblDisDate" runat="server" meta:resourcekey="lblDisDateResource1" /> <asp:Label ID="lblDate" runat="server" meta:resourcekey="lblDateResource1" /></td>
                <td><asp:Label ID="lblDisHeure" runat="server" meta:resourcekey="lblDisHeureResource1" /> <asp:Label ID="lblHeure" runat="server" meta:resourcekey="lblHeureResource1" /></td>
            </tr>
        </table>

        <asp:Button ID="btnVoter" runat="server" OnClick="btnVoter_Click" meta:resourcekey="btnVoterResource1"/>
        

        <hr />
        <asp:Label ID="lblNomSondage"  runat="server" Text="Nom du sondage" meta:resourcekey="lblNomSondageResource1" />
        <asp:TextBox ID="txtNom" runat="server" meta:resourcekey="txtNomResource1" /> <br />

        <asp:Button ID="btnCreerSondage" runat="server" Text="Créer un sondage" OnClick="btnCreerSondage_Click" meta:resourcekey="btnCreerSondageResource1" /> <br />
        <br />

        <asp:Label ID="lblErrSondage" runat="server" ForeColor="Red" meta:resourcekey="lblErrSondageResource1" />
    </form>
</asp:Content>
&#13;
&#13;
&#13;

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using ChoixResto2.Classes;
using ChoixResto2.Data;
using System.Diagnostics;
using System.Resources;
using System.Threading;
using System.Globalization;

namespace ChoixResto2.Site.Sondage
{
    public partial class gestionSondage : System.Web.UI.Page
    {
        public string MODE_AFFICHAGE = "Afficher les résultats";
        public string MODE_VOTE = "Voter";
        protected void Page_Load(object sender, EventArgs e)
        {
            bd.SondageDataTable dt;
            ListItem li;

            if (Session["lang"] == null)
                Session["lang"] = "fr-CA";

            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
                Response.Redirect("~/Site/Login/Index.aspx");

            if (!IsPostBack)
            {
                dt = Classes.Sondage.getSondage();

                foreach (bd.SondageRow dr in dt)
                {
                    li = new ListItem(dr["nom"].ToString(), dr["idSondage"].ToString());
                    ddlSondages.Items.Add(li);
                }

                ddlSondages_SelectedItemChanged(null, null);
            }
            else
            {
                Debug.WriteLine("GestionSondage ispostback");

                if (calFiltre.SelectedDate.Date == DateTime.MinValue.Date)
                    Debug.WriteLine("Cal non sélectionné");
                else
                    Debug.WriteLine("cal Sélectionné");
            }

            Common.setCulture(Session["lang"].ToString());

            Debug.WriteLine("culture : " + Thread.CurrentThread.CurrentCulture.Name);
            Debug.WriteLine("Uiculture : " + Thread.CurrentThread.CurrentUICulture.Name);
        }

        //Click sur créer sondage
        protected void btnCreerSondage_Click(object sender, EventArgs e)
        {
            int id;
            if (!string.IsNullOrWhiteSpace(txtNom.Text))
            {
                id = Classes.Sondage.creerSondage(txtNom.Text);

                Response.Redirect("~/Site/Sondage/Vote.aspx?id=" + id.ToString());
            }
            else
                lblErrSondage.Text = "Erreur : Vous devez entrer un nom de sondage.";
        }

        protected void ddlSondages_SelectedItemChanged(object sender, EventArgs e)
        {
            bd.SondageRow dr;
            if (Classes.Vote.isVoteIsPresent(int.Parse(ddlSondages.SelectedValue), (int)Common.getAuthentificationCookie()))
                btnVoter.Text = MODE_AFFICHAGE;
            else 
                btnVoter.Text = MODE_VOTE;

            dr = Classes.Sondage.getSondage(int.Parse(ddlSondages.SelectedValue));

            lblDate.Text = ((DateTime)dr["date"]).ToLongDateString();
            lblHeure.Text = ((DateTime)dr["date"]).ToShortTimeString();
        }

        //click sur btnVoter
        protected void btnVoter_Click(object sender, EventArgs e)
        {
            if (btnVoter.Text.Equals(MODE_VOTE))
                Response.Redirect("~/Site/Sondage/Vote.aspx?id=" + ddlSondages.SelectedValue);
            else
                Response.Redirect("~/Site/Sondage/listeResultat.aspx?id=" + ddlSondages.SelectedValue);
        }

        protected void calFiltre_SelectionChanged(object sender, EventArgs e)
        {
            ddlSondages.Items.Clear();
        }      
    }
}

Common.setCulture

public static void setCulture(string culture)
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
            Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
        }     

2 个答案:

答案 0 :(得分:0)

您只有gestionSondage.aspx.fr-CA.resx和默认(en-US)gestionSondage.aspx.resx文件。我认为这就是本地化不起作用的原因。您需要gestionSondage.aspx。{culturecode} .resx文件并将基于文化的字符串放入其中,然后本地化可以显示基于文化的字符串。

答案 1 :(得分:0)


感谢您的答复。我按你所说,但没有成功。以前,我的页面只有英文版,现在只有法文版。

事实上,我不明白,这是资源不起作用,但当我改用法语到英语时,引用了迄今为止的依据......

只需查看我页面上的日历:

French Version
English Version

同样,如果它可以帮助,这是gestionSondage.aspx.en-CA.resx的XML内容(因为我不能发布超过2个图像)(fr-CA是同样的东西,但是在法语中) )。

<?xml version="1.0" encoding="utf-8"?>
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
    <xsd:element name="root" msdata:IsDataSet="true">
      <xsd:complexType>
        <xsd:choice maxOccurs="unbounded">
          <xsd:element name="metadata">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" />
              </xsd:sequence>
              <xsd:attribute name="name" use="required" type="xsd:string" />
              <xsd:attribute name="type" type="xsd:string" />
              <xsd:attribute name="mimetype" type="xsd:string" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="assembly">
            <xsd:complexType>
              <xsd:attribute name="alias" type="xsd:string" />
              <xsd:attribute name="name" type="xsd:string" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="data">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="resheader">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" />
            </xsd:complexType>
          </xsd:element>
        </xsd:choice>
      </xsd:complexType>
    </xsd:element>
  </xsd:schema>
  <resheader name="resmimetype">
    <value>text/microsoft-resx</value>
  </resheader>
  <resheader name="version">
    <value>2.0</value>
  </resheader>
  <resheader name="reader">
    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=en-CA, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <resheader name="writer">
    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=en-CA, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <data name="btnCreerSondageResource1.Text" xml:space="preserve">
    <value>Create survey</value>
  </data>
  <data name="lblDisDateResource1.Text" xml:space="preserve">
    <value>Creation date :</value>
  </data>
  <data name="lblDisHeureResource1.Text" xml:space="preserve">
    <value>Creation time :</value>
  </data>
  <data name="lblDisPretChoisirResource1.Text" xml:space="preserve">
    <value>Ready to choose a restaurant?</value>
  </data>
  <data name="lblNomSondageResource1.Text" xml:space="preserve">
    <value>Survey name</value>
  </data>
</root>