[英]Change CultureInfo c#
I created 3 options idioms to my WebApp. 我为WebApp创建了3个选项惯用法。 This idiom are set in Buttons in all pages.
此惯用语在所有页面的“按钮”中设置。 The first option idiom is set CurrentInfo.
第一个选项惯用法设置为CurrentInfo。
My question is, who I can set in others pages this Idiom that user set in Homepage? 我的问题是,该用户在首页中设置的这个成语可以在其他页面中设置?
for example, to each page, in Page_Load I have this config: 例如,对于每个页面,在Page_Load中,我都有此配置:
Idioma.MudaCultura(CultureInfo.CurrentCulture.TwoLetterISOLanguageName.ToString());
My Button Event in Home Page. 主页中的我的按钮事件。 (basically this is the same for all pages)
(基本上所有页面都一样)
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");
}
My class Idiom 我班的成语
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);
}
}
}
What I need is setting CurrentInfo
with option that User was set in HomePage and if is different of this CurrentInfo. 我需要用在HomePage中设置User的选项设置
CurrentInfo
,如果此CurrentInfo与众不同。
Example: 例:
Home: My CurrentInfo is "en-US".
主页:我的CurrentInfo是“ en-US”。 Home: I change my Idiom to "es-ES".
主页:我将惯用语更改为“ es-ES”。 Other page: I need get this new CurrentInfo("es-ES").
其他页面:我需要获得这个新的CurrentInfo(“ es-ES”)。
Am I clear? 我清楚吗?
in order to make it in a professional way, you should know the hierarchical of the asp page, as you know any aspx page extends from System.Web.Pages so you have to create a class called BasePage this one will extends from Page and you will over-ride some default functions that will help you alot in cultureinfo and currency etc.. and make your aspx page extends from BasePage 为了以专业的方式进行制作,您应该了解asp页面的层次结构,因为您知道任何aspx页面都从System.Web.Pages扩展而来,因此您必须创建一个名为BasePage的类,该类将从Page扩展而您将覆盖一些默认功能,这些功能将帮助您大量使用cultureinfo和currency等。并使aspx页面从BasePage扩展
you need to reload page after changing the culture in order to make it take effect 您需要在更改区域性后重新加载页面才能使其生效
Check below example: 检查以下示例:
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();
}
}
I hope this was helpful 我希望这可以帮到你
This is not the complete solution, but shows you how can you set/retrieve something from the Session - if you decide to go this way. 这不是完整的解决方案,但是向您展示了如何从会话中设置/检索某些内容-如果您决定采用这种方式。
public string ChaveRM
{
get
{
if (Session["chaveRM"] != null)
return Session["chaveRM"].ToString();
return string.Empty;
}
set
{
Session["chaveRM"] = value;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.