0

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

  • What is BASE_PATH set to? If you dump $plugin_rel_path do you get the correct path? – Jacob Peattie May 30 at 11:08
  • Yes I get the correct absolute path to the plugin base directory – fefe May 30 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 May 30 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 Jun 3 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 Jun 3 at 9:38
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.

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.