• Resolved laszlogo

    (@laszlogo)


    I’m trying to change the local of the packingslip PDF so that it matches the website language instead of the user language. I see that inside the function translations() there is a filter for plugin_locale but i’m unable to get it to work.

    This is where i’m stuck at:

    add_filter('plugin_locale', 'custom_plugin_locale', 10, 2);
    
    function custom_plugin_locale($locale, $textdomain)
    {
       if($textdomain == 'woocommerce-pdf-invoices-packing-slips'):
          $locale = 'de_DE';
       endif;
       
       return $locale;
    }

    Could you please provide assistance, or even better a working example, on how to get it to work?

    • This topic was modified 2 years, 9 months ago by laszlogo.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    Your code should work fine, where did you add it? To ensure it’s being loaded early enough, you may want to place it in a custom plugin (mu-plugin or regular plugin), or use Code Snippets.

    Here’s a cleaner version of your code (logic is the same):

    add_filter( 'plugin_locale', function ( $locale, $textdomain ) {
    	if( $textdomain == 'woocommerce-pdf-invoices-packing-slips' ) {
    		$locale = 'de_DE';
    	}
    	return $locale;
    }, 10, 2);
    Thread Starter laszlogo

    (@laszlogo)

    Hello Ewout,

    Thanks for your quick reply.
    I tried to put the filter inside a muplugins_loaded action, but no succes:

    add_action('muplugins_loaded', 'custom_muplugins_loaded', 10);
    
    function custom_muplugins_loaded()
    {
       add_filter('plugin_locale', function($locale, $textdomain) {
          if($textdomain == 'woocommerce-pdf-invoices-packing-slips') {
             $locale = 'de_DE';
          }
          
          return $locale;
       }, 10, 2);
    }

    You don’t by any change have a working example on how the change the locale of the packingslip PDF?

    Plugin Contributor Ewout

    (@pomegranate)

    You hooked to muplugins_loaded, but where did you place the code? If you didn’t place the code inside an actual mu-plugin, the action will never be executed anymore, because it already executed before your code was loaded. And if you do place it inside an actual mu-plugin, there’s no need to delay until muplugins_loaded.

    You don’t by any change have a working example on how the change the locale of the packingslip PDF?

    The code from your original post is correct. I tested my own code too and it works as expected.

    The problem is not with the code itself, it’s where you place it (loading too late). If you want to execute this before plugins are loaded, you have 3 options:

    1. place it inside a mu-plugin
    2. place it inside a regular (custom) plugin
    3. Use Code Snippets
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Change plugin locale’ is closed to new replies.