Solved

Help with internationalisation in php

Posted on 2014-11-08
8
81 Views
Last Modified: 2014-11-18
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
0
Comment
Question by:andieje
  • 4
  • 2
  • 2
8 Comments
 
LVL 8

Expert Comment

by:Ahmed Merghani
ID: 40430317
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.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40430337
We have an article here that shows a design pattern that is appropriate for internationalization:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_8910-A-Polyglot-Web-Site-in-PHP.html
0
 

Author Comment

by:andieje
ID: 40430375
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40430407
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
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 8

Expert Comment

by:Ahmed Merghani
ID: 40430925
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40431261
Ahmed: Good suggestion!
0
 

Author Closing Comment

by:andieje
ID: 40451141
super thanks
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40451199
Thanks for the points and best of luck with the project! ~Ray
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now