我第一次做国际化,我有一个问题:
我想知道是否可以从母版页进行国际化。我添加了一个带有一些语言的下拉列表,我正在使用一个会话,所以当我更改一种语言时,会检测到新文化并且它会发生变化,页面会被重新加载并且内容(我正在使用资源)被翻译。但它不起作用。
我在这里附上一些代码:
- Modules.master.vb -
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
_Website = clsWebsiteDA.GetById(CInt(Request.QueryString("web")))
If Not Session("cms_language") Is Nothing Then
LanguageDropDownList1.SelectedValue = Session("cms_language")
Dim sCulture As String = Session("cms_language")
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture)
Thread.CurrentThread.CurrentUICulture = New CultureInfo(sCulture)
End If
End Sub
Protected Sub LanguageDropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles LanguageDropDownList1.SelectedIndexChanged
Session("cms_language") = LanguageDropDownList1.SelectedValue
Response.Redirect(Request.Url.ToString)
End Sub
结束班
- Modules.master(CSS) -
<asp:DropDownList ID="LanguageDropDownList1" runat="server" AutoPostBack="True"
Height="16px" Width="65px"
meta:resourcekey="LanguageDropDownList1Resource1">
<asp:ListItem Value="nl" meta:resourcekey="ListItemResource1">Dutch</asp:ListItem>
<asp:ListItem Value="fr" meta:resourcekey="ListItemResource1">French</asp:ListItem>
<asp:ListItem Value="es" meta:resourcekey="ListItemResource2">Spanish</asp:ListItem>
<asp:ListItem Value="en" meta:resourcekey="ListItemResource3">English</asp:ListItem>
</asp:DropDownList>
如果不能这样做,那么正确的方法是什么? 提前谢谢,
阿尔夫。
更新
我通过这个link找到了我认为符合我的问题的解决方案。但现在我有继承问题。 我在文件夹中创建了一个名为Basepage.aspx的新WebForm:
Imports System.Globalization
导入System.Threading
部分类基页 继承System.Web.UI.Page
Protected Overrides Sub InitializeCulture()
If Request.Form("DropDownList1") IsNot Nothing Then
Dim selectedLanguage As String = _
Request.Form("DropDownList1")
UICulture = Request.Form("DropDownList1")
Culture = Request.Form("DropDownList1")
Thread.CurrentThread.CurrentCulture = _
CultureInfo.CreateSpecificCulture(selectedLanguage)
Thread.CurrentThread.CurrentUICulture = New _
CultureInfo(selectedLanguage)
End If
MyBase.InitializeCulture()
End Sub
结束班
但是,当我在我的一个页面中删除时,
Partial Class Administration_index
Inherits System.Web.UI.Page
的
Partial Class Administration_index
Inherits Basepage
它抱怨说它没有定义。我该如何处理这种继承? VB赞赏,虽然C#还可以......
答案 0 :(得分:1)
是否必须尝试覆盖Page类的initializeCulture事件?
例如:
/// <summary>
/// Initializes culture for the page
/// </summary>
[VersionChange( "6.1.34.89", "24/12/2009", "Custom Cultures added" )]
protected override void InitializeCulture()
{
try
{
CultureInfo oCultureInfo;
try
{
oCultureInfo = CultureInfo.CreateSpecificCulture( this.CurrentCustomCulture );
}
catch ( ArgumentException )
{
//Get culture info based on Great Britain
CultureInfo cultureInfo = new CultureInfo( "en-GB" );
RegionInfo regionInfo = new RegionInfo( cultureInfo.Name );
CultureAndRegionInfoBuilder cultureAndRegionInfoBuilder = new CultureAndRegionInfoBuilder( this.CurrentCustomCulture, CultureAndRegionModifiers.None );
cultureAndRegionInfoBuilder.LoadDataFromCultureInfo( cultureInfo );
cultureAndRegionInfoBuilder.LoadDataFromRegionInfo( regionInfo );
// Custom Changes
cultureAndRegionInfoBuilder.CultureEnglishName = this.CurrentCustomCulture;
cultureAndRegionInfoBuilder.CultureNativeName = this.CurrentCustomCulture;
cultureAndRegionInfoBuilder.Register();
oCultureInfo = CultureInfo.GetCultureInfo( this.CurrentCustomCulture );
}
catch ( Exception )
{
throw;
}
Thread.CurrentThread.CurrentCulture = oCultureInfo;
Thread.CurrentThread.CurrentUICulture = oCultureInfo;
Page.Culture = oCultureInfo.Name;
Page.UICulture = oCultureInfo.Name;
base.InitializeCulture();
}
catch ( Exception )
{
throw;
}
}
编辑: 添加了基页面示例
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Web.UI;
using Objects.Database;
using Objects.Engine;
using System.Web.UI.WebControls;
namespace Objects.Controls
{
/// <summary>
/// Base class for all web pages
/// </summary>
[VersionChange( "6.1.38.112", "19/01/2010", "SessionKeys class now used" )]
public class BasePage : Controls.TabPersistentWebPage
{
//- PROPERTIES --------------------------------------------------------------------------------------------------------------
#region Properties
/// <summary>
/// Gets or Sets the Custom Culture for the system
/// </summary>
[VersionChange( "6.1.34.89", "24/12/2009", "Custom Cultures added" )]
protected string CurrentCustomCulture
{
get
{
if ( Session[ SessionKeys.CurrentCustomCulture ] == null )
{
//Set the culture to the system default
Session[ SessionKeys.CurrentCustomCulture ] = Settings.Instance.SystemCulture;
}
return Session[ SessionKeys.CurrentCustomCulture ].ToString();
}
set
{
Session[ SessionKeys.CurrentCustomCulture ] = value;
}
}
#endregion Properties
//- EVENTS ------------------------------------------------------------------------------------------------------------------
#region Events
/// <summary>
/// Initializes culture for the page
/// </summary>
[VersionChange( "6.1.34.89", "24/12/2009", "Custom Cultures added" )]
protected override void InitializeCulture()
{
try
{
CultureInfo oCultureInfo;
try
{
oCultureInfo = CultureInfo.CreateSpecificCulture( this.CurrentCustomCulture );
}
catch ( ArgumentException )
{
//Get culture info based on Great Britain
CultureInfo cultureInfo = new CultureInfo( "en-GB" );
RegionInfo regionInfo = new RegionInfo( cultureInfo.Name );
CultureAndRegionInfoBuilder cultureAndRegionInfoBuilder = new CultureAndRegionInfoBuilder( this.CurrentCustomCulture, CultureAndRegionModifiers.None );
cultureAndRegionInfoBuilder.LoadDataFromCultureInfo( cultureInfo );
cultureAndRegionInfoBuilder.LoadDataFromRegionInfo( regionInfo );
// Custom Changes
cultureAndRegionInfoBuilder.CultureEnglishName = this.CurrentCustomCulture;
cultureAndRegionInfoBuilder.CultureNativeName = this.CurrentCustomCulture;
cultureAndRegionInfoBuilder.Register();
oCultureInfo = CultureInfo.GetCultureInfo( this.CurrentCustomCulture );
}
catch ( Exception )
{
throw;
}
Thread.CurrentThread.CurrentCulture = oCultureInfo;
Thread.CurrentThread.CurrentUICulture = oCultureInfo;
Page.Culture = oCultureInfo.Name;
Page.UICulture = oCultureInfo.Name;
base.InitializeCulture();
}
catch ( Exception )
{
throw;
}
}
/// <summary>
/// Pre Render
/// </summary>
/// <param name="e">args</param>
[VersionChange("6.2.58.180", "23/06/2010", "IE7 Compatibility mode forced")]
protected override void OnPreRender(EventArgs e)
{
try
{
//Force IE7 compatibility
Page.Header.Controls.AddAt(0, new System.Web.UI.HtmlControls.HtmlMeta() { HttpEquiv = "X-UA-Compatible", Content = "IE=EmulateIE7" });
base.OnPreRender(e);
}
catch (Exception)
{
throw;
}
}
#endregion Events
//- METHODS -----------------------------------------------------------------------------------------------------------------
#region Methods
/// <summary>
/// Formats a decimal number to the specified format in the settings
/// </summary>
/// <param name="d">Decimal</param>
/// <returns>string representation of the decimal</returns>
protected string FormatDecimal( decimal d )
{
try
{
return d.ToString( Settings.Instance.DecimalFormat );
}
catch ( Exception )
{
throw;
}
}
/// <summary>
/// Removes a menu item based on the value
/// </summary>
/// <param name="MenuItemValue">The value of the menu item</param>
/// <param name="Menu">The Menu with the item to remove</param>
[VersionChange( "6.1.36.102", "11/01/2010", "Added this method to remove menu items. Duplicate from MasterBasePage", "AF" )]
protected void RemoveMenuItem( string MenuItemValue, Menu Menu )
{
try
{
foreach ( MenuItem oMenuItem in Menu.Items )
{
if ( oMenuItem.Value == MenuItemValue )
{
//Menu.Items.Remove( oMenuItem );
oMenuItem.Enabled = false;
return;
}
}
}
catch ( Exception )
{
throw;
}
}
/// <summary>
/// Opens a help window
/// </summary>
/// <param name="Link">The link to open</param>
[VersionChange( "6.1.41.125", "08/02/2010", "Method added to open a help window" )]
protected void OpenHelp( string Link )
{
try
{
base.OpenWindow( Link, 800, 600, "Help", Enums.ScrollBars.Two, true, true, false, false );
}
catch ( Exception )
{
throw;
}
}
#endregion
//---------------------------------------------------------------------------------------------------------------------------
}
}
答案 1 :(得分:0)
在其他地方询问并回答了类似问题(特别是有关本地化字符串资源的问题):Localizing strings in master pages of ASP.NET MVC application
正如您所指出的,关键点是MasterPage
不是Page
的类型,因此它无法访问Page的资源机制。
如果您需要做的是在母版页中本地化字符串,并且如果可以在全局资源文件中找到本地化字符串,则此代码将在您的母版页的代码隐藏中起作用:
protected string GetLocalizedString(string key)
{
return (string) GetGlobalResourceObject("default.aspx", key);
}
您可以在母版页本身中引用具有此类代码的本地化字符串:
<h1><%= GetLocalizedString("header_string_identifier") %></h1>
这完全取决于default.aspx.resx
目录中的资源文件App_GlobalResources
。虽然您显然知道如何创建资源文件,但为了完整性,我会说最简单的方法是调用Add,然后添加New Item,然后从Visual Studio的项目上下文菜单中调用Resource File。
虽然使用Visual Studio内置的资源编辑器创建资源条目很容易,但您也可以手动编辑它。无论哪种方式,资源文件都应包含这样的部分:
<data name="header_string_identifier" xml:space="preserve">
<value>This is the H1 text to display.</value>
</data>
不容错过的两个细节是:
确保资源条目中的name=
与密钥匹配
母版页指令中的参数。
确保第一个
GetGlobalResourceObject
调用中的参数与名称匹配
删除了尾随.resx
的资源文件。