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
1 Answer
Reset to default 0I 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.
BASE_PATH
set to? If you dump$plugin_rel_path
do you get the correct path? – Jacob Peattie Commented May 30, 2019 at 11:08load_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