• Resolved ReactorShop

    (@reactorshop)


    Hi Chouby,

    I hope you’re doing great. A late very happy new year I wish for you.

    Getting back on track, I noticed this behavior change since you separated the Front End from the Admin Side translations.

    When there’s an Ajax call, these sessions seems to be treated as if they were admin side sessions, maybe because they call the admin-ajax.php or maybe it’s something else.

    I remember this wasn’t the case at least up until PLL 1.1.6.

    I’ll just mention an example of a string which is part of a server reply:

    $cart_messages[] = str_replace( "[product_name]", $cart_item->get_title(), __( 'You just added "[product_name]" to your cart.', 'wpsc' ) );

    You’ll get the “You just added…” message sent to the browser instead of the translated version.

    It’s not a show stopper, so it can wait.

    Thank you in advance, Chouby.

    https://www.ads-software.com/plugins/polylang/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Chouby

    (@chouby)

    Unfortunately, WP does not provide any way to detect if an ajax request is done on admin or on frontend. It has always been an issue for Polylang. So Polylang does provide one, but that means plugins using ajax on frontend must be aware of Polylang ??
    You just have to set the variable ‘pll_load_front’ in the ajax request
    https://polylang.wordpress.com/documentation/documentation-for-developers/general/

    Thread Starter ReactorShop

    (@reactorshop)

    The issue that I have is that the plugin which is making this request is not created by me (WP e-Commerce), so a pull request as specific as this might not be accepted by their team.

    I will modify the ajax request and maintain it by myself.

    By the way, when you get a chance, please take a look at my website so that you check how far I’ve gone integrating Polylang to WP e-Commerce, Theme My Login and the Customizr theme:

    https://www.ReactorShop.com

    Theme My Login is a little tricky to translate, because it loads its translations MO file really late and on its own ‘theme_my_login’ domain, but if you check the plugin code, in reality it uses the default domain to look for translations.

    I had to merge their MO file with WordPress’ default MO file using the ‘init’ hook in order for its translations to be found.

    I’m far from releasing a Polylang/WP e-commerce plugin, because it currently only takes into account my specific website setup.

    Thank you, Chouby!

    Plugin Author Chouby

    (@chouby)

    I am a very poor jquery programmer but I am aware of this:
    https://gist.github.com/rosshanney/6202009
    developped for another plugin.

    I did not try it but I should in the future or try to find a better way to detect ajax requests on frontend.

    I am sure that what you did was not an easy task ! I am aware that Theme my login does not work properly with Polylang. And integrating an e-commerce plugin and a multilingual plugin seems to be quite a huge task. I had a look at woocommerce-multilingual which integrates Woocommerce and WPML. This bridge is bigger than Polylang itself !

    Thread Starter ReactorShop

    (@reactorshop)

    Don’t worry, Chouby. jQuery is not easy.

    WP e-Commerce’s shopping cart widget currently doesn’t update when using cache plugins.

    A fix it’s on its way, but in the meantime I had to add to my plugin a plain mini shopping cart placed in the side bar and a small jQuery code snippet to update it.

    I hope you do find a way to detect the front end ajax requests. Besides WP e-Commerce, Theme My Login does some requests too, so if you register a new user, the process starts in the correct language but ends asking for the user registration information using the default language because of this. It’s reasonable to think that others plugins would be coded to send translated server responses too.

    Regarding the work I’ve done, it has taken me long because even though it started with interfacing Polylang with WP e-Commerce, in the course of doing this I noticed and had to fix WP e-Commerce behavior not related to translations by using its hooks, provided pull requests to fix its USPS and UPS shipping modules, created shipping modules to rate 2 Mexican shipping companies (Estafeta and Redpack), and worked on a partitioning problem/3D bin packing algorithm to reduce shipping costs by combining shipment packages that fit into a shipping box that meets shipper’s restrictions.

    Regarding PLL, other improvement opportunity I saw in your plugin is that the server redirect query string is not considered when forming a translated URL. I fixed this by hooking to ‘pll_translation_url’, checking and, if necessary, recreating the URL and appending this string to the end. I need that string appended to the URL because WP e-Commerce handles the customer information in “tabs”, which are identified by the server in this string.

    Seeing that you have interacted with Theme My Login before, here’s the code I had to add to my plugin to fix the links translations:

    function p4we_my_login_domain() {
    	global $l10n;
    
    	$locale = get_locale();
    	$mofile = WP_PLUGIN_DIR . '/theme-my-login/language/' . 'theme-my-login-' . $locale . '.mo';
    	if( file_exists( $mofile ) )
    		load_textdomain( 'default', $mofile );
    }
    add_action( 'plugins_loaded', 'p4we_my_login_domain', 9 ); //User links titles are read with a priority of 10 when plugins_loaded actions are applied.
    
    function p4we_tml_user_links( $user_links ) {
    	foreach( $user_links as $key => $user_link ) {
    		$user_links[$key]['title'] = __( $user_link['title'] );
    	}
    	return $user_links;
    }
    add_filter( 'tml_user_links', 'p4we_tml_user_links', 11 ); //Fixes user links titles translations before they're picked up by the theme.
    
    function p4we_tml_title( $title ) {
    	return __( $title );
    }
    add_filter( 'tml_title', 'p4we_tml_title', 11 ); //Fixes TML //Fixes TML titles translations before they're picked up by the theme.

    Thank you, Chouby.

    Plugin Author Chouby

    (@chouby)

    I spent some time on it and I believe that I found a way to automatically detect (without setting ‘pll_load_front’) wether an ajax request is made on frontend or backend.

    I just uploaded a development version (1.4dev18) which implements this (and which doesn’t use ‘pll_load_front’ anymore for the detection).

    Thread Starter ReactorShop

    (@reactorshop)

    Chouby, please, let me congratulate you on this work.

    The situation is solved:

    WP e-Commerce’s add to cart Fancy Notification translated with Polylang

    I started checking yesterday what to do about it. As I said before, jQuery is not easy.

    Regarding the calendar plugin jQuery you showed me, $.ajaxSetup use is not recommended by the jQuery team:

    jQuery.ajaxSetup use not recommended

    I was looking for an alternative when I saw your post.

    Thank you, Chouby.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘During Ajax calls, the server only responds with default language strings’ is closed to new replies.