HOWTO: Conditionally displaying localized forums using a custom...

Expand / Collapse
 

HOWTO: Conditionally displaying localized forums using a custom...


This article shows how to use a single InstantForum.NET installation to provide multi-lingual versions of your forums.

For this example to work you need to ensure your forum names are structured and have a consistent naming convention based on the language they will target and the name of the culture querystring passed into the forum. This example will look for the string passed into the Culture querystring within your forum titles and only display those that contain matching text.

For example if you have an existing web site and which to provide a link to both an English and French Canadian version of your forums you could use two links as shown below...

http://forum.domain.com/?Culture=en-US
http://forum.domain.com/?Culture=fr-CA

Depending on which link you clicked the forum would attempt to filter forums based on the culture and also default to the language pack for that culture if available within the Globalization folder.

1. Persist the culture querystring into a client side cookie

Open InstantASP.InstantForum.UI\MasterPages\MasterPage.vb. Modify the AddParsedSubObjects method as shown below.

Protected Overrides Sub AddParsedSubObject(ByVal obj As Object)

    ' check for culture querystring
    If System.Web.HttpContext.Current.Request.QueryString("Culture") <> "" Then
         InstantASP.Common.HttpUtility.Cookies.SetCookie("forumCulture", _
         System.Web.HttpContext.Current.Request.QueryString("Culture"))
    End If


    If TypeOf obj Is InstantASP.InstantForum.UI.Controls.Content Then
         Me.ArrListContents.Add(obj)
    Else
         Me.DefaultControl.Controls.Add(DirectCast(obj, System.Web.UI.Control))
    End If

End Sub

This will save the value of the "Culture" querystring to a client side cookie. We will be using this cookie to determine which language pack to default to and also which forums we should remove from the collection to display only forums for that culture.

2. Set the default Language Pack

If a language pack exists that matches your Culture querystring parameter you can modify the forum to use this language pack by default.

Open InstantASP.InstantForum\HttpContext\ForumContext.vb. Locate the "GetCurrentCulture" property and modify as shown below...

Private ReadOnly Property GetCurrentCulture() As String
    Get

    Dim CultureCookie As System.Web.HttpCookie = _
    InstantASP.Common.HttpUtility.Cookies.GetCookie("forumCulture")

    If Not CultureCookie Is Nothing Then
         If System.IO.Directory.Exists(InstallPath + _
         InstantASP.InstantForum.Globals.Paths.GlobalizationDirectory + _
         "\" + CultureCookie.Value) Then
              Return CultureCookie.Value
         End If
    End If


    If System.Web.HttpContext.Current.Request.IsAuthenticated Then
         If GetCurrentUser.Culture <> "" Then
              Return GetCurrentUser.Culture
         Else
              Return CurrentSharedSettings.Culture
         End If
    Else
         Return CurrentSharedSettings.Culture
    End If
    End Get
End Property

3. Filter forums based on the culture cookie

You can use the value within the culture cookie to build a new forum collection containing only the forums for the current culture. This allows you to hide your French Canadian forums for your English visitors and visa-versa.

You will need to open \InstantASP.InstantForum.UI\Controls\ViewForumGroups.vb and modify the BindData method as shown below...

Private Sub BindData()

    ' we don't have a forum, display index
     If Forum Is Nothing Then
         .. removed code here to make this sample more readable
    End If

    Dim CultureCookie As System.Web.HttpCookie = _
    InstantASP.Common.HttpUtility.Cookies.GetCookie("forumCulture")

    Dim FilterdForumCollection As InstantASP.InstantForum.Collections.ForumCollection = Nothing

    If Not CultureCookie Is Nothing Then

         FilterdForumCollection = New InstantASP.InstantForum.Collections.ForumCollection

         For i As Int32 = 0 To ForumCollection.Count - 1
              Dim Forum As InstantASP.InstantForum.Components.Forum = ForumCollection.Item(i)
              ' add forum groups that contain our culture within the forum name
              ' to the new filterdforumcollection, this only filters forum groups
              If Forum.Name.ToLower.IndexOf(CultureCookie.Value.ToLower()) > -1 Then
                   FilterdForumCollection.Add(Forum)
              End If
         Next

    End If


    ' do we have a collection?
    If ForumCollection.Count > 0 Then

         ' bind group forum collection to repeater
         If Not FilterdForumCollection Is Nothing Then
              ctlForumGroups.DataSource = FilterdForumCollection
         Else
              ctlForumGroups.DataSource = ForumCollection
         End If


         ' bind data to repeater
         ctlForumGroups.DataBind()

    Else ' hide the control

         ' disable controls
         Me.Visible = False

    End If

End Sub

This will fiilter forum groups and only display those groups which contain the string contained within the Culture querystring / cookie within the group name. For example if you link to your forums with the culture http://forum.comain.com/?Culture=fr-CA only forum groups which contain "fr-CA" will be displayed. Other forum groups will be hidden.

That's It

Don't forget once you've made the changes above you will need to rebuild the InstantForum.NET solution. Whilst there are improvements that can be made to this sample it shows the basic steps to control the default culture of your forums automatically using a querystring parameter. I hope this was helpful and would welcome any comments or suggestions below.


Rate this Article:
     

User Comments

Click to subscribe to comments RSS feed...
Member Photo by Ryan Healey posted 10th August 2009
Support Guru with 85 recognition pointsSupport Guru with 85 recognition pointsSupport Guru with 85 recognition pointsSupport Guru with 85 recognition pointsSupport Guru with 85 recognition pointsSupport Guru with 85 recognition pointsSupport Guru with 85 recognition pointsSupport Guru with 85 recognition points
This comment was helpful. 0 This comment is not helpful.
Hi Andrew,

Hope your very well. Many thanks for your comments. Did we resolve this via support?

If we can still assist please don't hesitate to email support with this link and i'll ensure we follow up here.

Regards,

Ryan
No Member Photo by mhong0814 posted 9th June 2009
New Member with 9 recognition pointsNew Member with 9 recognition pointsNew Member with 9 recognition pointsNew Member with 9 recognition pointsNew Member with 9 recognition pointsNew Member with 9 recognition pointsNew Member with 9 recognition pointsNew Member with 9 recognition points
This comment was helpful. 0 This comment is not helpful.
Hi Ryan,

This instruction is helpful, and I have added the de-DE language pack with these codes to my project successfully.
But now I have a strange result when I filter the forum with the Culture querystring,that is,
 
1.  when I pass the querystring to the URL with Culture value "de-DE"(?Culture=de-DE), these are no any forum topics displayed.
2.  but when I pass the querystring with "en-US"(?Culture=en-US), the all forum topics shows.

So,
how could I create a forum topice only belong to de-DE language pack?
I want to distingiush and separate the de-DE forum topics and en-US ones.
These is my question, and thanks for you help.

Andrew
Marked helpful 0 times based on 1 vote

Add Your Comments


Name: *
Email Address:
Web Address:
   
  
 
 
   
Verification Code:
*
 

Details
Product: InstantForum.NET
Version: 4.1
Type: HOWTO
Level: Intermediate
Article not rated yet.
Article has been viewed 5,060 times.
Last Modified:5th May 2009
Last Modified By: Ryan Healey
Options

Home | Products | Purchase | Services | Support | Company | Contact Us
Privacy Statement | © 1999-2009 InstantASP Limited. All Rights Reserved.