SOS

Shane O'Sullivan's technical blog… really ties the room together

Archive for the ‘i18n’ Category

Setting the default locale in Dojo using PHP

Posted by Shane O'Sullivan on September 13, 2008

When writing locale aware JavaScript for a browser environment,  you only have access to the locale that the browser used upon installation.  If the user changes their preferred locale, a JavaScript application has no way of detecting it.  See the Dojo documentation for more details on this at http://dojotoolkit.org/book/dojo-book-0-9/part-3-programmatic-dijit-and-dojo/i18n/specifying-locale

However, the list of accepted languages are sent to the web server in the request headers.  It is also possible to instruct Dojo as to which locale to use.  Below is an example of how to do this in PHP

<?php
 // Get the array of request headers
 $h = getallheaders();
 // Retrieve the 'Accept-Language' header, a comma separated list
 $langs  = $h['Accept-Language'];
 // Parse the first language
 $locale=strtok($langs,",");
 if(strlen($locale) < 1) {
  // Set a default language if you like
  $locale = "en";
 }else {
  // Make sure that the optional 'q' parameter is not included
  $locale = strtok($locale, ";");
 }
?>
<script type="text/javascript" src="dojo/dojo.js" djConfig="parseOnLoad:true,locale:'<?=$locale?>'"> </script>

Note the djConfig attribute when including the dojo.js file. It specifies the locale to be used.

Posted in Ajax, Dojo, i18n, Javascript, open source, Technical | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.

Join 535 other followers

%d bloggers like this: