NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page PHP Client Language Detection

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search
6 members in live chat ~  


Reply
 
LinkBack Thread Tools
Old 01-23-2011, 06:37 AM THREAD STARTER               #1 (permalink)
 
BillyConnite's Avatar
Join Date: Jul 2005
Location: Coffs H, Australia
Posts: 3,456
BillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond reputeBillyConnite has a reputation beyond repute
 


Wildlife Parkinson's Disease Parkinson's Disease

Cool PHP Client Language Detection


Hey all,

Found a nice snippet here. It parses the server variable HTTP_ACCEPT_LANGUAGE and grabs the preferred language of the browser client. This can be very useful when targeting certain languages/countries.
????: NamePros.com http://www.namepros.com/code/698172-php-client-language-detection.html

If you use an advertising service that pays only for views from a certain country, you're not getting paid for a great chunk of your bandwidth usage to other countries. You can use this to display different ads for different languages, or redirect to a translated version of your site. It's got many uses.

PHP Code:
<?php
/*
Script Name: Full Operating system language detection
Author: Harald Hope, Website: [url]http://techpatterns.com/[/url]
Script Source URI: [url]http://techpatterns.com/downloads/php_language_detection.php[/url]
Version 0.3.6
Copyright (C) 8 December 2008

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Get the full text of the GPL here: [url]http://www.gnu.org/licenses/gpl.txt[/url]

Coding conventions:
[url]http://cvs.sourceforge.net/viewcvs.py/phpbb/phpBB2/docs/codingstandards.htm?rev=1.3[/url]
*/

/*
Changes:
0.3.6 - added possible $feature values to comment header section
*/

/******************************************
Script is currently set to accept 2 parameters, triggered by $feature value.
for example, get_languages( 'data' ):
1. 'header' - sets header values, for redirects etc. No data is returned
2. 'data' - for language data handling, ie for stats, etc.
    Returns an array of the following 4 item array for each language the os supports:
    1. full language abbreviation, like en-ca
    2. primary language, like en
    3. full language string, like English (Canada)
    4. primary language string, like English
*******************************************/

// choice of redirection header or just getting language data
// to call this you only need to use the $feature parameter
function get_languages$feature$spare='' )
{
    
// get the languages
    
$a_languages languages();
    
$index '';
    
$complete '';
    
$found false;// set to default value
    //prepare user language array
    
$user_languages = array();

    
//check to see if language is set
    
if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) )
    {
        
$languages strtolower$_SERVER["HTTP_ACCEPT_LANGUAGE"] );
        
// $languages = ' fr-ch;q=0.3, da, en-us;q=0.8, en;q=0.5, fr;q=0.3';
        // need to remove spaces from strings to avoid error
        
$languages str_replace' '''$languages );
        
$languages explode","$languages );
        
//$languages = explode( ",", $test);// this is for testing purposes only

        
foreach ( $languages as $language_list )
        {
            
// pull out the language, place languages into array of full and primary
            // string structure:
            
$temp_array = array();
            
// slice out the part before ; on first step, the part before - on second, place into array
            
$temp_array[0] = substr$language_list0strcspn$language_list';' ) );//full language
            
$temp_array[1] = substr$language_list0);// cut out primary language
            //place this array into main $user_languages language array
            
$user_languages[] = $temp_array;
        }

        
//start going through each one
        
for ( $i 0$i count$user_languages ); $i++ )
        {
            foreach ( 
$a_languages as $index => $complete )
            {
                if ( 
$index == $user_languages[$i][0] )
                {
                    
// complete language, like english (canada)
                    
$user_languages[$i][2] = $complete;
                    
// extract working language, like english
                    
$user_languages[$i][3] = substr$complete0strcspn$complete' (' ) );
                }
            }
        }
    }
    else
// if no languages found
    
{
        
$user_languages[0] = array( '','','','' ); //return blank array.
    
}
    
// print_r($user_languages);
    // return parameters
    
if ( $feature == 'data' )
    {
        return 
$user_languages;
    }

    
// this is just a sample, replace target language and file names with your own.
    
elseif ( $feature == 'header' )
    {
        switch ( 
$user_languages[0][1] )// get default primary language, the first one in array that is
        
{
            case 
'en':
                
$location 'english.php';
                
$found true;
                break;
            case 
'sp':
                
$location 'spanish.php';
                
$found true;
                break;
            default:
                break;
        }
        if ( 
$found )
        {
            
header("Location: $location");
        }
        else
// make sure you have a default page to send them to
        
{
            
header("Location: default.php");
        }
    }
}

function 
languages()
{
// pack abbreviation/language array
// important note: you must have the default language as the last item in each major language, after all the
// en-ca type entries, so en would be last in that case
    
$a_languages = array(
    
'af' => 'Afrikaans',
    
'sq' => 'Albanian',
    
'ar-dz' => 'Arabic (Algeria)',
    
'ar-bh' => 'Arabic (Bahrain)',
    
'ar-eg' => 'Arabic (Egypt)',
    
'ar-iq' => 'Arabic (Iraq)',
    
'ar-jo' => 'Arabic (Jordan)',
    
'ar-kw' => 'Arabic (Kuwait)',
    
'ar-lb' => 'Arabic (Lebanon)',
    
'ar-ly' => 'Arabic (libya)',
    
'ar-ma' => 'Arabic (Morocco)',
    
'ar-om' => 'Arabic (Oman)',
    
'ar-qa' => 'Arabic (Qatar)',
    
'ar-sa' => 'Arabic (Saudi Arabia)',
    
'ar-sy' => 'Arabic (Syria)',
    
'ar-tn' => 'Arabic (Tunisia)',
    
'ar-ae' => 'Arabic (U.A.E.)',
    
'ar-ye' => 'Arabic (Yemen)',
    
'ar' => 'Arabic',
    
'hy' => 'Armenian',
    
'as' => 'Assamese',
    
'az' => 'Azeri',
    
'eu' => 'Basque',
    
'be' => 'Belarusian',
    
'bn' => 'Bengali',
    
'bg' => 'Bulgarian',
    
'ca' => 'Catalan',
    
'zh-cn' => 'Chinese (China)',
    
'zh-hk' => 'Chinese (Hong Kong SAR)',
    
'zh-mo' => 'Chinese (Macau SAR)',
    
'zh-sg' => 'Chinese (Singapore)',
    
'zh-tw' => 'Chinese (Taiwan)',
    
'zh' => 'Chinese',
    
'hr' => 'Croatian',
    
'cs' => 'Czech',
    
'da' => 'Danish',
    
'div' => 'Divehi',
    
'nl-be' => 'Dutch (Belgium)',
    
'nl' => 'Dutch (Netherlands)',
    
'en-au' => 'English (Australia)',
    
'en-bz' => 'English (Belize)',
    
'en-ca' => 'English (Canada)',
    
'en-ie' => 'English (Ireland)',
    
'en-jm' => 'English (Jamaica)',
    
'en-nz' => 'English (New Zealand)',
    
'en-ph' => 'English (Philippines)',
    
'en-za' => 'English (South Africa)',
    
'en-tt' => 'English (Trinidad)',
    
'en-gb' => 'English (United Kingdom)',
    
'en-us' => 'English (United States)',
    
'en-zw' => 'English (Zimbabwe)',
    
'en' => 'English',
    
'us' => 'English (United States)',
    
'et' => 'Estonian',
    
'fo' => 'Faeroese',
    
'fa' => 'Farsi',
    
'fi' => 'Finnish',
    
'fr-be' => 'French (Belgium)',
    
'fr-ca' => 'French (Canada)',
    
'fr-lu' => 'French (Luxembourg)',
    
'fr-mc' => 'French (Monaco)',
    
'fr-ch' => 'French (Switzerland)',
    
'fr' => 'French (France)',
    
'mk' => 'FYRO Macedonian',
    
'gd' => 'Gaelic',
    
'ka' => 'Georgian',
    
'de-at' => 'German (Austria)',
    
'de-li' => 'German (Liechtenstein)',
    
'de-lu' => 'German (Luxembourg)',
    
'de-ch' => 'German (Switzerland)',
    
'de' => 'German (Germany)',
    
'el' => 'Greek',
    
'gu' => 'Gujarati',
    
'he' => 'Hebrew',
    
'hi' => 'Hindi',
    
'hu' => 'Hungarian',
    
'is' => 'Icelandic',
    
'id' => 'Indonesian',
    
'it-ch' => 'Italian (Switzerland)',
    
'it' => 'Italian (Italy)',
    
'ja' => 'Japanese',
    
'kn' => 'Kannada',
    
'kk' => 'Kazakh',
    
'kok' => 'Konkani',
    
'ko' => 'Korean',
    
'kz' => 'Kyrgyz',
    
'lv' => 'Latvian',
    
'lt' => 'Lithuanian',
    
'ms' => 'Malay',
    
'ml' => 'Malayalam',
    
'mt' => 'Maltese',
    
'mr' => 'Marathi',
    
'mn' => 'Mongolian (Cyrillic)',
    
'ne' => 'Nepali (India)',
    
'nb-no' => 'Norwegian (Bokmal)',
    
'nn-no' => 'Norwegian (Nynorsk)',
    
'no' => 'Norwegian (Bokmal)',
    
'or' => 'Oriya',
    
'pl' => 'Polish',
    
'pt-br' => 'Portuguese (Brazil)',
    
'pt' => 'Portuguese (Portugal)',
    
'pa' => 'Punjabi',
    
'rm' => 'Rhaeto-Romanic',
    
'ro-md' => 'Romanian (Moldova)',
    
'ro' => 'Romanian',
    
'ru-md' => 'Russian (Moldova)',
    
'ru' => 'Russian',
    
'sa' => 'Sanskrit',
    
'sr' => 'Serbian',
    
'sk' => 'Slovak',
    
'ls' => 'Slovenian',
    
'sb' => 'Sorbian',
    
'es-ar' => 'Spanish (Argentina)',
    
'es-bo' => 'Spanish (Bolivia)',
    
'es-cl' => 'Spanish (Chile)',
    
'es-co' => 'Spanish (Colombia)',
    
'es-cr' => 'Spanish (Costa Rica)',
    
'es-do' => 'Spanish (Dominican Republic)',
    
'es-ec' => 'Spanish (Ecuador)',
    
'es-sv' => 'Spanish (El Salvador)',
    
'es-gt' => 'Spanish (Guatemala)',
    
'es-hn' => 'Spanish (Honduras)',
    
'es-mx' => 'Spanish (Mexico)',
    
'es-ni' => 'Spanish (Nicaragua)',
    
'es-pa' => 'Spanish (Panama)',
    
'es-py' => 'Spanish (Paraguay)',
    
'es-pe' => 'Spanish (Peru)',
    
'es-pr=> 'Spanish (Puerto Rico)',
    
'es-us' => 'Spanish (United States)',
    
'es-uy' => 'Spanish (Uruguay)',
    
'es-ve' => 'Spanish (Venezuela)',
    
'es' => 'Spanish (Traditional Sort)',
    
'sx' => 'Sutu',
    
'sw' => 'Swahili',
    
'sv-fi' => 'Swedish (Finland)',
    
'sv' => 'Swedish',
    
'syr' => 'Syriac',
    
'ta' => 'Tamil',
    
'tt' => 'Tatar',
    
'te' => 'Telugu',
    
'th' => 'Thai',
    
'ts' => 'Tsonga',
    
'tn' => 'Tswana',
    
'tr' => 'Turkish',
    
'uk' => 'Ukrainian',
    
'ur' => 'Urdu',
    
'uz' => 'Uzbek',
    
'vi' => 'Vietnamese',
????: NamePros.com http://www.namepros.com/showthread.php?t=698172
    
'xh' => 'Xhosa',
    
'yi' => 'Yiddish',
    
'zu' => 'Zulu' );

    return 
$a_languages;
}
?>
Note: get_languages will return an array of '','','','' if the server variable is not set.

