// reloads page with new URL (to be used in support of Rails I18n functionality)
   function set_language(local_pref, available_locales) {
       
         var current_url = window.location.href
       
       var new_url = swap_locale(current_url, local_pref, available_locales);
       window.parent.location.replace(new_url);
   };
    // returns a new string replacing any discovered locales with the requested one
    function swap_locale(url, locale_pref, available_locales) {
        var url_path = url.split("/");
        var available_locales = available_locales.split(",");
        var language_found = false;

        // for each supported locale detect if its present in the URI
        jQuery.each(url_path, function(index,value) {
            // if the locale is discovered swap it out
            if($.inArray(value, available_locales) >= 0){
                url_path[index] = locale_pref;
                language_found = true;
            }
        });
        // rewrite the new URL using new locale or add locale if none was discovered
        if( language_found == true) {
            return url_path.join("/");
        } else {
            url_path[2] = url_path[2].concat("/", locale_pref);
        };
        return url_path.join("/");
    };
