Support » Plugin: Polylang » Unable to translate strings in WooCommerce modules

  • Resolved jeesus

    (@jeesus)


    Hi, there

    I really like Polylang and it has been meeting my expectations since I discovered it. However, now I’m in need of changing some WooCommerce strings. Polylang works everywhere else in the site, but there.

    For example I want to change WooCommerce view the cart button string:
    $message = sprintf('<a href="%s" class="button wc-forward">%s</a> %s', get_permalink( wc_get_page_id( 'cart' ) ), pll__('View cart'), $added_text );

    Somehow it doesn’t translate, although the strings appear in the admin section for translation. It doesn’t show empty string, it just displays ‘View cart’. If I use the same pll_(‘View cart’) anywhere else, it translates just fine.

    Any ideas? Is WooCommerce blocking this thing or smth? I know there are .po and .mo translation files for WooCommerce, but they don’t actually behave like I want them to.

    Thanks!

    https://wordpress.org/plugins/polylang/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Going to subscribe to this post because I face the same problem!

    I have the same problem, i hope the author can help us !

    I did a simple hack to get around this. the pll__() and pll_e() functions are just wrappers for the wordpress gettext (__() and _e()) functions with text domain “pll_string”. Just paste it into your functions.php, register the strings with pll_register_string() and translate them. I haven’t tested it very closely, but I guess it works until the author comes up with something better 🙂

    add_filter( 'gettext',
    	function ( $translations, $text, $domain ) {
    		// Don't send translated strings to pll and don's send pll translations to itself
    		if (
    			$translations == $text
    			&& function_exists( 'pll__' )
    			&& 'pll_string' != $domain
    		) {
    			return pll__( $text );
    		} else {
    			return $translations;
    		}
    	},
    	101,
    	3 );

    Thanks johanpalmfjord, seems to work well

    I’d like to try this solution, but could you tell me what do you mean by “register the strings with pll_register_string()”? Where do I do that, in functions.php as well? Thanks a lot!

    @ Alitscha
    Exactly. For the strings to appear in the polylang admin you’ll need to register them first. You do that with pll_register_string() in your theme’s functions.php. Documentation: https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/#pll_register_string

    sorry, it is still not completely clear…do I have to register each single string? where do I get the single string names? and will then the strings appear in the string translation list in the dashboard?
    thanks so much in advance.

    maybe I haven’t given enough details: I actually don’t see woocommerce strings in the polylang dashboard.

    @johanpalmfjord: for the record, I have solved my own issue using this plugin https://wordpress.org/plugins/woo-poly-integration/
    your code works as well, so I only mention that as an alternative, “plugin” solution. Thanks again!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Unable to translate strings in WooCommerce modules’ is closed to new replies.