Detect Locale with javascript

Language detectionThere may be times where you would want to detect the language of the users browser. I would normally use php to detect the users browser language. However, when this is not possible, it can also be done using the following javascript:

 thelocale = 'en-GB'; // set default locale
if ( navigator ) { if ( navigator.language ) {// for netscape thelocale = navigator.language; } else if ( navigator.browserLanguage ) {// internet explorer 5.5 or above
        thelocale = navigator.browserLanguage;
    }
    else if ( navigator.systemLanguage ) {// windows specific
        thelocale = navigator.systemLanguage;
    }
    else if ( navigator.userLanguage ) {
        thelocale = navigator.userLanguage;
    }
}

PHP Language Detection

There is a very simple method to detect what language the users web browser is using.

The following code:


//session variable ensures it's available
//throughout current session

$_SESSION['lang'] = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
echo $_SESSION['lang'];

Multilingual
Will give you the correct two letter ISO code for the language the browser is using.

ISO Language codes

The ISO language code can be used to declare the language of a Web page (or a portion of a Web page) or identify the default browser language.

Language ISO Code
Abkhazian ab
Afar aa
Afrikaans af
Albanian sq
Amharic am
Arabic ar
Aragonese an
Armenian hy
Assamese as
Aymara ay
Azerbaijani az
Bashkir ba
Basque eu
Bengali (Bangla) bn
Bhutani dz
Bihari bh
Bislama bi
Breton br
Bulgarian bg
Burmese my
Byelorussian (Belarusian) be
Cambodian km
Catalan ca
Cherokee
Chewa
Chinese (Simplified) zh
Chinese (Traditional) zh
Corsican co
Croatian hr
Czech cs
Danish da
Divehi
Dutch nl
Edo
English en
Esperanto eo
Estonian et
Faeroese fo
Farsi fa
Fiji fj
Finnish fi
Flemish
French fr
Frisian fy
Fulfulde
Galician gl
Gaelic (Scottish) gd
Gaelic (Manx) gv
Georgian ka
German de
Greek el
Greenlandic kl
Guarani gn
Gujarati gu
Haitian Creole ht
Hausa ha
Hawaiian
Hebrew he, iw
Hindi hi
Hungarian hu
Ibibio
Icelandic is
Ido io
Igbo
Indonesian id, in
Interlingua ia
Interlingue ie
Inuktitut iu
Inupiak ik
Irish ga
Italian it
Japanese ja
Javanese jv
Kannada kn
Kanuri
Kashmiri ks
Kazakh kk
Kinyarwanda (Ruanda) rw
Kirghiz ky
Kirundi (Rundi) rn
Konkani
Korean ko
Kurdish ku
Laothian lo
Latin la
Latvian (Lettish) lv
Limburgish ( Limburger) li
Lingala ln
Lithuanian lt
Macedonian mk
Malagasy mg
Malay ms
Malayalam ml
Maltese mt
Maori mi
Marathi mr
Moldavian mo
Mongolian mn
Nauru na
Nepali ne
Norwegian no
Occitan oc
Oriya or
Oromo (Afan, Galla) om
Papiamentu
Pashto (Pushto) ps
Polish pl
Portuguese pt
Punjabi pa
Quechua qu
Rhaeto-Romance rm
Romanian ro
Russian ru
Sami (Lappish)
Samoan sm
Sangro sg
Sanskrit sa
Serbian sr
Serbo-Croatian sh
Sesotho st
Setswana tn
Shona sn
Sichuan Yi ii
Sindhi sd
Sinhalese si
Siswati ss
Slovak sk
Slovenian sl
Somali so
Spanish es
Sundanese su
Swahili (Kiswahili) sw
Swedish sv
Syriac
Tagalog tl
Tajik tg
Tamazight
Tamil ta
Tatar tt
Telugu te
Thai th
Tibetan bo
Tigrinya ti
Tonga to
Tsonga ts
Turkish tr
Turkmen tk
Twi tw
Uighur ug
Ukrainian uk
Urdu ur
Uzbek uz
Venda
Vietnamese vi
Volapük vo
Wallon wa
Welsh cy
Wolof wo
Xhosa xh
Yi
Yiddish yi, ji
Yoruba yo
Zulu zu

