Function in a plugin is behaving differently than function in the functions.php inside a child theme.
I have a custom simple plain plugin with nothing but the following code in it and it works perfectly. It translates wpamelia plugin correctly with all strings as expected.
add_filter( 'load_textdomain_mofile', 'load_custom_plugin_translation_file', 2, 2 );
function load_custom_plugin_translation_file( $mofile, $domain ) {
if ( 'wpamelia' === $domain ) {
$mofile = ABSPATH . DIRECTORY_SEPARATOR . 'wp-content' . DIRECTORY_SEPARATOR . 'plugins' . '/amelia-translate/de_DE/wpamelia-de_DE.mo';
}
return $mofile;
}
When I put the same code into my child theme's functions.php file and disable the custom plugin with code above, translation stops showing.
When I output all $mofile variables from the load_textdomain_mofile
filter into error_log for debugging purposes, all but 'wpamelia' textdomain are present. So I tried translating another plugin this way, to make sure it is plugin related issue, again it worked from the plugin file, but not from functions.php.
The .mo file path is tested and valid, file exists.
I tried using different function name instead of load_custom_plugin_translation_file
so there is no conflict with the disabled plugin.
Why is the same function behaving differently when part of a plugin and differently when in functions.php? Is child theme not meant to be used with load_textdomain_mofile
?
UPDATE
In the original plugin WPAmelia's code, translation is loaded this way:
load_plugin_textdomain('wpamelia', false, plugin_basename(__DIR__) . '/languages/' . AMELIA_LOCALE . '/');
So I put following line into the after_setup_theme
function and and moved the .mo file into this directory, it started partialy working.
load_textdomain( 'wpamelia', get_stylesheet_directory() . '/translations/wpamelia/wpamelia-de_DE.mo' );
Everything is translated fine, front end, back end, but the wp admin menu in back-end is not.
Back-end admin menu is quite important for using this plugin correctly. I tried to play with after_setup_theme
's priority, (0 - 999) but it did not help at all.