Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

Creating multilingual websites - Part 2

, 25 Aug 2004
Creating multilingual websites - Part 2
LocalizedSamplePart2.zip
LocalizedSample
bin
Localization.dll
LocalizedSample.dll
language
en-CA
fr-CA
Localization
bin
Localization.csproj.user
Localization.resharperoptions
LocalizedSample.csproj.webinfo
LocalizedSample.resharperoptions
LocalizedSamplePart2VB.zip
LocalizedSampleVB
bin
Localization.dll
LocalizedSampleVB.dll
language
en-CA
fr-CA
Localization
bin
Localization.dll
Localization.vbproj.user
LocalizedSampleVB.resharperoptions
LocalizedSampleVB.vbproj.webinfo
Imports System.Web
Imports System.IO
Imports System.Xml


Public NotInheritable Class ResourceManager
   Private Sub New()
   End Sub

   'Returns the culture name (ie, en-CA, fr-CA, en-US) of the current request
   Public Shared ReadOnly Property CurrentCultureName() As String
      Get
         Return System.Threading.Thread.CurrentThread.CurrentCulture.Name
      End Get
   End Property

   'just a wrapper to the getstring method to showcase how you can create shortcuts
   Public Shared ReadOnly Property Colon() As String
      Get
         Return GetString("colon")
      End Get
   End Property

   'Gets the resource you want. If you are in DEBUG mode and the key isn't there, this will throw an exception
   Public Shared Function GetString(ByVal key As String) As String
      Dim messages As Hashtable = GetResource()
      If messages(key) Is Nothing Then
         messages(key) = String.Empty
#If DEBUG Then
         Throw New ApplicationException("REsource value not foudn for key: " + key)
#End If
      End If
      Return CStr(messages(key))
   End Function

   Private Shared Function GetResource() As Hashtable
      Dim currentCulture As String = CurrentCultureName
      Dim defaultCulture As String = LocalizationConfiguration.GetConfig.DefaultCultureName
      Dim cacheKey As String = "Localization:" + defaultCulture + ":"c + currentCulture
      If HttpRuntime.Cache(cacheKey) Is Nothing Then
         Dim resource As New Hashtable

         LoadResource(resource, defaultCulture, cacheKey)
         If defaultCulture <> currentCulture Then
            Try
               LoadResource(resource, currentCulture, cacheKey)
            Catch ex As FileNotFoundException
            End Try
         End If

      End If
      Return CType(HttpRuntime.Cache(cacheKey), Hashtable)
   End Function

   Private Shared Sub LoadResource(ByVal resource As Hashtable, ByVal culture As String, ByVal cacheKey As String)
      Dim file As String = LocalizationConfiguration.GetConfig.LanguageFilePath + "\"c + culture + "\Resource.xml"
      Dim xml As New XmlDocument
      xml.Load(file)
      For Each n As XmlNode In xml.SelectSingleNode("Resource")
         If n.NodeType <> XmlNodeType.Comment Then
            resource(n.Attributes("name").Value) = n.InnerText
         End If
      Next
      HttpRuntime.Cache.Insert(cacheKey, resource, New Caching.CacheDependency(file), DateTime.MaxValue, TimeSpan.Zero)
   End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Karl Seguin

Canada Canada
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 26 Aug 2004
Article Copyright 2004 by Karl Seguin
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid