Optionally provide private feedback to help us improve this article...

Thank you for your feedback!


Displaying localized forums within InstantForum

This article shows how to use a single InstantForum 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...

https://forum.domain.com/?Culture=en-US
https://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.cs. Modify the AddParsedSubObjects method as shown below.

protected override void AddParsedSubObject(object obj)
{
	// check for culture querystring
	if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.QueryString("Culture"))) {
		InstantASP.Common.HttpUtility.Cookies.SetCookie("forumCulture", System.Web.HttpContext.Current.Request.QueryString("Culture"));
	}

	if (obj is InstantASP.InstantForum.UI.Controls.Content) {
		this.ArrListContents.Add(obj);
	} else {
		this.DefaultControl.Controls.Add((System.Web.UI.Control)obj);
	}

}

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 string GetCurrentCulture {

	get {
		System.Web.HttpCookie CultureCookie = InstantASP.Common.HttpUtility.Cookies.GetCookie("forumCulture");

		if ((CultureCookie != null)) {
			if (System.IO.Directory.Exists(InstallPath + InstantASP.InstantForum.Globals.Paths.GlobalizationDirectory + "\\" + CultureCookie.Value)) {
				return CultureCookie.Value;
			}
		}

		if (System.Web.HttpContext.Current.Request.IsAuthenticated) {
			if (!string.IsNullOrEmpty(GetCurrentUser.Culture)) {
				return GetCurrentUser.Culture;
			} else {
				return CurrentSharedSettings.Culture;
			}
		} else {
			return CurrentSharedSettings.Culture;
		}
	}
}

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 void BindData()
{
	// we don't have a forum, display index
	if (Forum == null) {
// removed code here to make this sample more readable
	}

	System.Web.HttpCookie CultureCookie = InstantASP.Common.HttpUtility.Cookies.GetCookie("forumCulture");

	InstantASP.InstantForum.Collections.ForumCollection FilterdForumCollection = null;


	if ((CultureCookie != null)) {
		FilterdForumCollection = new InstantASP.InstantForum.Collections.ForumCollection();

		for (Int32 i = 0; i <= ForumCollection.Count - 1; i++) {
			InstantASP.InstantForum.Components.Forum 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) {
				FilterdForumCollection.Add(Forum);
			}
		}

	}

	// do we have a collection?
	if (ForumCollection.Count > 0) {
		// bind group forum collection to repeater
		if ((FilterdForumCollection != null)) {
			ctlForumGroups.DataSource = FilterdForumCollection;
		} else {
			ctlForumGroups.DataSource = ForumCollection;
		}

		// bind data to repeater
		ctlForumGroups.DataBind();

	// hide the control
	} else {

		// disable controls
		this.Visible = false;

	}

}

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 https://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

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.