WordPress Locale Switching on Accept-Language Header

Is there anyway to get wordpress to automatically switch locales based on the HTTP Accept-Language header?

Similar to how ASP.NET can do it with their globalization=”auto” config setting. The reason for this is that I want my dates to be custom formatted based on where you are viewing the site from – not what the wordpress site settings use for everybody around the world.

Read More

This way somebody from the USA (en-US) will see 12/25/2011 but somebody from Australia (en-AU) will see 25/12/2011.

Related posts

Leave a Reply

1 comment

  1. that’s going to be a bit tricky since WordPress doesn’t relate date and time format to language. so to get the current language from the browser you can use $_SERVER['HTTP_ACCEPT_LANGUAGE'] and then based on that you can change the date format:

    function update_date_format(){
        $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
        if (strpos($lang, 'au') > 0){
            $date_format = 'd/m/Y'; // 25/12/2011
        }else{
            $date_format = 'm/d/Y'; // 12/25/2011
        }
        update_option('date_format',$date_format);
    }
    

    after you paste this function in your theme’s functions.php add to your theme’s header.php at the very top:

    <?php update_date_format(); ?>