Support » Plugin: WooCommerce Menu Cart » Gettext plural strings

  • Hi.
    I am trying to translate this piece of string in a gettext function filter.

    add_filter( 'gettext', function( $translation, $text, $domain ) {
    	if ( $domain == 'wp-menu-cart' ) {
    		if ( $text == '%d item' ) { $translation = '%d tilmeldt'; }
    		if ( $text == '%d items' ) { $translation = '%d tilmeldte'; }
    	return $translation;
    }, 10, 3 );

    That does not work. Is there something I have to be aware of, when trying to translate singular/plural strings?
    The string that I am trying to translate is in this line:
    sprintf(_n('%d item', '%d items', $item_data['cart_contents_count'], 'wp-menu-cart'), $item_data['cart_contents_count']);

    I don’t want to translate the .po file, because this is overwritten on updates.
    I hope you can help. 🙂

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Ewout


    Hi! For plural gettext strings you need the ngettext filter instead of the gettext filter.

    I would recommend to use the po/mo files instead though, if you place them in wp-content/languages/wp-menu-cart/ they will not be overwritten by plugin updates.

    Do note that after making changes to these strings, you will either need to close and reopen the browser tab or add something to the cart, to reset the browsers sessiondata (this is different from regular browser cache so a hard refresh will not work). This is where WooCommerce stores the AJAX fragments and they will not be updated otherwise.

    Let me know if that works for you!

    Thread Starter Vayu Robins


    Hi Ewout and thanks for helping!
    I have tried to do as you explained, but it doers not work. What works though is moving the po/mo files into this dir: wp-content/languages/wp-menu-cart/plugins/

    But as far as I know, these files will be overwritten, when updates to the language files arrive. I am pretty sure other plugins work that way at least. Maybe not WC Menu Cart plugin? That’s why I chose to use the gettext filter, but I will try the ngettext filter. Thanks.

    Plugin Author Ewout


    Hello Vayu,
    I double checked for you, but this works fine on my own site.
    Make sure to double check the path and the fact that you include both the po AND mo file (it’s the mo file that is used by gettext actually).

    The correct path is (note the subfolder with the wp-menu-cart name):


    The default path where WordPress would store (and overwrite) the language pack is:


    However, if you are using the WooCommerce version of this plugin that shouldn’t matter because WordPress would default to the plugin slug (woocommerce-menu-bar-cart) for the textdomain (we have built in backwards compatibility). This means that if you are not using the WP Menu Cart version but the WooCommerce version of the plugin (they’re identical except for the name), WP will not download/overwrite that language pack at all so you’re even safe if you put it there.

    If you have the file in the correct location mentioned above, that should still work (taking the sessionsdata quirk into account, closing and reopening the tab after making the changes).

    If all else fails you can indeed use the ngettext filter.

    Let us know if that works!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Gettext plural strings’ is closed to new replies.