1 of 3
1
Multi Language Site Setup
Posted: 03 May 2006 03:32 PM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  879
Joined  02-05-2002

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

 Signature 

Member of the EE Pro Network

Profile
 
 
Posted: 04 May 2006 07:58 AM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23521
Joined  05-20-2002

Dang, Cocoaholic!  Thanks for the nice write up.  I like breaking it out and using the global array to hold the country code.  You should add this to the wiki- it’s definitely a handy tutorial.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 04 May 2006 08:03 AM   [ Ignore ]   [ # 2 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15343
Joined  05-15-2004

I agree. I’m attempting to make a second language version of my blog over the weekend, and this will come in handy. Yes, do put it in the wiki, please grin

 Signature 

Everything will be good in the end. If it’s not good, it’s not the end.

Profile
MSG
 
 
Posted: 04 May 2006 11:17 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  879
Joined  02-05-2002

OK, I’ll add it to the wiki when I find the time… if someone else will look it over and turn my clumsy English into a proper tutorial. wink

 Signature 

Member of the EE Pro Network

Profile
 
 
Posted: 04 May 2006 12:58 PM   [ Ignore ]   [ # 4 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1401
Joined  01-15-2005

Good one Cocoaholic! Thanks for sharing it.

 Signature 

EE Duration Tags | {view_count_total}

Profile
 
 
Posted: 10 May 2006 03:59 AM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  879
Joined  02-05-2002

@Ingmar, did you use this method for you blog yet? Would like to know if it worked for your setup.

I figured out how to remove “index.php” from the additional countries.
Just add a .htaccess file to each sub folder and include the country code in the “RewriteRule”.

In this example I added “/nl” in the last line:

RewriteRule ^\.htaccess$ - [F]
RewriteRule
^favicon\.ico - [L]
RewriteCond
%{REQUEST_FILENAME} !-f
RewriteCond
%{REQUEST_FILENAME} !-d
RewriteRule
^(.*)$ /nl/index.php?/$1 [L]

EDIT: Yes, I will add this to the wiki soon…

 Signature 

Member of the EE Pro Network

Profile
 
 
Posted: 10 May 2006 05:21 AM   [ Ignore ]   [ # 6 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15343
Joined  05-15-2004

No, I haven’t actually come around to implementing this yet, but will soon (I hope). Looks quite straight forward.

 Signature 

Everything will be good in the end. If it’s not good, it’s not the end.

Profile
MSG
 
 
Posted: 10 May 2006 05:22 AM   [ Ignore ]   [ # 7 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1401
Joined  01-15-2005

Great, I will test this tomorrow.

 Signature 

EE Duration Tags | {view_count_total}

Profile
 
 
Posted: 13 May 2006 04:25 AM   [ Ignore ]   [ # 8 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1454
Joined  09-16-2004

Very nice, I have some experience with multi-language sites myself and this is yet another interesting way of doing it. Be sure to add this to the Wiki…

 Signature 

Peace, e-man.
stookstudio.com, websites built with care and web standards. LinkedIn profile

Profile
 
 
Posted: 23 May 2006 01:01 PM   [ Ignore ]   [ # 9 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  147
Joined  06-19-2005

Thanks a lot Cocoaholic and e-man for sharing your experience with us.
On www.qualilangues.com I’d started a multilingual website using some of the principles you exposed.
...

Multilingual website-owners unite and share your knowledge!

Profile
 
 
Posted: 13 July 2006 06:15 AM   [ Ignore ]   [ # 10 ]  
Grad Student
Avatar
Rank
Total Posts:  45
Joined  06-13-2006

thx Cocoaholic & e-man for your ideas.

here is a php version for switching the language staying on the same page.
works on:
http://mydomain.com
http://mydomain.com/en/template_group/template/
http://mydomain.com/de/template_group/template/

don’t forget to set “Allow PHP?” to “Yes”

<?
function getLanguageURL($lang) {
    $u
= explode("/", $_SERVER['REQUEST_URI']);
    
$u[1] = $lang;
    return
implode("/", $u);
}
?>

<a href="<? echo getLanguageURL('en'); ?>" title="Change the language to english">EN</a> |
<
a href="<? echo getLanguageURL('de'); ?>" title="Wechsle die Sprache zu Deutsch">DE</a>

your example with php:
don’t forget to set “Allow PHP?” to “Yes” in your “Template Preferences”
for each template where you like to switch the language.

<?
function getLanguageURL($lang) {
    $u
= explode("/", $_SERVER['REQUEST_URI']);
    
$u[1] = $lang;
    return
implode("/", $u);
}
?>

<!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>
</
head>

<
body>

<!--
EE code -->
{assign_variable:my_weblog="default_site"}

<p><a href="<? echo getLanguageURL('nl'); ?>">NL</a> | <a href="<? echo getLanguageURL('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>

 Signature 

br, yout
eepl 1.5.2 - 20070102 - foreverart
(not finished! - many changes & much more content will come)
———————————————————————————————————————->
tip: LAOS - Lürzer’s Archive

Profile
 
 
Posted: 13 July 2006 06:19 AM   [ Ignore ]   [ # 11 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  879
Joined  02-05-2002

Perfect!

Thanks for sharing smile

 Signature 

Member of the EE Pro Network

Profile
 
 
Posted: 13 July 2006 07:21 AM   [ Ignore ]   [ # 12 ]  
Grad Student
Avatar
Rank
Total Posts:  45
Joined  06-13-2006

i’ve to thank you. your idea was one of the reasons why i bought “ee” wink

i use this path.php in http://mydomain.com/

<?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/en/";
$site_index = "";
$site_404 = "";
$global_vars = array(
"country_code" => "",
"c_code" => "EN"
); // This array must be associative

?>


i use this path.php in http://mydomain.com/en/

<?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/en/";
$site_index = "";
$site_404 = "";
$global_vars = array(
"country_code" => "",
"c_code" => "EN"
); // This array must be associative

?>


& this path.php in http://mydomain.com/de/

<?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/de/";
$site_index = "";
$site_404 = "";
$global_vars = array(
"country_code" => "DE",
"c_code" => "DE"
); // This array must be associative

?>

so i can use “c_code” for:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="{c_code}" lang="{c_code}">


& for includes:

<!--  :: sidebar  -->
{embed="inc/{c_code}_sidebar"}
<!--  sidebar ::  -->


& if you use some arrays like explained from e-man in wiki: Multi language site

<?php

$info
="{c_code}";

switch (
$info)
{
case "en":
$lang="en";
$high= "high season";
$low= "low season";
break;

case
"de":
$lang = "de";
$high= "hoch";
$low= "neben";
break;

default :
$lang="en";
$high= "high season";
$low= "low season";
}

?>

so the only thing i am missing is any way to change the rest like the way
“member localization setting” does ...

eg: date, advanced search, ...

 Signature 

br, yout
eepl 1.5.2 - 20070102 - foreverart
(not finished! - many changes & much more content will come)
———————————————————————————————————————->
tip: LAOS - Lürzer’s Archive

Profile
 
 
Posted: 13 July 2006 07:36 AM   [ Ignore ]   [ # 13 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  879
Joined  02-05-2002

Don’t have time to look into this right now, but could it be done using an extension?

This hook looks interesting: http://www.pmachine.com/developers/hooks/sessions_start/.
Maybe it would be possible to get the “c_code” and use it to override “$SESS->userdata[‘language’];” ?

Hmm, I might try this later…

 Signature 

Member of the EE Pro Network

Profile
 
 
Posted: 13 July 2006 07:47 AM   [ Ignore ]   [ # 14 ]  
Grad Student
Avatar
Rank
Total Posts:  45
Joined  06-13-2006

sounds interesting THX smile

 Signature 

br, yout
eepl 1.5.2 - 20070102 - foreverart
(not finished! - many changes & much more content will come)
———————————————————————————————————————->
tip: LAOS - Lürzer’s Archive

Profile
 
 
Posted: 17 July 2006 09:17 AM   [ Ignore ]   [ # 15 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  879
Joined  02-05-2002

Unfortunately using the extension hooks in ‘core.session.php’ is not really an option, see this thread.

You can still get what you want using a quick hack.

Add another variable called ‘language’ to your ‘path.php’ file, make sure the value is exactly the same as the language pack you are using.

$global_vars = array(
"language" => "dutch",
"c_code" => "NL"
);

Open ‘core.session.php’ and paste the following code just after the ‘sessions_end’ hook (line 315 in my version):

// language hack
if(isset($IN->global_vars['language'])) $this->userdata['language'] = $IN->global_vars['language'];
// end language hack

Too bad we can’t do this without hacking a core file. (AFAIK)

 Signature 

Member of the EE Pro Network

Profile
 
 
Posted: 17 July 2006 09:38 AM   [ Ignore ]   [ # 16 ]  
Grad Student
Avatar
Rank
Total Posts:  45
Joined  06-13-2006

thank you so much! it works perfect for me smile

the hook starts at line 307:

// 'sessions_end' hook.
//  - Modify the user's session/member data.
//  - Additional Session or Login methods (ex: log in to other system)
//
$edata = $EXT->call_extension('sessions_end', $this);
if (
$EXT->end_script === TRUE) return;
//
// -------------------------------------------

just placed it after it smile

 Signature 

br, yout
eepl 1.5.2 - 20070102 - foreverart
(not finished! - many changes & much more content will come)
———————————————————————————————————————->
tip: LAOS - Lürzer’s Archive

Profile
 
 
Posted: 03 September 2006 03:46 PM   [ Ignore ]   [ # 17 ]  
Lab Assistant
RankRank
Total Posts:  209
Joined  07-04-2005

Was perousing this thread as I am looking into an English / German site and want to use this method.  I assume that removing the index.php should be the first step before i start anything?  Is that correct?

Also, my .htaccess file looks like this:

RewriteEngine On
RewriteRule
^\.htaccess$ - [F]
RewriteRule
^favicon\.ico - [L]
RewriteCond
%{REQUEST_FILENAME} !-f
RewriteCond
%{REQUEST_FILENAME} !-d
RewriteRule
^(.*)$ /index.php?/$1 [L]

The standard EE template is all that is installed.  when I click on the about link I get an error 404.  I tried the include exclude statements as the wiki suggested but to no avail.  Any ideas?

Profile
 
 
Posted: 08 September 2006 05:48 AM   [ Ignore ]   [ # 18 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  962
Joined  12-20-2002

Great, cocoaholic, this is the elegant way to internationalize.

Now, if we only had “clone field” or even better, multiple field groups like we now have multiple category groups, and clone field group…

Never satisfied, am I?

 Signature 

Who ain’t a slave? - Ishmael

Profile
 
 
   
1 of 3
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 64877 Total Logged-in Users: 69
Total Topics: 81802 Total Anonymous Users: 44
Total Replies: 439747 Total Guests: 304
Total Posts: 521549    
Members ( View Memberlist )
Newest Members:  dxrsmdanbilly8hrkiliwysso50kexpressoKlaasdarrenstylestravelerjcatoncvadrata