Page: 1, 2  Next

Language detection added to PHP browser detection script
jeffd
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
[moved from this thread [ Note: download PHP language detection script here]
[guinness wrote]
i'm working on another script that i adapted from another page. it's to auto-detect the language of the browser and then allow you to change language if you happen to want a different language. do you have any interest in working on this with me? you could post it on your site when your done, as i'm sure others would be interested in using such a script.

Here's a link to a forum where i started working on it. Wasn't getting much good help there though:

:: Code ::
 <?php

function lixlpixel_get_env_var($Var)
{
if(empty($GLOBALS[$Var]))
{
$GLOBALS[$Var]=(!empty($GLOBALS['_SERVER'][$Var]))?
$GLOBALS['_SERVER'][$Var]:
(!empty($GLOBALS['HTTP_SERVER_VARS'][$Var]))?
$GLOBALS['HTTP_SERVER_VARS'][$Var]:'';
}
}

function lixlpixel_detect_lang()
{
// Detect HTTP_ACCEPT_LANGUAGE & HTTP_USER_AGENT.
lixlpixel_get_env_var('HTTP_ACCEPT_LANGUAGE');
lixlpixel_get_env_var('HTTP_USER_AGENT');

$_AL=strtolower($GLOBALS['HTTP_ACCEPT_LANGUAGE']);
$_UA=strtolower($GLOBALS['HTTP_USER_AGENT']);

// Try to detect Primary language if several languages are accepted.
foreach($GLOBALS['_LANG'] as $K)
{
if(strpos($_AL, $K)===0)
return $K;
}

// Try to detect any language if not yet detected.
foreach($GLOBALS['_LANG'] as $K)
{
if(strpos($_AL, $K)!==false)
return $K;
}
foreach($GLOBALS['_LANG'] as $K)
{
if(preg_match("//[\[\( ]{$K}[;,_\-\)]//",$_UA))
return $K;
}

// Return default language if language is not yet detected.
return $GLOBALS['_DLANG'];
}

// Define default language.
$GLOBALS['_DLANG']='en';

// Define all available languages.
// WARNING: uncomment all available languages

$GLOBALS['_LANG'] = array(
'af', // afrikaans.
'ar', // arabic.
'bg', // bulgarian.
'ca', // catalan.
'cs', // czech.
'da', // danish.
'de', // german.
'el', // greek.
'en', // english.
'es', // spanish.
'et', // estonian.
'fi', // finnish.
'fr', // french.
'gl', // galician.
'he', // hebrew.
'hi', // hindi.
'hr', // croatian.
'hu', // hungarian.
'id', // indonesian.
'it', // italian.
'ja', // japanese.
'ko', // korean.
'ka', // georgian.
'lt', // lithuanian.
'lv', // latvian.
'ms', // malay.
'nl', // dutch.
'no', // norwegian.
'pl', // polish.
'pt', // portuguese.
'ro', // romanian.
'ru', // russian.
'sk', // slovak.
'sl', // slovenian.
'sq', // albanian.
'sr', // serbian.
'sv', // swedish.
'th', // thai.
'tr', // turkish.
'uk', // ukrainian.
'zh' // chinese.
);

// Redirect to the correct location.

//header('location:

http://22nd.org/'.lixlpixel_detect_lang().'/index.php'); // Example

Implementation
echo '<html><head></head><body>Welcome to 22nd.org.<br>We have

detected your preferred language is: '.lixlpixel_detect_lang().' If

this is correct, please proceed:</body></html>'; // For Demonstration

?>

[added code for reference purposes from other site, jeffd]
webmaster-talk.com/showthread.php?t=10868

I'd love to have your help, and hopefully I can make it worth your while.

Let me know. Thanks

-rob
Back to top
PHP browser Language detection
jeffd
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
From the code it looks like it would be a relatively simple task to add that as a feature of the browser detection script, I'd just add your function, with some modifications, to the primary script, then add one or two more output options, language 2 letter or full.

This seems like a pretty decent idea to add as a possible function to the main script.

Adding the features you're looking for is pretty easy, I'll do it and show it to you, I think it's easy anyway, except for testing, have to play with that a bit.
Back to top
guinness
Status: Interested
Joined: 05 Jul 2004
Posts: 12
Reply Quote
still working on the language script?
Back to top
jeffd
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
I had to collect some information, I'm going to add that as a module to the browser detection script, I got a more complete languages list, also there were a few factors I wasn't aware of.

Since the script will need to help as many users as possible it will need to return quite a bit of data, currently I"m planning to return the following:

array of values:

primary language, 2 letter abbrv
primary language, including language subversion, 5 letter abbr if available.
primary language, full language
primary language including full subversion

secondary language, and so on....

I'm redoing the whole website today, it's about 2/3 done, check it out, no iframes, home pages and free scripts section currently are switched over, rest of site will be switched today, hopefully.

I'm not sure I'll have time to do the language script, maybe if I finish today, maybe not. After today I won't have much time til tuesday.
Back to top
guinness
Status: Interested
Joined: 05 Jul 2004
Posts: 12
Reply Quote
cool. looks good so far. no worries if you can't get the language script done today. i certainly know what its like to have too many projects to possibly get them all done!

best,

-rob
Back to top
working script is ready
jeffd
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
I found a few hours and got that working, you can download the language detection script and give it a try, it's pretty easy to use.

I currently have two parameters possible, data or header, plus there is a spare parameter that could be used to tell it what page to send it to or something.

data will return an array of all the language values

:: Code ::
<?php
include('language_detection.inc');
$a_languages = get_languages( 'data', '' );
echo( 'main language id is: ' . $a_languages[0][0] . '<br />Language is: ' .  $a_languages[0][2]);
// print_r($a_languages) is for your testing, it prints out all the array values
print_r($a_languages);
?>


the first element of the array is the language, in this order (array items start with 0, not 1):
0. full language abbreviation, like en-ca
1. primary language abbreviation, like en
2. full language description, like English (Canada)
3. primary language description, like English.

if you do a print_r($a_languages); you would see something like this:

:: Code ::
Array
(
    [0] => Array
        (
            [0] => en-us
            [1] => en
            [2] => English (United States)
            [3] => English
        )

    [1] => Array
        (
            [0] => en
            [1] => en
            [2] => English
            [3] => English
        )

)


Keep in mind that the 'header' must be set the very first thing on your page, before any other code is output, like this:

:: Code ::
<?php
include('language_detection.inc');
// this will trigger a header redirect in the script
// you don't need to do anything else, although you'd have to
// modify the script to redirect to the right pages
get_languages( 'header', '' );
?>
<html>
<head>
<title>your test page</title>
.......


If you try outputting a header after anything, even a blank space, you will get an error.

If you make this mistake, you will get a header error message like this:

:: Code ::
Warning: Cannot modify header information - headers already sent by (output started at /usr/www/websites/yoursite/index.php:11) in /usr/www/websites/includes/programs/language_detection.inc on line 105

Back to top
guinness
Status: Interested
Joined: 05 Jul 2004
Posts: 12
Reply Quote
thanks a bunch!

I got the 'data' form of the script to run fine, but the 'header' function seems to have a bug. when i run that i get the following error:

Warning: Missing argument 2 for get_languages() in /home/httpd/vhosts/22nd.org/httpdocs/language_detection.inc on line 34

also, how would i got out restricting the function to only print part of the information from the array. For example if I want to my page to say:

Your browser is set to show this page in: English

How would I call up just the "English" word (or whatever language it was set to) when using a print command?

Thanks again! I will make sure to give you ample credit on my page!

-rob
Back to top
jeffd
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
:: Quote ::
Missing argument 2 for get_languages() in


That's because the function call was missing argument two, as php error messages go that's one of the more clear ones, I forgot to put that in, I fixed that in the above.

When the script function calls for a certain number of parameters, in this case two, the function call on the page must also have those two parameters, even if the second one is blank, like get_languages( 'data', '' );

:: Quote ::
how would i got out restricting the function to only print part of the information from the array. For example if I want to my page to say:


You select the array component you want to work with, this is a two dimensional array, that is, an array of arrays, the syntax for extracting data from a two dimensional array is this:

:: Code ::
<?php
include('language_detection.inc');
$main_language = get_languages( 'data', '');
echo $main_language[0][3];
?>


the primary language will be $main_language[0], that is, the first array in the array of language data.
the primary language string will be $main_language[0][3], that is, in the first language array, get the second dimension array, the one that actually contains the language data, and then get the last item in that secondary array, the primary root language.

I'll be adding a few features to that script, the ability to set cookies to maintain the language state across the site, etc, but the primary functionality is there now.

The print_r($main_language) is for testing of the script, and for debugging, you wouldn't use that one the actual page. That just prints out the whole array structure, and the data it contains
Back to top
superduper
Status: New User - Welcome
Joined: 19 Jul 2004
Posts: 2
Reply Quote
great piece of work :-) Looking forward to the planned extensions (cookies)
Back to top
jeffd
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
As soon as I can figure out a fairly reliable way to integrate cookies I'll have it up, glad you like the script, that might be of use to someone or other.
Back to top
Display posts from previous:   
Page: 1, 2  Next
All times are GMT - 8 Hours