1

I'm using this plugin to show the reading time of my post: https://jasonyingling.me/reading-time-wp/

However, I have modified some stuff because I want to be able to use it with different languages and change the positioning. So far, everything is working smoothly. The only tiny issue is that when the reading time is just a minute, it displays the word "minutes" (plural). The original plugin has a way to deal with this, but since I'm translating it I can't make use of it. To my poor understanding, this should be quite easy to fix if I could use the value returned by the plugin, but I don't know how to do that.

Here is how is currently being displayed in my content-single.php file

<div id="rtime">
    <?php pll_e( 'Reading Time:' ); echo do_shortcode('[rt_reading_time]');  pll_e( 'minutes' );?>
    </div><!-- #rtime -->

This is the way the plugin determines whether to show "minute" or "minutes" I just don't know how to apply that to my code.

public function rt_reading_time($atts, $content = null) {

        extract (shortcode_atts(array(
            'label' => '',
            'postfix' => '',
            'postfix_singular' => '',
        ), $atts));

        $rtReadingOptions = get_option('rt_reading_time_options');

        $rtPost = get_the_ID();

        $this->rt_calculate_reading_time($rtPost, $rtReadingOptions);

        if($this->readingTime > 1) {
            $calculatedPostfix = $postfix;
        } else {
            $calculatedPostfix = $postfix_singular;
        }

        return "
        <span class='span-reading-time'>$label $this->readingTime $calculatedPostfix</span>
        ";
    }

I thought about modifying the plugin's code itself, but I will have issues when a new update comes, and anyway, I wouldn't be sure how to do it.

Thanks in advance!

2

1 Answer 1

0

The shortcode uses 'postfix' and 'postfix_singular', which can be used in your code in a few ways. You can pass them directly in your do_shortcode() call: echo do_shortcode('[rt_reading_time postfix="minutes" postfix_singular="minute"]'), but that doesn't help when you want to use translations. For that, you can use sprintf() to create a string that gets passed to do_shortcode() as a parameter.

Example:

$shortcode_call = sprintf(
    '[rt_reading_time postfix="%1$s" postfix_singular="%2$s"]',
    esc_html_x( 'minutes', 'minutes plural', 'plugin-slug' ),
    esc_html_x( 'minute', 'minutes single', 'plugin-slug' )
);

echo do_shortcode( $shortcode_call );
4
  • Thanks! I think I sort of get it.... but where would I add the translations in your example? Commented Feb 1, 2018 at 20:06
  • I downvoted this answer because this code doesn't work for languages that doesn't construct plurals like english : gnu.org/software/gettext/manual/html_node/Plural-forms.html
    – mmm
    Commented Feb 1, 2018 at 20:55
  • @mmm Sure, but to be fair, that's a limitation of the plugin itself, not of my code. wordpress.org/plugins/reading-time-wp gives you the option of changing the text before the time, the singular and plural after, and not the arrangement of the words for fully localized text. Commented Feb 27, 2018 at 17:35
  • the questioner need "to use it with different languages" then I downvoted to show to the readers that this answer doesn't meet this requirement. And I think that there no easier fix that complety rewriting this shortcode.
    – mmm
    Commented Feb 27, 2018 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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