Java. Dynamic Internationalization and Localization with the JQuery Globalization Plugin

This small Java 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
    String header = request.getHeader("Accept-Language");
    
    // Split each language into separate locales
    String[] locales = header.split(",");

    // Load the globalize.js file
    out.println("<script src=\"globalize.js\" type=\"text/javascript\"></script>");

    // Load the appropriate Globalize plugin file for each locale
    for (int i=0; i<locales.length; i++) {
        int end = (locales[i].indexOf(";") == -1) ? locales[i].length() : locales[i].indexOf(";");
        String locale = locales[i].substring(0,end);
        out.println("<script src=\"cultures/globalize.culture." + locale + ".js\" type=\"text/javascript\"></script>");
    }

    // Finally, call culture(), when browser finishes loading the webpage
    out.println("<script>$(document).ready(function(){Globalize.culture(\"" + header + "\");});</script>");
%>