Anyway, found it very useful, hope you do too.
BillyConnite is offline   Reply With Quote
Old 02-27-2011, 07:25 PM   #2 (permalink)
If only you knew...
 
maximum's Avatar
Join Date: Oct 2005
Location: Inside your head...
Posts: 990
maximum has a reputation beyond reputemaximum has a reputation beyond reputemaximum has a reputation beyond reputemaximum has a reputation beyond reputemaximum has a reputation beyond reputemaximum has a reputation beyond reputemaximum has a reputation beyond reputemaximum has a reputation beyond reputemaximum has a reputation beyond reputemaximum has a reputation beyond reputemaximum has a reputation beyond repute
 


Child Abuse Special Olympics Save a Life Baby Health Autism
nice one!
__________________
--- The greatest truths ever told, and the greatest lies ever told, all consist of exactly the same three words:
"I LOVE YOU"
--- The best say little, only say what is important.....then they shut up and sit down.
maximum is offline   Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.namepros.com/code/698172-php-client-language-detection.html
Posted By For Type Date
php language | BoardReader This thread Refback 03-01-2011 03:49 AM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHPCM.com - PHP Client Manager - 6 years old - 5L .com nielsencl Domains For Sale - Auctions 19 08-18-2010 01:00 PM
TOP 1st class IDN`s! dont miss to see! isi007 IDN Domains For Sale 1 03-07-2009 12:35 PM
Dan's snippets Daniel Programming 4 12-20-2008 03:12 AM
Proxy Detection in php axilant CODE 1 06-25-2004 05:24 PM

 
All times are GMT -7. The time now is 07:39 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger