I have no idea what is wrong here. First I would like to share probably all required info.

Plugin tree:

$ tree
├── classes
│   ├── database
│   │   ├── plugin-auth.php
│   │   ├── plugin-user.php
│   │   └── wp-user-x.php
│   ├── main
│   │   └── plugin.php
│   └── woocommerce
│       └── plugin-order.php
├── config.php
├── plugin.php
├── languages
│   ├── plugin-shop-en_US.mo
│   ├── plugin-shop-en_US.po
│   ├── plugin-shop-pl_PL.mo
│   └── plugin-shop-pl_PL.po
└── templates
    └── register.php

plugin.php contains Text Domain: plugin-shop

.po and .mo files were generated with Poedit

plugin contains similar code which is executed as first:

class Plugin
    const TEXT_DOMAIN = 'plugin-shop';

    public function initHooks()
        add_action('plugins_loaded', array($this, 'load_textdomain'));

    public function load_textdomain() {
        load_plugin_textdomain( self::TEXT_DOMAIN, false, basename( dirname( __FILE__ ) ) . '/languages' ); 

classses/woocommerce/plugin-order.php contains by example

function order_status_completed()
     wp_mail( $to, __('Something bought', $this->_textdomain ), $body, $headers );

where 'Something bought' were successfully indexed by Poedit

Problem happens when I'm trying to send mail to user on such an action.

add_action( 'woocommerce_order_status_completed', array($this, 'order_status_completed'), 10, 1 );

mail is sent but with english text.

  1. Locale for WordPress are set to pl_PL - tested by sending in mail subject locale() result. Why is it sending original text?
  2. By the way. I want to send mail with text basing on user locale received by get_user_locale() method. How to deal with it?
  • Your second question should be a separate one. – cjbj May 9 '18 at 12:49

This problem looks a bit too complicated and localized to come up with a definitive answer, but it looks like an execution order issue. Here are some suggestions what you could check:

  1. You are using a variable in your translation function. Do not do this.
  2. I'm not sure where that Woocommerce-action is executed, but it might be executed before WP gets to the load_textdomain hook. Not very likely, but not impossible either if it tries to send the mail immediately after the plugin is loaded.
  3. Your add_action uses the default priority. Higher or lower might help.
  4. Check the global $wp_filter variable to see of some plugin is messing with the locales.
  • The problem were wrong languages path. basename( dirname( _FILE_ ) ) . '/languages' ) My bad - Plugin class is defined in main/plugin.php file so language path were wp-content/plugins/main/languages/plugin-shop-pl_PL.po which is bad. Thanks for your suggestions. Thanks to this i've found Query Monitor tool where is Languages section which displayed me bad path. – MEAT May 9 '18 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.