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
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.
本文标签: Load custom translation in custom plugin fails
版权声明:本文标题:Load custom translation in custom plugin fails 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745442213a2658496.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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