I'm doing some internationalization for first time, and I have an issue:
I would like to know if it is possible to internationalize from a master page. I added a dropdown list with some languages and I'm using a session , so when I change a language, the new Culture is detected and it changes, the page is reloaded and the contents (I'm using resources) translated. But it's not working.
Here I attach some code:
--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
End Class
--Modules.master(aspx)--
<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>
If it cannot be done like this, what is the proper approach? Thanks in advance,
Alf.
UPDATE:
I found through this link the solution that I think that fits with my problem. But now I have a problem of inheritance. I created in the folder a new WebForm called Basepage.aspx:
Imports System.Globalization
Imports System.Threading
Partial Class Basepage Inherits 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
End Class
But, when I subtitute for example, in one of my pages,
Partial Class Administration_index
Inherits System.Web.UI.Page
for
Partial Class Administration_index
Inherits Basepage
it complains saying that it's not defined. How can I deal with this inheritance? VB appreciated,though C# is ok...
Have to tried to override the initializeCulture event of the Page class?
eg:
/// <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;
}
}
EDIT: Added base page example
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
//---------------------------------------------------------------------------------------------------------------------------
}
}