Hiding content – accessibility consideration


If you need to hide content but still want it available to screen readers, avoid using  display:none in your CSS. display:none means exactly what it says – NONE! Thus the element will also be hidden from all screen readers and all browsers.

To have the element hidden from browsers, but still available to screen readers use techniques such as

position: absolute;
left: -9999px;
or
text-indent: -9999px;
More methods can be found here: Hiding Content for Accessibility.

If you are hiding content temporarily, it’s most likely that you will be using jQuery to make it visible. This also means that to users who do not have Javascript enabled browsers, the element will always be hidden.

This can be dealt with by using jQuery to hide the element on page load, instead of using the CSS methods. Therefore, if the browser does not have javascript enabled, the element will always be visible.

Copy file from remote server

Use this script to copy a file from a remote server and save on your own server.



<?php
$feed = "http://www.domain.com/thefile.pdf";// url to original

$copydir = "/home/webdev/public_html/copies/"; //directory to copy to (CHMOD 777)

$data = file_get_contents($feed);

$file = fopen($copydir . "thefile.pdf", "w+");

fputs($file, $data);

fclose($file);

?>

Tags: , , ,
Posted in php by admin. No Comments

Use php in .xml files


XML with PHP
Using PHP and MySQL in XML sitemaps is quite easy.

The first step is to use the AddType directive.

The second step is to add the following code to the top of your .xml file:

<?php
session_start();
unset($_SESSION['aErrors']);
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" ?>';
?>

Tags: , ,
Posted in mysql php by admin. No Comments

AddType Directive


The AddType directive is added to the .htaccess file in the directory, to modify the default mimetypes of the files you wish to be effected.

A common use of the AddType directive is if you wish to use PHP in .xml files for dynamic xml sitemaps or .html files.

The syntax is quite simple:

AddType mime-type file-extension

So, if we wish to include PHP in our .html file, we add the following to the .htaccess file:

AddType application/x-httpd-php .xml
AddType application/x-httpd-php .html

If using PHP version 5.x you may also wish to try:

AddType application/x-httpd-php5 .html
AddType application/x-httpd-php5 .xml

Tags: , ,
Posted in htaccess php by admin. No Comments

Force compatibility viewer

A few months ago I created a large CMS website for a client using HTML5 plenty of jQuery. I thought it looked great. However, she informed me that it wasn’t rendering properly. It looked fine to me. She was using the latest version of IE, IE9 – I wasn’t. The answer was simple, force the compatibility view on IE9 to render as IE8. Just add this to yoursection:

<meta http-equiv="X-UA-Compatible" content="IE=8">

Posted in html5 jquery by admin. No Comments

PHP/MySQL connection script

Many of us use more than one server during the entire project lifecycle. To ensure that you are able to connect to the various versions of the mysql database on each server, use this php connection script:

<?php

$host = $_SERVER['HTTP_HOST']; // which server is it running on

switch ($host)
{
case ‘localhost’:
$server=”localhost”;
$user=”root”;
$db=”database;
$password=”";
break;

case ‘www.webaddress.com’:
$server=”mysqlserver”;
$user=”username”;
$db=”database”;
$password=”password”;
break;

case ‘www.address2.com’:
$server=”mysqlserver2″;
$user=”username”;
$db=”database”;
$password=”password”;
break;

default:
$server=”";
$user=”";
$db=”";
$password=”";
}

$link = mysql_connect($server, $user, $password);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}

$selected = mysql_select_db($db,$link)
or die(“Could not select database<br/><br/>” . mysql_error());

?>

Tags: , ,
Posted in mysql php by admin. No Comments