Link to home
Start Free TrialLog in
Avatar of andieje
andieje

asked on

Help with internationalisation in php

Hi

I have a cusomter who wants a website with a dropdown with a list of countries and the website is displayed in the appropriate language

1) Is  internationisation the right world for this? Quoting from http://www.w3.org/standards/webdesign/i18n

http://www.w3.org/standards/webdesign/i18n

2) Secondly is this possible on a single website as other internationalised websites send you off to a differnt website for the country you chooose

3) I have seen gettext. I will look into this, Is this a possible solution?


I can only imagine you have every phrase on the website stored in each language and depending on what language the user picked from the drop down you have a massive switch statement on country to go to the database and get all the statements for that page in the right language

thanks a lot
Avatar of Ahmed Merghani
Ahmed Merghani
Flag of Sudan image

1- Yes that is the right word "internationalization" with "z" instead of "s". Some time we call it "localization" when the target language is "Arabic".
2- Yes and usually the other websites if we suppose the first domain is "mydomain.us", others may be as "mydomain.uk" and so on.

If you will go with the option for multiple website, no need for "internationalization" because each website will be different from the others. Other wise, no need for multiple website, and instead there will be one website with different languages.
NOTE, you will need to consider "LTR" and "RTL" language in your internationalization.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of andieje
andieje

ASKER

Yes it should be z - i was wrongly bought up to believe that americans use ize and english use ise but both are acceptable according to the OED though ize is the english version :)

Ahmed - it looks to me like you are saying in point to that we go to difffernt websites with different domains.  "if we suppose the first domain is "mydomain.us", others may be as "mydomain.uk" and so on."

I need to be able to do this one website, Hope that's clear

Ray - i cant load your page but i will look at it when it loads and come back - thanks. The page is just hanging
Sorry - I don't know why you would have a hard time getting to the article.  Here is a more-or-less faithful reproduction of what you would see there.  HTH, ~Ray

Introduction
This article describes the general design elements of a multilingual web site. The site will be very simple, but all the important parts of the design will be present. By clicking on the flags at the bottom, our clients can change the languages.

The Design of the Directory Structure
We want to be able to support many languages, but with a single, simple design.  A directory structure that makes sense for this is in place at www.php.net.  The PHP folks have organized the online manual this way, where XX indicates the language.

php.net/manual/XX/function.var-dump.php

The standard terminology for languages is prescribed by ISO639, an evolving standard that employs short abbreviation codes.  See http://en.wikipedia.org/wiki/ISO_639-1 for more information.  For our example we will develop our web site in three languages: English, French and German.  The abbreviations we will use for English, French and German are en, fr, and de (from the endonym Deutsch), respectively.  

Our directory structure will look like this with three separate language directories.

www_root
|
|__ image/
|__ index.php
|__ language.php
|__ template.php
|
|__ en
|   |__index.php
|
|__ fr
|   |__index.php
|
|__ de
     |__index.php

If we wanted to add a directory for a fourth language, for example Spanish (/es/), we would be able to copy any of the language directories and translate the text into Spanish.

What Language Should We Use?
We might want to consider using an IP-to-Location service to choose the language, but in practice this is problematical.  Consider the French who travel in Germany.  Even if they were accessing the web site from a German city, it would not be reasonable to assume that the French now wanted to use German language.  So instead of using geolocation to make decisions, we will give the client a highly recognizable visual clue, the national flags.  We will make these flag icons into links that activate a common script.  The common script will recognize the language, set a cookie and redirect to the appropriate language directory.  We can find images of the national flags here:
http://en.wikipedia.org/wiki/National_flags

And we can use a drawing program like Photoshop to create our icons.  We will use English as the assumed language; our French and German clients will be able to get their languages with a single click on the flag icon.

The Home Page in the WWW Root
Our home page will try to recognize the client's preferred language by looking for a long-lived cookie. If the cookie is not set or is invalid, we will choose English.  We will redirect the browser to the appropriate directory.

<?php // /index.php
error_reporting(E_ALL);

// THE DEFAULT LANGUAGE
$lang = 'en';

// IF WE REMEMBER A LANGUAGE PREFERENCE
if (isset($_COOKIE["lang"]))
{
    switch($_COOKIE["lang"])
    {
        case 'en' : $lang = 'en';
        break;

        case 'fr' : $lang = 'fr';
        break;

        case 'de' : $lang = 'de';
        break;
    }
}

// REDIRECT TO THE LANGUAGE VERSION OF THE SITE
$home = $lang . DIRECTORY_SEPARATOR;
header("Location: $home");
exit;

Open in new window


The Language Page
In order to give our client the ability to change languages, we need a language selection page.  Instead of using the value in $_COOKIE, it will use the URL argument in $_GET.  It is no accident that this script looks very much like the home page of the web root.

<?php // /language.php
error_reporting(E_ALL);

// THE DEFAULT LANGUAGE
$lang = 'en';

// IF WE ARE GIVEN A LANGUAGE PREFERENCE
if (isset($_GET["lang"]))
{
    switch(strtolower($_GET["lang"]))
    {
        case 'en' : $lang = 'en';
        break;

        case 'fr' : $lang = 'fr';
        break;

        case 'de' : $lang = 'de';
        break;
    }
}

// SET THE LANGUAGE FOR ABOUT A YEAR
setcookie('lang', $lang, time()+365*24*60*60, DIRECTORY_SEPARATOR);

// REDIRECT TO THE LANGUAGE VERSION OF THE SITE
$home = $lang . DIRECTORY_SEPARATOR;
header("Location: $home");
exit;

Open in new window


Handling Common Elements
Our image files are common to all language versions.  Similarly, our template script will be the same for all languages, so we will set up the template as a common script that can be included in each version of the web page.  Near the bottom of this script you will see where we visualize the cookie.  When we are able to see data like this, it makes debugging much easier.

<?php // TEMPLATE
error_reporting(E_ALL);

// OUR LANGUAGES
$languages = array
( 'en' => 'English'
, 'fr' => 'French'
, 'de' => 'Deutsch'
)
;

// CREATE THE PAGE TEMPLATE USING HEREDOC NOTATION
$html_1 = <<<TEMPLATE
<!DOCTYPE html>
<html dir="ltr" lang="$lang">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" />
</head>
<body>
<h1>$head <img src="../image/flag_$lang.png" /></h1>
<p>
<img src="../image/piglet.png" />
</p>
TEMPLATE;



// CREATE OUR FLAG LINKS
$html_2 = $text . '<br/>';
foreach ($languages as $abbr => $language)
{
    // CREATE OUR LINKS USING HEREDOC NOTATION
    $choices = <<<CHOICES
<a title="$language" href="../language.php?lang=$abbr"><img src="../image/flag_$abbr.png" /></a>
CHOICES;

    // APPEND EACH FLAG LINK TO THE HTML STRING
    $html_2 .= $choices . PHP_EOL;
}

// WRITE THE HTML STRINGS TO THE BROWSER
echo $html_1;
echo $html_2;

// SHOW THE COOKIE IF IT HAS BEEN SET
if (!empty($_COOKIE))
{
    if (isset($_COOKIE["lang"]))
    {
        echo "<pre>COOKIE: ";
        var_dump($_COOKIE["lang"]);
    }
}

Open in new window


Different Content for Different Languages
The use of "heredoc" notation makes PHP templating into a very simple task.  We simply assign values to the variables that are embedded in the heredoc blocks.  Learn more about heredoc and nowdoc on the PHP web site:
http://php.net/manual/en/language.types.string.php.  

You may find, as I do, that the heredoc notation is much less confusing than trying to deal with escaped quotation marks, apostrophes, and concatenation operators.

Here are our three different languages.

<?php // /de/index.php DEUTSCH
error_reporting(E_ALL);

// SET LANGUAGE AND TEXT
$lang = 'de';
$head = 'Schweinchen';
$text = 'Sprache wählen';

// LOAD THE TEMPLATING SCRIPT
require_once('../template.php');

Open in new window


<?php // /en/index.php ENGLISH
error_reporting(E_ALL);

// SET LANGUAGE AND TEXT
$lang = 'en';
$head = 'Piglet';
$text = 'Choose Language';

// LOAD THE TEMPLATING SCRIPT
require_once('../template.php');

Open in new window


<?php // /fr/index.php FRENCH
error_reporting(E_ALL);

// SET LANGUAGE AND TEXT
$lang = 'fr';
$head = 'Petit Cochon';
$text = 'Choisissez une langue';

// LOAD THE TEMPLATING SCRIPT
require_once('../template.php');

Open in new window


From This Design...
We could certainly do many more interesting things.  For example, the variables that make up our pages, $lang, $head, $text could be much more extensive (In a real-world example, they would probably come from a data base.)  If we wanted to add another language, it would be fairly easy.  And most importantly, the new translation could be worked on without disrupting any of the existing translations.  Once our new translation is ready for publication, we would install a new flag icon, and we would make a simple change to the template script to add the new element to the $languages array.

The combination of a common template and separate language directories makes this a flexible design with an intuitive client interface.

See it in Action
http://iconoun.com/demo/polyglot/

Afterword
Since this article was conceived, the use of UTF-8 character encoding has grown greatly.  Some Western European characters can collide with the UTF-8 signal byte, and a consequence of the increasing popularity of UTF-8 is an increasing incidence of character set collisions.  The usual symptom is a burst of garbage characters on the browser screen.  If you find this happening to you, please read this article on Character Sets.

Useful References
http://www.omniglot.com/language/time/seasons.htm
http://www.omniglot.com/language/time/months.htm
http://www.omniglot.com/language/time/days.htm
http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
andieje I think Ray article is perfect and this is what you want.
@Ray I have a little comment in your article. If you add paragraph handling the "RTL" language like "arabic", that will be awesome.
Ahmed: Good suggestion!
Avatar of andieje

ASKER

super thanks
Thanks for the points and best of luck with the project! ~Ray