Support » Developing with WordPress » Change locale inside ajax call

  • Resolved dingo-d

    (@dingo_d)


    I posted this originally on wordpress.stackexchange but got no answer. The original post can be found here.

    I am creating an invoice plugin for my own use, because I want to have all the invoices in one place. The github is here:

    https://github.com/dingo-d/invoice-create-plugin

    I have it working, except that I cannot select the language in which to save the pdf file.

    The only way I got it working is either by changing the language of the WordPress backend in Settings, or by using the locale hook

    add_filter( 'locale', 'mbd_change_locale', 10 );
    
    function mbd_change_locale() {
        return 'hr_HR';
    }

    But this effectively does the same thing like changing the language.

    I tried using setlocale() function inside the ajax callback function, but had no luck with it.

    Any help is welcome 🙂

Viewing 12 replies - 1 through 12 (of 12 total)
  • It’s probably more easy to set the filter like you do, and removing it after when the pdf is created.

    Yeah, but I only got it to work outside of the main class. So I must be missing something…

    Also the pdf creating is happening inside ajax callback, so I’m not sure I can set the filter in there…

    Moderator bcworkz

    (@bcworkz)

    Whether adding a filter is effective or not depends on what you need it for. It’s obvious when stated that you have to add a filter before the filter fires, but knowing when your add_filter() call executes in relation to the filter itself firing is far from obvious.

    You can certainly add a filter within an AJAX callback, but whether it works or not depends on when it’s needed. If the filter does not work, it was likely added too late. What you could do is add your filter in your regular plugin code, contingent on $_POST[‘action’] having the same value as your AJAX request. Then, like mkdgs suggests, remove it within your AJAX callback once the PDF is created.

    Hmmm not sure I understood you. The only filter I found that worked was

    function mbd_change_locale() {
        return 'hr';
    }
    
    add_filter( 'locale', 'mbd_change_locale', 10 );

    Now I have a dropdown that has languages next to the download pdf button. When you select the language, the data-language property on the button changes. On the click of that button I can pick that property up, and pass it to my AJAX.

    In my AJAX callback function I can get all the things I need – post_id and language through the $_POST array.

    With that I can have a simple if, or switch condition inside it that can check the $_POST[language] and according to it change the locale (set it up in a variable $locale for instance).

    The problem is that I don’t know how to use the filter without it impacting the entire backend of the site. All I need to do is to use it so that I can use the translatable strings in my .mo files when creating the pdf.

    You can check the github link for the entire code if that helps 🙂

    When i take look to the code of load_plugin_textdomain()

    I see:

    $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
    $mofile = $domain . '-' . $locale . '.mo';

    So use filter ‘plugin_locale’ before load_plugin_textdomain() and removing it after may do the trick.

    Yeah, the problem with this is that I am calling my load_plugin_textdomain() function inside a function that is hooked on the plugins_loaded hook, which is one of the earliest hook to call.

    So putting

    
    function load_my_locale( $locale, $domain ) {
        $locale = 'hr';
        return $locale;
    }
    
    add_filter( 'plugin_locale', 'load_my_locale', 10, 2 );
    

    Inside my ajax callback isn’t doing anything :\

    • This reply was modified 2 years, 4 months ago by  dingo-d.

    Have you tried to not call load_plugin_textdomain() at plugin init when it’s an ajax request ?

    ( And call it into mbd_invoice_create_pdf() )

    • This reply was modified 2 years, 4 months ago by  mkdgs.

    Maybe like the example set here https://codex.wordpress.org/Plugin_API/Filter_Reference/locale:

    add_filter( 'locale', 'set_my_locale' );
    function set_my_locale( $lang ) {
      if ( 'gl' == $_GET['language'] ) {
        // set to Greenlandic
        return 'ka_GL';
      } else {
        // return original language
        return $lang;
      }
    }
    • This reply was modified 2 years, 4 months ago by  Moe Loubani.

    Tried that, but didn’t work. The conditional set is not the problem. The problem is getting the pdf to be translated 😉

    I commented my $this->set_locale(); method where I load plugin textdomain, and then added both codes to my ajax callback, but I guess this is too late to influence the locale at this point :\

    I don’t understand, if you set that filter above does the PDF not get translated? I mean if you set it sitewide it must, right?

    You even say here that it worked:

    The only way I got it working is either by changing the language of the WordPress backend in Settings, or by using the locale hook

    So just change the conditional to something like if $_POST[‘action’] === ‘generate_pdf’ and when the ajax call happens it should be in the new language

    • This reply was modified 2 years, 4 months ago by  Moe Loubani.
    • This reply was modified 2 years, 4 months ago by  Moe Loubani. Reason: meant $_POST

    Got it to work from your advice on the facebook group. Before initializing the plugin I added

    
    if ( isset( $_POST['language'] ) ) { // Input var okay.
    
    	add_filter( 'locale', 'mbd_set_my_locale' );
    	/**
    	 * Set locale based on the $_POST choice.
    	 *
    	 * @param  string $locale Locale code.
    	 * @return string         Selected locale code.
    	 */
    	function mbd_set_my_locale( $locale  ) {
    		$language = sanitize_text_field( wp_unslash( $_POST['language'] ) ); // Input var okay.
    
    		if ( 'hr' === $language ) {
    			$locale = 'hr';
    		} else {
    			$locale = 'en';
    		}
    		return $locale;
    	}
    }
    // Now init the plugin.
    $init = new CreateInvoicePluginInit();
    

    Thanks!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Change locale inside ajax call’ is closed to new replies.