SOS

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

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: