x
 
Create New Page
 View Previous Changes    ( Last updated by Lisa Wess )

Multi language site

Table of Contents


Multi-language site

Several suggestions were made in this thread http://www.pmachine.com/forums/viewthread/9320/ and I ran with Sue’s http://www.shecodes.com idea.

Several people have mailed me about how to do this so I decided to do this write-up.
All of this info is presented “as is”, I’m sure more experienced php coders can come up with more elaborate stuff but all of this was used to great effect on a real-world site. http://www.readshotel.com

Alternative Method

An alternative method for multilingual sites is written up in a separate article.

An extension is also available here: http://expressionengine.com/forums/viewthread/51959/

Basic concept

You can present your content in different languages using a combination of URL-segments http://www.eewiki.com/wiki/URL_Segment_Variables, custom fields and a bit of PHP.

As an example, say you have a 3-language site (english (en), spanish (es) and german (de) ).
For every piece of info in your weblog create a custom field and use an extension (or prefix, whatever you prefer) like this:

* mycustomfield_en
* mycustomfield_es
* mycustomfield_de

then enter the appropriate data in the correct language.
The one great advantage of doing this, is that it keeps all versions of your content inside one blog/post, making updates and edits easier and more site-consistent.

Now add an en/, es/ or de/ segment to the URL to the page where you want your content to appear.
example: http://www.mysiterocks.com/templategroup/template/en/

Be sure that in your template preferences your template is set to parse PHP on input.(Allow PHP?: yes PHP Parsing Stage: input)
Now add this snippet of code to the top your template:

<?php

$info
="{segment_3}";

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

case
"de":
$lang = "de";
break;

case
"es":
$lang = "es";
break;

default :
$lang="en";

}

?>

The switch function http://es2.php.net/manual/en/control-structures.switch.php checks which values URL segment 3 has and passes that on to the variable $lang.

The great thing about using switch is that you can set a default value so that you always have a working variable $lang.

Now whenever you need to display a language version use this snippet of PHP: 

<?php echo $lang ?>

A more specific example using EE tags:

{exp:weblog:entries weblog="myweblog"}
{mycustomfield_<?php
echo $lang ?>}
{
/exp:weblog:entries}

That’s basically all you need to do to.

Expanding the basic concept

It doesn’t end there of course.
You can do the same using templates and embeds as well. For example using this to serve up your site navigation in different languages, or your meta (and lang) tags inside the head of your html document.
So you have a template called mainnav that holds the html code code for your main site navigation.
You can define 3 versions:
* mainnav_en
* mainnav_es
* mainnav_de
fill them with the appropriate language content and embed them using the same concept illustrated above:

example of lang definition inside a doctype”

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
>


<
html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo($lang) ?>" lang="<?php echo $lang ?>" >

Using php arrays with keywords and defining more variables

Say you have an html table (or whatever) and some of the keywords you use on that page can’t be fitted in your customfields.
Using a php array function: http://es2.php.net/manual/en/function.array.php with those keywords can give you added flexibilty.
You can also define added variables and use those.

Example of added variables: (used here) http://www.readshotel.com/index.php/hotel/tariffs/en/)

<?php

$info
="{segment_3}";

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

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

case
"es":
$lang = "es";
$high= "alta temporada";
$low= "baja temporada";
break;

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

?>

