如何使用 asp:DropDownList 更改页面本地化?
我有本地化的应用程序,用户可以在下拉控件中选择她喜欢的语言。 OnSelectedIndexChanged 事件之后有回发,并且 CurrentThread.CurrentCulture 应设置为用户选择的内容。
Login.aspx.cs
public partial class Login : BasePage
{
protected void LanguageDrop_changed(object sender, EventArgs e)
{
var lang = LanguageDropDown.SelectedValue;
Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
}
}
但是,它无法正常工作,因为 LanguageDrop_click 事件在 InitializeCulture 之后触发,因此初始化了一些默认区域性,将控件本地化为默认语言,然后设置了 CurrentThread.CurrentCulture。因此,用户必须再次刷新页面才能以所选语言查看它们。
更新: 我找到了部分答案:http://www.codeproject.com/Kb/aspnet/localizationByVivekTakur。 ASPX 他从 Form 集合中检索 InitializeCulture() 事件中的语言下拉值
I have localized application where user can choose her preferred language in dropdown control. After OnSelectedIndexChanged event there is postback and CurrentThread.CurrentCulture should be set to what user chooses.
Login.aspx.cs
public partial class Login : BasePage
{
protected void LanguageDrop_changed(object sender, EventArgs e)
{
var lang = LanguageDropDown.SelectedValue;
Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
}
}
However, it does not work correct because LanguageDrop_click event fires after InitializeCulture so some default culture is initialized, controls are localized to default language and then CurrentThread.CurrentCulture is set. So user have to refresh page once more to see them in chosen language.
UPDATE:
I found partial answer: http://www.codeproject.com/Kb/aspnet/localizationByVivekTakur.aspx
He retrieves language dropdown value in InitializeCulture() event from Form collection
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好的答案是我提供的链接
或进行 Response.Redirect 以便再次请求页面,然后设置区域性。
The best answer is either link I provided
or making Response.Redirect so page is requested again and then culture is set.
不幸的是,根据我的经验,没有更好的选择,您将不得不执行另一次刷新。你的推理是正确的。
In my experience there's no better option unfortunately and you will have to perform another refresh. Your reasoning is correct.