A Resource Manager Wrapper User Control, used on ASP.NET 1.1 for Globalization and Localization of apps
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Resources
Imports System.Reflection
Imports System.Threading
Public Class SiteTextControl
Inherits System.Web.UI.UserControl
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
End Sub
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private _Name As String
Private lit As LiteralControl
Private res As ResourceManager
Private Shared _myCulture As String
Public Shared ReadOnly Property myCulture() As String
Get
Return Thread.CurrentThread.CurrentCulture.ToString
End Get
End Property
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Public WriteOnly Property Text() As String
Set(ByVal Value As String)
If Controls.Count > 0 Then
lit.Text = Value
End If
End Set
End Property
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Protected NotOverridable Overrides Sub CreateChildControls()
res = New ResourceManager("machinelocator.Resource1", [Assembly].GetExecutingAssembly())
Dim _Text As String = res.GetString(_Name, Thread.CurrentThread.CurrentCulture)
lit = New LiteralControl(_Text)
Controls.Add(lit)
lit.Dispose()
End Sub
Public Shared Function GetResourceContent(ByVal name As String) As String
Dim rm As ResourceManager = New ResourceManager("machinelocator.Resource1", [Assembly].GetExecutingAssembly())
Return rm.GetString(name)
End Function
End Class
On the Application events at the Global.asax your code should look like:
mports System.Web
Imports System.Web.SessionState
Imports System.Globalization
Imports System.Threading
Imports System.Resources
Imports System.Reflection
Imports System.Collections
Imports System.IO
Public Class Global
Inherits System.Web.HttpApplication
Private rm As ResourceManager
#Region " Component Designer Generated Code "
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
components = New System.ComponentModel.Container
End Sub
#End Region
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session is started
End Sub
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request
Dim culturePref As String = Nothing
Dim cultureSet As Boolean = False
Try
If Request.Cookies.Count <> 0 Then
If Request.Cookies("CulturePref").Value <> Nothing Then
culturePref = Request.Cookies("CulturePref").Value
cultureSet = True
End If
End If
Catch ex As Exception
'nothing
End Try
If cultureSet Then
Try
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culturePref)
Catch
'Set the default culture
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
End Try
End If
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture
End Sub
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires upon attempting to authenticate the use
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Fires when an error occurs
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session ends
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application ends
End Sub
End Class
And finally for using this control you can either put it on your HTML or use it on your CodeBehind like:
strDetail =SiteTextControl.GetResourceContent("AproxResource")
Special thanks to Mark Kucera the original creator of the control, the above code have very few modifications.
0 Comments:
Post a Comment
<< Home