PHP. Dynamic Internationalization and Localization with the JQuery Globalization Plugin

This small PHP snippet dynamically includes the JQuery globalization plugin (globalize.js) and the user specific locale and culture to the webpage. This is done by checking the headers sent by the browser, and finding out the user's preferred language. For instance, if the Accept-Language field in a header sent by Firefox is "en-us, en;q=0.5", this means the primary culture of the user is "en-us" with the value "q=1.0", the safe fail over for the system is "en" neutral ad has the value "q=0.5". Then the snippet will load the "globalize.culture.en-US.js" and "globalize.culture.en.js" to the webpage and finally will initiate the culture on browser webpage load.

// Get user's preferred language
$accept_language = $_SERVER["HTTP_ACCEPT_LANGUAGE"];

// Split each language into separate locales
$languages = explode(",", $accept_language);

// Load the globalize.js file
echo "<script src=\"globalize.js\" type=\"text/javascript\"></script>";

// Load the appropriate Globalize plugin file for each locale
for each ($languages as $language) {
    $locale = explode(";", $language);
    echo "<script src=\"cultures/globalize.culture.".$locale[0].".js\" type=\"text/javascript\"></script>";

// Finally, call culture(), when browser finishes loading the webpage
echo '<script>$(document).ready(function(){Globalize.culture("'.$accept_language.'");});</script>';