Hi all,
I was playing with this idea for a multi language website and thought it would be nice to share what I have so far.
What I wanted to achieve was a url structure like this:
http://mydomain.com/template_group/template/ for English (main site language)
http://mydomain.com/nl/template_group/template/ for Dutch
http://mydomain.com/fr/template_group/template/ for French
etc. (I have a few more countries but for this example we will use only 2)
First let’s start with a list of the steps I took:
1) created 2 new directories in the root of my EE install, “nl” and “fr”.
2) copied “index.php” and “path.php” to each of these directories.
3) changed the $system_path, $site_url and $global_vars variables in “path.php”.
4) created custom fields for each language.
5) changed some code in my templates.
If you’re still interested, here is the long version:
1) Create new directories for all countries/languages you want in the root of your EE install.
2) Put a copy of “index.php” and “path.php” in each of these directories.
3) Open the “path.php” files that are now inside your new country directories (“nl” and “fr”)
- change the $system_path variable to $system_path = “../system/”;
- change the $site_url variable to $site_url = “http://mydomain.com/nl/”; (make sure the country code matches the directory)
- add a “country_code” variable to the $global_vars array, my “nl” file looks like this:
<?php
// ------------------------------------------------------
// DO NOT ALTER THIS FILE UNLESS YOU HAVE A REASON TO
// ------------------------------------------------------
// Path to the directory containing your backend files
$system_path = "../system/";
// ------------------------------------------------------
// MANUALLY CONFIGURABLE VARIABLES
// See user guide for more information
// ------------------------------------------------------
$template_group = "";
$template = "";
$site_url = "http://mydomain.com/nl/";
$site_index = "";
$site_404 = "";
$global_vars = array(
"country_code" => "NL"
); // This array must be associative
?>
The {country_code} variable can now be used to tell EE which language it needs to show.
4) Create custom fields for each country and prefix the Field Name with a country code, for this example we’ll just use the Title and Body field to keep it simple. You should end up with something like this:
- NLtitle
- FRtitle
- NLbody
- FRbody
To make sure the main language (english in this case) that uses the regular {title} and {body} fields without the prefix, keeps working, also add the country_code variable to the “path.php” in the root of your site, but leave its value empty! likse this:
$global_vars = array(
"country_code" => ""
);
5) In your templates you can now replace {title} with {{country_code}title} and {body} with {{country_code}body}.
Pointing your browser to http://mydomain.com/nl/ should now take the {country_code} variable from your “path.php” (NL in this case) use it to change {title} into {NLtitle} and display the NLtitle field.
I also added some javascript to simply replace the country code in the url and reload the page so you can swap languages and stay on the same page.
Here is an example template:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Multi Language Test</title>
<!-- JS code -->
<script type="text/JavaScript">
<!--
function swapLanguage(lang) {
l = location.href;
u = document.URL.split('/');
w = u[u.length-(u.length-3)];
location.href = l.replace(w,lang);
}
//-->
</script>
<!-- // JS code -->
</head>
<body>
<!-- EE code -->
{assign_variable:my_weblog="default_site"}
<p><a href="javascript:swapLanguage('nl')">NL</a> | <a href="javascript:swapLanguage('fr')">FR</a></p>
{exp:weblog:entries weblog="default_site"}
<h2 class="title">
{{country_code}title}
</h2>
{{country_code}body}
{if country_code == ""}
<div class="posted">Posted by {author} on {entry_date format='%m/%d'} at {entry_date format='%h:%i %A'}</div>
{/if}
{if country_code == "NL"}
<div class="posted">Geschreven door {author} op {entry_date format='%m/%d'} om {entry_date format='%h:%i %A'}</div>
{/if}
{/exp:weblog:entries}
<!-- // EE code -->
</body>
</html>
I have no idea if this approach has any drawbacks, it seems to be working well so far.
The only problem I ran into was with .htaccess, I use it to remove index.php and didn’t figure out how to change te rewrite rules. If anyone knows I’d love to hear from you.
I’m using this method:
RewriteRule ^\.htaccess$ - [F]
RewriteRule ^favicon\.ico - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Cheers
