there's a nice article in this months (Feb. 2003!!!) PC world (UK) (www.pcw.co.uk) on 
this.

see www.nigelwhitfield.com for details.

Best Wishes & Happy New Year

Paul Roberts
[EMAIL PROTECTED]
++++++++++++++++++++++++
----- Original Message ----- 
From: "Michael J. Pawlowsky" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, December 30, 2002 4:53 PM
Subject: [PHP] Multi-lingual Language Support in PHP



Keywords: Multiple, Language, PHP, $HTTP_ACCEPT_LANGUAGE

I'm just shooting this one up here for the Archives.  

Since I could not find a built in PHP function to access the preferred languages on a 
user I came up with this.
The original version acted as a http server in that you needed to have a different 
file for evey language (ie. index.php.en, index.php.fr, index.php.de).

I find that's a complicated way to work since if you need to update code you will have 
to do it in several files.

I prefer simply 

if (phpLang_current == 'fr') {
print "Ceci est francais";
} else (
// default to english
print "This is english";
}

If it's a large block of text you can have seperate include text files as in.


if (phpLang_current == 'fr') {
include("mytext.fr");
} else (
// default to english
include("mytext.en");
}


So in case anyone else ever needs this... Here it is...


<?php

// +--------------------------------------------------------------------------+
// | phpLang (simple version) based on phpLang version 0.5.0 - 2002/02/23     |
// | Modified by Michael J. Pawlowsky on 2002/12/30                           |
// | [EMAIL PROTECTED]                                                    |
// +--------------------------------------------------------------------------+
// | Copyright (c) 2000-2001 The phpHeaven-team                               |
// +--------------------------------------------------------------------------+
// | License:  GNU/GPL - http://www.gnu.org/copyleft/gpl.html                 |
// +--------------------------------------------------------------------------+
// | Original release available on phpHeaven:                                 |
// |    http://www.phpheaven.net/rubrique7.html?var_recherche=phpLang         |
// |                                                                          |
// | Authors:  Nicolas Hoizey <[EMAIL PROTECTED]>                         |
// |           Loïc Chapeaux <[EMAIL PROTECTED]>                             |
// +--------------------------------------------------------------------------+


// parameter to add in url
if(!defined('phpLang_urlParam'))
define('phpLang_urlParam', 'lang');

// path to image files
if(!defined('phpLang_images'))
define('phpLang_images', '/flags/');

// indicates if it should put a cookie
if(!defined('phpLang_useCookie'))
define('phpLang_useCookie', true);


// list of available languages, order it as you need
$phpLang_languages = array(
"en([-_][[:alpha:]]{2})?|english" => array('en', 'english'),
"fr([-_][[:alpha:]]{2})?|french" => array('fr', 'french'),
);



$HTTP_ACCEPT_LANGUAGE = getenv('HTTP_ACCEPT_LANGUAGE');
$HTTP_USER_AGENT = getenv('HTTP_USER_AGENT');


// language code detection
function phpLang_detectLanguage($str, $from)
{
$ext = '';
reset($GLOBALS['phpLang_languages']);
while($ext == '' && list($key, $name) = each($GLOBALS['phpLang_languages'])) {
if (($from == 1 && eregi("^".$key."$",$str)) || ($from == 2 && 
eregi("(\(|\[|;[[:space:]])".$key."(;|\]|\))",$str))) {
$ext = $name[0];
}
}

return $ext;
}


// finds the appropriate language file
if (isset($HTTP_GET_VARS[phpLang_urlParam])) {
// a language as been chosen by the user
define('phpLang_current', $HTTP_GET_VARS[phpLang_urlParam]);
}

if (!defined('phpLang_current') && phpLang_useCookie && 
isset($HTTP_COOKIE_VARS['phpLangCookie'])) {
// a language as been found in a cookie previously set
define('phpLang_current', $HTTP_COOKIE_VARS['phpLangCookie']);
}

if (!defined('phpLang_current') && isset($HTTP_ACCEPT_LANGUAGE) && 
trim($HTTP_ACCEPT_LANGUAGE) != '') {
// looks at the languages accepted by the browser
$accepted = explode(',', $HTTP_ACCEPT_LANGUAGE);
while(!defined('phpLang_current') && list($key, $name) = each($accepted)) {
$code = explode(';', $name);
$ext = phpLang_detectLanguage($code[0], 1);
if(file_exists(phpLang_localizedFileName($ext))) {
define('phpLang_current', $ext);
}
}
}

if (!defined('phpLang_current') && isset($HTTP_USER_AGENT) && trim($HTTP_USER_AGENT) 
!= '') {
// looks at the browser's identification
$ext = phpLang_detectLanguage($HTTP_USER_AGENT, 2);
define('phpLang_current', $ext);
}

if(!defined('phpLang_current')) {
// if no language yet found, chose the first existing in site's list
reset($phpLang_languages);
while(!defined('phpLang_current') && list($key, $name) = each($phpLang_languages)) {
define('phpLang_current', $name[0]);
}
}

// detection done, cookie update

if(defined('phpLang_current')) {
if(phpLang_useCookie) {
// set a cookie expiring in one year for current language
setcookie('phpLangCookie', phpLang_current, time() + 60*60*24*365, "/", "rconline.ca");
}
// defines a string to add at the end of each link
define('phpLang_link', phpLang_urlParam.'='.phpLang_current);
} else {
// no language found
define('phpLang_current', '');
define('phpLang_link', phpLang_urlParam.'=');
}

// function that adds the flags with links for existing files
// give as first parameter the HTML string to put between each flag
function AddFlags($between = "", $showCurrent = false)
{
$REQUEST_URI = $_SERVER['REQUEST_URI'];

reset($GLOBALS["phpLang_languages"]);
$temp = "";
while(list($key, $name) = each($GLOBALS["phpLang_languages"])) {
if($showCurrent || $name[0] != phpLang_current) {

if (strchr($REQUEST_URI, '?')){
$seperator="&";
} else {
$seperator="?";
}
echo($temp.'<a href="'.$REQUEST_URI.$seperator.phpLang_urlParam.'='.$name[0].'">');
echo('<img src="'.phpLang_images.$name[0].'.gif" border="0" align="middle" width="24" 
height="16" alt="'.$name[1].'" />');
echo('</a>');
$temp = $between;
}
}
}


// then, you can use two constants in your scripts :
//   phpLang_current : current language code
//   phpLang_link    : add this in the links after a '?' or a '&'
?>









-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to