Localised Estimated Reading Time

So a few people have been writing about my estimated reading time function recently. In particular – it got mentioned on WPTavern in a post all about adding an Estimated Reading Time to your theme.

Then I got this tweet:

Hey @binarymoon – Just read “WordPress – Estimated Reading Time” and I think you should not forget i18n in the code”.

— Christian Zumbrunnen (@chzumbrunnen) July 3, 2014

Localisation

Any time you develop anything for WordPress (that will be used publicly) you should make sure it is fully localised. By that – I mean it should make use of the localisation functionality to allow other people to translate it into their language.

So – I decided to revisit the code and make sure it’s fully translatable.

For anyone who is interested in localising WordPress themes or plugins – there’s a comprehensive document on i18n (internationalization) on the Codex.

When writing the code originally I had considered localisation, but couldn’t see a clean way to do it since it involved multiple plurals (minute, minutes and second, seconds) so to do properly would need quite a few if then statements making things more complex than should be necessary. Having thought about it further I think the easiest solution is to change the text being displayed so that it is simpler – thus needing less text.

I have ended up with the following:

/**
 * Estimate time required to read the article
 *
 * @return string
 */
function kent_estimated_reading_time() {

    $post = get_post();

    $words = str_word_count( strip_tags( $post->post_content ) );
    $minutes = floor( $words / 120 );
    $seconds = floor( $words % 120 / ( 120 / 60 ) );

    if ( 1 <= $minutes ) {
        $estimated_time = sprintf( _n( '%d minute', '%d minutes', $minutes, 'kent' ), $minutes );
    } else {
        $estimated_time = sprintf( _n( '%d second', '%d seconds', $seconds, 'kent' ), $seconds );
    }

    return $estimated_time;

}

How was it for you? Let me know on BlueSky or Mastodon

(Please) Link to this page

Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.

Related Posts

31 Oct 2013

WordPress – Estimated Reading Time

I have a theme coming out soon that displays the ‘estimated reading time’ for each blog post on the homepage.The theme is inspired by the new blogging service Medium – but I first saw the idea mentioned on Brian Crays...
27 May 2013

WordPress: 10 Years Young, What Does The Future Hold?

WordPress is now 10 years old. I started using wordpress 9 years ago – which means I joined the WordPress community early on. The reason I chose WordPress is simply because of the fabled 5 minute install process – I...
08 Aug 2013

The Death of WordPress Theme Frameworks

WordPress theme frameworks are on their way out. They’re dying a slow death. At least that’s what I think.Nathan Rice recently wrote an article with his thoughts about theme frameworks – in defense of them – however he clearly has...
01 Apr 2015

The State of WordPress Themes #wcldn

I recently spoke on a panel at WordCamp London 2015e. Lance – who used to be the Theme Team lead at WordPress.com – asked me if I wanted to speak on a panel with him at WordCamp London 2015. I’ve...
29 Mar 2009

The future of WordPress themes

A couple of weeks ago there was quite a lot of talk within the WordPress themes community about the future of WordPress. Ian Stewart started it, and then it spread around the blogosphere… so I thought I’d offer my rather...
13 May 2010

6 Tips to Build Better WordPress Themes

If you want to make WordPress themes, for clients, to release for free or to sell, then there are a lot of factors you need to take into consideration. Below are some hints and tips that should help ease your...