Multilingual MidCOM sites

Finland is a country with three official languages, and many organizations here want to provide their websites in multilingual format. While Midgard CMS supports storing multilingual content in same content tree, this is still not widely used. A far more common strategy is to use language prefix URLs.

With language prefixes each translation of a site has its own content tree starting from the language name, or 2-letter code. This has the advantage of allowing slightly different content structure to be presented on different translations. For example, a company might want to present products differently to the domestic and international markets. It also makes it easy to tell what language the content behind a link is in.

To make a MidCOM site multilingual there are two steps.

First, set up the website normally. Then log into the AIS interface and create top-level topics for each language you want to have on your site. It doesn't matter what component is used to drive those topics, but very often it is de.linkm.taviewer. The URL name of the topics should be the two letter country code.

The top-level country topics are actually the front pages of different language versions of the site. The site structure can be created freely under them, although it would be recommended to make the site structure quite identical between languages.

Then you have to make MidCOM aware of the language topics. This is done by creating a configuration snippet (/sitegroup-config/midcom-template/config) for the MidCOM Site Template.

Log into Aegir and go to the Snippets tab. There you should create a sitegroup-config top-level directory to the same sitegroup where the site is in. Then create a midcom-template snippet directory under that. In this directory you can place the configuration PHP code in a config snippet.

Configure the languages used on the site:

// Languages enabled on the site, and information about them
// in format "URL prefix" => "Language code"
$GLOBALS["midcom_site"]["site_languages_array"] = array(
"fi" => "fi", // Finnish
"en" => "en", // English
"ge" => "ka" // Georgian
);

// Default language
$midcom_site["default_language"] = "en";
$midcom_site["site_language"] = $GLOBALS["midcom_site"]["site_languages_array"][$midcom_site["default_language"]];

Set MidCOM's language based on the URL prefix:

if ($_MIDGARD["self"] == $GLOBALS["midcom_site"]["uri"]) 
{
// On-site language
if ($_MIDGARD["argc"] > 0
&& array_key_exists($_MIDGARD["argv"][0],$GLOBALS["midcom_site"]["site_languages_array"]))
{
$GLOBALS["midcom_site"]['site_language'] = $GLOBALS["midcom_site"]["site_languages_array"][$_MIDGARD["argv"][0]];
}
}
elseif ($_MIDGARD["self"] == $GLOBALS["midcom_site"]["uri"]."midcom-admin/ais/")
{
// AIS language
if ($_MIDGARD["argc"] > 0)
{

foreach ($GLOBALS["midcom_site"]["site_languages_array"] as $prefix => $code)
{
$language_topic = mgd_get_topic_by_name($GLOBALS["midcom_site"]["root_topic"]->id,$prefix);
if ($language_topic
&& ($language_topic->id == $_MIDGARD["argv"][0]
|| mgd_is_in_topic_tree($language_topic->id, $argv[0])))
{
$GLOBALS["midcom_site"]['site_language'] = $code;
}
}
}
}

This is all needed to make a site multilingual. With this setup the root URL of the site can be used for a splash screen presented to users for selecting language.

As an option, you can also utilize HTTP Language Negotiation for automatically directing the user to correct language version of the site. To do this, add the following to the midcom-template config snippet:

// Language negotiation
if ($_MIDGARD["uri"] == $GLOBALS["midcom_site"]["uri"])
{
if ($_SERVER[HTTP_ACCEPT_LANGUAGE])
{
$langstr = explode(',',$_SERVER[HTTP_ACCEPT_LANGUAGE]);

if (is_array($langstr))
{
foreach ($langstr as $language)
{
$language = substr($language,0,2);

if (array_key_exists($language,$GLOBALS["midcom_site"]["site_languages_array"]))
{
header("Location: ".$GLOBALS["midcom_site"]["uri"].$language."/");
exit;
}
}
}
}
// No language preferences matched, go to default language
header("Location: ".$GLOBALS["midcom_site"]["uri"].$GLOBALS["midcom_site"]['default_language']."/".$suffix);
exit;
}

Date output in MidCOM is tied to the site language, and uses PHP strftime() function for output. For this to work your server must have the locale you need installed on Unix level. On Debian the locales can be installed via the following command:

/usr/sbin/dpkg-reconfigure locales

Select the locales you need from the list and they will be installed by dpkg.

Updated 2005-09-27: This tutorial is now maintained in the Midgard Wiki.


Read more Midgard posts.