我的下拉列表中有一个语言列表,其中包含en-NZ,en-US等形式的值,但页面不会更改回发语言,因为我的语言代码错误。有人可以查看我的代码并告诉我,我在改变我的页面语言时做错了吗
我的lbllanguage.Text会在第二次回发时发生变化,所以它假设在第一次点击时更改ChangeLanguage_Click事件
主页:
protected void Page_PreRender(object sender, EventArgs e)
{
string Culture = (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture;
if (ddlChangeLanguage.Items.FindByValue(Culture) != null)
{
ddlChangeLanguage.SelectedValue = (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<CultureInfo> languages = CultureInfo.GetCultures(CultureTypes.SpecificCultures).OrderBy(x => x.Name).ToList();
SortedDictionary<string, string> sortedLanguages = new SortedDictionary<string, string>();
foreach (CultureInfo language in languages)
{
RegionInfo regionInfo = new RegionInfo(language.Name);
if (!sortedLanguages.ContainsKey(regionInfo.EnglishName))
{
sortedLanguages.Add(regionInfo.EnglishName, language.Name);
}
}
foreach (KeyValuePair<string, string> language in sortedLanguages)
{
ddlChangeLanguage.Items.Add(new ListItem { Value = language.Value, Text = language.Key });
}
ddlChangeLanguage.SelectedValue = (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture;
}
lbllanguage.Text = this.UICulture;
}
protected void ChangeLanguage_Click(object sender, EventArgs e)
{
(HttpContext.Current.Profile as ProfileCommon).Preferences.Culture = ddlChangeLanguage.SelectedValue;
}
和我的主页继承的我的BasePage
public class BasePage : System.Web.UI.Page
{
public BasePage()
{
}
protected override void InitializeCulture()
{
this.Culture = (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture;
this.UICulture = (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture;
}
}
答案 0 :(得分:2)
您必须将字符串存储在资源文件或附属程序集中才能进行翻译:
ASP.NET Web Page Resources Overview
<asp:Button ID="Button1" runat="server"
Text="<%$ Resources:WebResources, Button1Caption %>" />
答案 1 :(得分:1)
配置当前线程:
var culture = (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture;
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
答案 2 :(得分:0)
尝试将Page_PreRender中的逻辑更改为DropDownList.SelectedIndexChanged的处理程序。