admin管理员组

文章数量:1134239

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?

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?
Share Improve this question edited May 9, 2018 at 12:35 cjbj 15k16 gold badges42 silver badges89 bronze badges asked May 9, 2018 at 11:51 MEATMEAT 111 bronze badge 1
  • Your second question should be a separate one. – cjbj Commented May 9, 2018 at 12:49
Add a comment  | 

1 Answer 1

Reset to default 1

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.

本文标签: woocommerce offtopicwordpress plugin translation not working