Example of using php array: (used herehttp://www.readshotel.com/index.php/restaurant/menu_a_la_carte/en/)

<?php

$info
="{segment_3}";

switch (
$info)
{
case "en":
$lang="en";
$dishtype= array ("Starters", "Meat Main Courses", "Fish Main Courses", "Desserts", "White Dessert Wine", "Half Bottles", "By The Glass", "Red Dessert Wine");
break;

case
"de":
$lang = "de";
$dishtype= array ("Vorspeisen", "Hauptgerichte mit Fleisch", "Hauptgerichte mit Fisch", "Desserts", "Liebliche Weißweine ", "Kleine Flaschen", "Glas", "Lieblicher Rotwein");
break;

case
"es":
$lang = "es";
$dishtype= array ("Entrantes", "Platos Principales de Carne", "Platos Principales de Pescado", "Postres", "Vino Blancos Dulces", "Media Botella", "Por Copas", "Vino Tinto Dulce");
break;

default :
$lang="en";
$dishtype= array ("Starters", "Meat Main Courses", "Fish Main Courses", "Desserts", "White Dessert Wine", "Half Bottles", "By The Glass", "Red Dessert Wine");

}

?>

Displaying an array variable in your template:

<h3><?php echo $dishtype[4] ?></h3>


As I’ve said before, be sure to add these snippets before your Doctype-declaration and set php to parse on input in your template

What happens when I use a category

On the site I worked on I used numbers for categories.
EE adds these to the URL so you’ll have to account for that in your template.
Use this code in your template when you are displaying cusom fields using a category:

<?php

if ( is_numeric("{segment_3}") )
{
$info
="{segment_4}";


} else {
    $info
="{segment_3}";


}
switch ($info)
{
case "en":
$lang="en";
break;

case
"de":
$lang = "de";
break;

case
"es":
$lang = "es";
break;

default :
$lang="en";

}

?>

This is pretty straightforward. The isnumeric functionhttp://es2.php.net/manual/en/function.is-numeric.php checks whether URL segment 3 is a number (i.e. you are on a page using a category) and puts the value of URL segment 4 (en. es. de) in your variable. Sorted.

Switching languages on every page

This is something that could come in handy when your visitors come across your site in german (for whatever reason) and want to read your content in english.
The following is a template you can embed in every page on your site to use as a “switcher”.
It sniffs out the URL of the page you’re on, removes the final URL segment (es, de, en) so you can add it yourself in an unordered html list.
Style using CSS to taste.

<?php

if ( is_numeric("{segment_3}") )
{
$info
="{segment_4}";


} else {
    $info
="{segment_3}";


}
switch ($info)
{
case "en":
$lang="en";
break;

case
"de":
$lang = "de";
break;

case
"es":
$lang = "es";
break;

default :
$lang="en";

}

?>

<?php
$pageurl
= substr(trim( $_SERVER['PHP_SELF']), 0, (strlen($_SERVER['PHP_SELF'])-3));
?>
<ul>
<
li><a href="http://www.mysiterocks.com<?php echo $pageurl ?>de/">|DE</a></li>
<
li><a href="http://www.mysiterocks.com<?php echo $pageurl ?>es/">|ES</a></li>
<
li><a href="http://www.mysiterocks.com<?php echo $pageurl ?>en/">|EN</a></li>
</
ul>

Again make sure that php is set to parse on input in this template.
Let’s say you call this template “language_switch” inside the “snippets” template group you can embed it like this:

I’m beginning to repeat myself but again the template in which you are embedding this subtemplate has to have php set to parse on input.

Multi language image replacement

Last one. On several pages I used image replacement to give nicer looking page titles.
Again this is fairly straightforward using the $lang variable.
Sample HTML code with EE tags:

{exp:weblog:entries weblog="yourblog" }
<h2 class="<?php echo $lang ?>" > {yourcustomfield_<?php echo $lang ?>}</h2>
{/exp:weblog:entries}

Sample CSS code using PHARK image replacementhttp://www.mezzoblue.com/tests/revised-image-replacement/;

h2 {


text
-indent: -9000px; padding-bottom: 35px;
margin-top: 35px;
}

h2
.en {


background
: url(http://www.mysiterocks.com/images/title_en.gif) no-repeat; }

h2.es {


background
: url(http://www.mysiterocks.com/images/title_es.gif) no-repeat; }

h2.de {
background
:url(http://www.mysiterocks.com/images/title_de.gif) no-repeat;
}

This speaks for itself, I guess, just use $lang as a “class” then set a different image for each class (en, es, de)
That’s all folks hope you can use this…

Category:Languages

Categories: