0

I want to display post view in localize number. I add these function in function.php to do so

function make_bangla_number($str)
{
    $engNumber = array(1,2,3,4,5,6,7,8,9,0);
    $bangNumber = array('১','২','৩','৪','৫','৬','৭','৮','৯','০');
    $converted = str_replace($engNumber, $bangNumber, $str);

    return $converted;
}

add_filter( 'the_views', 'make_bangla_number' );

But I am unable to show the number in localize. Whenever i call the_views it shows the english number. Any idea how to show the post view number in localize language?

For further info here is my post view function:

// function to count post views.
function setPostViews($postID) {
    $count_key = 'views';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}
// function to display number of post views.
function the_views($postID){
    $count_key = 'views';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' বার';
}

I have Bangla language pack installed and my site CHARSET is also UTF-8. Bangla language pack does all but can't convert the English number into bangla number. So I using that code. With that i can convert the date number in Bangla but unable to convert the views. So I am here.

1 Answer 1

0

The problem is most probably, that you don't actually call the filter. WordPress does this not automatically for you.

If you want to create a custom filter you have to manually call apply_filters, so that the added filters get executed:

function the_views($postID){
    $count_key = 'views';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        $count = 0;
    }

    // use apply_filters to call the filter
    return sprintf($count == 0 ? '%s View' : '%s বার', 
        apply_filters('the_views', $count));

    // better alternative: use the WordPress translation API:
    //return sprintf(_n('%s View', '%s Views', $count), 
        //apply_filters('the_views', $count));
}

I have modified the code a bit so the digit "0" gets also translated/converted. I have also added a suggestion at the end to use the WP translation API. There's a bit more effort required to translate it that way but it would bring some benefits (you could translate it to other languages without code-changes and the result would be proper pluralized).

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.