在ASP.NET应用程序中实现语言菜单的最佳方法

时间:2022-08-26 20:49:23

I trying to implement a typical languages menu where users can select the language they want to view the site in through a menu that appears throughout all pages in the site.

我尝试实现一个典型的语言菜单,用户可以通过显示在网站所有页面中的菜单选择他们想要查看网站的语言。

The menu will appear on multiple master pages (currently one for pages where users are logged in and one for pages where users are not).

该菜单将出现在多个母版页上(当前一个用于登录用户的页面,另一个用于用户未登录的页面)。

My current implementation is having a single master page base class (let's call it MasterBase). MasterBase has an event

我目前的实现是拥有一个主页基类(让我们称之为MasterBase)。 MasterBase有一个活动

public event LanguageChangedEventHandler LanguageChanged;

where LanguagedChangedEventHandler is simply defined as

其中,LanguagedChangedEventHandler只是定义为

public delegate void LanguageChangedEventHandler(string NewCulture);

MasterBase also has an overridable method

MasterBase也有一个可覆盖的方法

protected virtual void OnLanguageChanged(string NewCulture)

which just basically fires the event.

这基本上只是解雇了这个事件。

Each master page that inherits MasterBase overrides OnLanguageChanged and does the usual stuff like set the Thread's CurrentUICulture and the language cookie then does a

每个继承MasterBase的母版页都会覆盖OnLanguageChanged并执行常见的操作,如设置Thread的CurrentUICulture和语言cookie然后执行

Server.Transfer(this.Page.AppRelativeVirtualPath, true);

to get the page to reload with localized values for the new culture. On the master page for logged in users it also updates the user's language pref in the db.

使页面重新加载新文化的本地化值。在登录用户的母版页上,它还会更新数据库中用户的语言首选项。

Each language option is currently a LinkButton on a master page that inherits from MasterBase. When the link is clicked it calls the base OnLanguagedChanged method passing in the correct culture info. E.g.

每个语言选项当前是从MasterBase继承的母版页上的LinkBut​​ton。单击链接时,它会调用基于OnLanguagedChanged的方法传递正确的区域性信息。例如。

protected void btnEnglish_Click(object sender, EventArgs e) {
    this.OnLanguageChanged("en-US");
    }

Each page that needs to handle a language change then has some code in the page load that looks like...

每个需要处理语言更改的页面在页面加载中都有一些代码看起来像......

((MasterBase)this.Master).LanguageChanged += this.Page_OnLanguageChanged;
// Where Page_OnLanguageChanged has the signature of LanguageChangedEventHandler
// and has stuff like reloading options in a drop down using the new language.

Quite a convoluted 'framework' =)

相当复杂的'框架'=)

  1. Firstly it's hard for new developers to know they have to hook up a method to the MasterBase's LanguageChanged event to handle language changes. Yes, we do document it. But still it's not something straightforward and obvious.
  2. 首先,新开发人员很难知道他们必须将方法连接到MasterBase的LanguageChanged事件来处理语言更改。是的,我们会记录它。但它仍然不是直截了当且显而易见的。

  3. Secondly, all language changes are post backs. This is problematic especially when you want to navigate back with the browser Back button.
  4. 其次,所有语言变化都是回复。这是有问题的,特别是当您想要使用浏览器后退按钮导航回来时。

I'm looking for a more elegant solution. One that doesn't have both the problems above and also handles my current requirements.

我正在寻找更优雅的解决方案。一个既没有上述问题,也处理我当前的要求。

Greatly appreciate any suggestions. Thanks.

非常感谢任何建议。谢谢。

2 个解决方案

#1


1  

It seems to me that it would be better to implement this in a control that sets an application variable that all pages could use. That way you could just implement the code in one place and have it always available on each page that displays the control (could be in your master's so all pages that inherit get it automatically). I think in the control you would have a handler that sets the global language setting and then reloads the page. Each page would check the language setting during page_load or prerender and load the proper localized strings accordingly.

在我看来,最好在控件中实现它,设置一个所有页面都可以使用的应用程序变量。这样你就可以在一个地方实现代码并让它始终在显示控件的每个页面上都可用(可以在你的master中,所以所有继承的页面都会自动获取)。我认为在控件中你将拥有一个设置全局语言设置的处理程序,然后重新加载页面。每个页面都会在page_load或prerender期间检查语言设置,并相应地加载正确的本地化字符串。

#2


1  

I would just use the PreInit event on base page to set the current ui culture. I am not clear on why you would need each page to know when language is changed.

我只想在基页上使用PreInit事件来设置当前的ui文化。我不清楚你为什么需要每个页面来知道语言何时被改变。

#1


1  

It seems to me that it would be better to implement this in a control that sets an application variable that all pages could use. That way you could just implement the code in one place and have it always available on each page that displays the control (could be in your master's so all pages that inherit get it automatically). I think in the control you would have a handler that sets the global language setting and then reloads the page. Each page would check the language setting during page_load or prerender and load the proper localized strings accordingly.

在我看来,最好在控件中实现它,设置一个所有页面都可以使用的应用程序变量。这样你就可以在一个地方实现代码并让它始终在显示控件的每个页面上都可用(可以在你的master中,所以所有继承的页面都会自动获取)。我认为在控件中你将拥有一个设置全局语言设置的处理程序,然后重新加载页面。每个页面都会在page_load或prerender期间检查语言设置,并相应地加载正确的本地化字符串。

#2


1  

I would just use the PreInit event on base page to set the current ui culture. I am not clear on why you would need each page to know when language is changed.

我只想在基页上使用PreInit事件来设置当前的ui文化。我不清楚你为什么需要每个页面来知道语言何时被改变。