Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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(); ?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.