admin管理员组

文章数量:1425734

I'm working on a custom plugin and I try to add translations but without any success

   class MyClass
    {
       public function __construct()
       {
         add_action('plugins_loaded', [$this, 'ausg_langs_i18n'] );
       }

    /**
     * Trying to load plugins locales
     */
    public function ausg_langs_i18n()
    {
        $plugin_rel_path = BASE_PATH . 'lang';
        load_plugin_textdomain( 'ausg', false, $plugin_rel_path );

    }
}

on plugin init the text domain is set to ausg the .mo file is generated with PoEdit.

if I var_dump load_plugin_textdomain( 'ausg', false, $plugin_rel_path ) the result is false

How to debug further this issue?

Update

     define('BASE_LANG_DIR', basename(__DIR__) . '/lang');

     public function __construct()
    {
        $this->lang = $this->setLangGlobal();

        add_action('plugins_loaded', [$this, 'ausg_langs_i18n'] );

    }

  public function ausg_langs_i18n()
    {
        load_plugin_textdomain( 'ausg', false, BASE_LANG_DIR );
    }

here is the wronhg path output Warning: MO file not found at /var/www/my-plugin/web/app/languages/plugins/ausg-en_US.mo

I'm working on a custom plugin and I try to add translations but without any success

   class MyClass
    {
       public function __construct()
       {
         add_action('plugins_loaded', [$this, 'ausg_langs_i18n'] );
       }

    /**
     * Trying to load plugins locales
     */
    public function ausg_langs_i18n()
    {
        $plugin_rel_path = BASE_PATH . 'lang';
        load_plugin_textdomain( 'ausg', false, $plugin_rel_path );

    }
}

on plugin init the text domain is set to ausg the .mo file is generated with PoEdit.

if I var_dump load_plugin_textdomain( 'ausg', false, $plugin_rel_path ) the result is false

How to debug further this issue?

Update

     define('BASE_LANG_DIR', basename(__DIR__) . '/lang');

     public function __construct()
    {
        $this->lang = $this->setLangGlobal();

        add_action('plugins_loaded', [$this, 'ausg_langs_i18n'] );

    }

  public function ausg_langs_i18n()
    {
        load_plugin_textdomain( 'ausg', false, BASE_LANG_DIR );
    }

here is the wronhg path output Warning: MO file not found at /var/www/my-plugin/web/app/languages/plugins/ausg-en_US.mo

Share Improve this question edited Jun 3, 2019 at 7:35 fefe asked May 29, 2019 at 16:25 fefefefe 8943 gold badges14 silver badges34 bronze badges 5
  • What is BASE_PATH set to? If you dump $plugin_rel_path do you get the correct path? – Jacob Peattie Commented May 30, 2019 at 11:08
  • Yes I get the correct absolute path to the plugin base directory – fefe Commented May 30, 2019 at 11:25
  • 1 Well the documentation for load_plugin_textdomain() states that $plugin_rel_path is supposed to be "Relative path to WP_PLUGIN_DIR where the .mo file resides.". So you shouldn't be passing the absolute path. – Jacob Peattie Commented May 30, 2019 at 12:20
  • Thanks for the feedback, debuging the issue with the provided hook from @Tim I see that wordpress tries to pick up the mo files from wrong path. However I defined the relative path as you suggested – fefe Commented Jun 3, 2019 at 7:32
  • That isn't the "wrong" path. WordPress looks there first, and then tries the path you give. Sounds like it's working as expected now. – Tim Commented Jun 3, 2019 at 9:38
Add a comment  | 

1 Answer 1

Reset to default 0

I think Jacob has probably solved your problem in the comments.

However, to your question:

How to debug further this issue?

I would hook into the load_textdomain_mofile filter and check the fully built file paths WordPress is actually trying to read: Example:

<?php
function debug_missing_mofile( $mofile, $domain ){
   if( 'ausg' === $domain && ! is_readable($mofile) ){
       trigger_error('MO file not found at '.$mofile, E_USER_WARNING);
   }
   return $mofile;
}
add_filter('load_textdomain_mofile','debug_missing_mofile',999,2);

You should get two warnings. First for a missing file under WP_LANG_DIR, Second for a missing file that is probably your incorrect plugin path.

Notes:

  • Up to you how you print the error. This example just raises a warning.
  • Hooking with low priority to ensure other filters modifying paths have run.

本文标签: Load custom translation in custom plugin fails