0

我为我的 WebApp 创建了 3 个选项习语。这个成语在所有页面的Buttons中都设置了。第一个选项习语是设置CurrentInfo。

我的问题是,我可以在其他页面中设置用户在主页中设置的这个成语?

例如,对于每个页面,在 Page_Load 我有这个配置:

Idioma.MudaCultura(CultureInfo.CurrentCulture.TwoLetterISOLanguageName.ToString());

主页中的我的按钮事件。(基本上所有页面都是一样的)

protected void es_OnChange(object sender, EventArgs e)
{
    Idioma.MudaCultura("es");
    lblWelcome.Text = Idioma.RetornaMensagem("welcome");
    btnRequestAccess.Text = Idioma.RetornaMensagem("btnRequestAccess");
    btnTickets.Text = Idioma.RetornaMensagem("btnTickets");
    btnManager.Text = Idioma.RetornaMensagem("btnManager");
    btnManageFolders.Text = Idioma.RetornaMensagem("btnManageFolders");
}

我的班级成语

public class Idioma
{
    private string chaveRM = "";

    public string ChaveRM
    {
        get { return chaveRM; }
        set { chaveRM = value; }
    }

    public static string RetornaMensagem(string NomeMensagem) 
    {
        ResourceManager resx = new ResourceManager(typeof(RM)); 
        return resx.GetString(NomeMensagem, System.Threading.Thread.CurrentThread.CurrentCulture); 
    } 

    public static void MudaCultura(string cultura)
    { 
        if (cultura=="pt") 
        { 
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("pt", true); 
        }

        else if(cultura=="en") 
        { 
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en", true); 
        }

        else if (cultura == "es")
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("es", true);
        }
    }
}

我需要的是设置CurrentInfo用户在主页中设置的选项,以及是否与此 CurrentInfo 不同。

例子:

主页:我的 CurrentInfo 是“en-US”。Home:我将我的成语改为“es-ES”。其他页面:我需要获取这个新的 CurrentInfo("es-ES")。

我清楚了吗?

4

2 回答 2

1

为了以专业的方式制作,您应该知道 asp 页面的层次结构,因为您知道任何 aspx 页面都从 System.Web.Pages 扩展,因此您必须创建一个名为 BasePage 的类,该类将从 Page 扩展,而您将覆盖一些默认功能,这些功能将在文化信息和货币等方面为您提供很多帮助。并使您的 aspx 页面从 BasePage 扩展

更改文化后需要重新加载页面才能使其生效

检查以下示例:

public class BasePage: Page
{ 
  public BasePage()
  {
    //
    // TODO: Add constructor logic here
    //


  }
  protected override void OnPreInit(EventArgs e)
  {
    Page.Theme = "Default";
    base.OnPreInit(e);
  }
  protected override void OnInit(EventArgs e)
  {
    base.OnInit(e);
  }
protected override void InitializeCulture()
{
    string CurrencySymbol = "";
    string lang = string.Empty;
    HttpCookie cookie = Request.Cookies["ddl_LanguageSwitcher"];

    if (cookie != null && cookie.Value != null)
    {
        lang = cookie.Value;
        CultureInfo eg = CultureInfo.CreateSpecificCulture(lang);
        IsArabic = (lang.IndexOf("ar-") >= 0);
        if (IsArabic)
        {
            CurrencySymbol = "جم";
        }
        else
        {
            CurrencySymbol = "EGP";
        }

        DateTimeFormatInfo di = new DateTimeFormatInfo();
        di.FullDateTimePattern = "dd/MM/yyyy HH:mm:ss";
        di.ShortDatePattern = "dd/MM/yyyy";
        eg.DateTimeFormat = di;
        NumberFormatInfo ni = new NumberFormatInfo();
        ni.CurrencySymbol = CurrencySymbol;
        ni.CurrencyDecimalDigits = 2;
        ni.NumberDecimalDigits = 2;
        //ni.CurrencyGroupSeparator = "-";
        ni.NumberGroupSeparator = "";
        eg.NumberFormat = ni;
        System.Threading.Thread.CurrentThread.CurrentUICulture = eg;
        System.Threading.Thread.CurrentThread.CurrentCulture = eg;
        _culture = Request.Cookies["ddl_LanguageSwitcher"].Value;
    }
    else
    {
        if (string.IsNullOrEmpty(lang)) lang = BL.Settings.DefaultLanguage;
        CultureInfo eg = CultureInfo.CreateSpecificCulture(lang);
        IsArabic = (lang.IndexOf("ar-") >= 0);
        if (IsArabic)
        {
            CurrencySymbol = "جم";
            //eg.NumberFormat.CurrencySymbol = "جم";
        }
        else
        {
            CurrencySymbol = "EGP";
            //eg.NumberFormat.CurrencySymbol = "EGP";
        }

        DateTimeFormatInfo di = new DateTimeFormatInfo();
        di.FullDateTimePattern = "dd/MM/yyyy HH:mm:ss";
        di.ShortDatePattern = "dd/MM/yyyy";
        eg.DateTimeFormat = di;

        NumberFormatInfo ni = new NumberFormatInfo();
        ni.CurrencySymbol = CurrencySymbol;
        ni.CurrencyDecimalDigits = 2;
        ni.NumberDecimalDigits = 2;
        //ni.CurrencyGroupSeparator = "-";
        ni.NumberGroupSeparator = "";
        eg.NumberFormat = ni;
        System.Threading.Thread.CurrentThread.CurrentUICulture = eg;
        System.Threading.Thread.CurrentThread.CurrentCulture = eg;
        HttpCookie cookie2 = new HttpCookie("ddl_LanguageSwitcher");
        cookie2.Value = lang;
        Response.SetCookie(cookie2);
        _culture = "ar-EG";
    }
    base.InitializeCulture();
}
}

我希望这可以帮到你

于 2013-08-09T12:18:08.193 回答
0

这不是完整的解决方案,但会向您展示如何从 Session 中设置/检索某些内容 - 如果您决定采用这种方式。

public string ChaveRM
{
    get 
    { 
        if (Session["chaveRM"] != null) 
            return Session["chaveRM"].ToString();

        return string.Empty;
    }
    set 
    { 
        Session["chaveRM"] = value; 
    }
}
于 2013-08-09T12:44:13.757 回答