localization – How to properly load text domain of custom mu plugin

Question

I can’t manage to load the text domain of my plugins and it’s slowly but surely driving me nuts; especially cuz all the error feedback I get is: false.

What I’ve tried so far (adding the following code in my main plugin file):

According to some forum answers:

add_action(
        'init',
        function() {
          load_muplugin_textdomain( 'my-mu-plugin', 'my-mu-plugin/languages' );
        }
      );

This results in an error when a page is loaded in the default language (load_muplugin_textdomain() returns false), and fails to localize dynamic strings (e.g. when you make an AJAX request to the server whose answer includes __( 'Hello', 'my-mu-plugin' ), and your current locale is not english, you still get ‘Hello’ back from the server)

According to the actual wp docs:

function my_plugin_load_my_own_textdomain( $mofile, $domain ) {
    if ( 'my-mu-plugin' === $domain && false !== strpos( $mofile, WP_LANG_DIR . '/plugins/' ) ) {
        $locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
        $mofile = WPMU_PLUGIN_DIR . "https://wordpress.stackexchange.com/" . dirname( plugin_basename( __FILE__ ) ) . '/languages/' . $domain . '-' . $locale . '.mo';
    }
    return $mofile;
}
add_filter( 'load_textdomain_mofile', 'my_plugin_load_my_own_textdomain', 10, 2 );

This does not work at all.

UPDATE

I’ve added all of my .mo files from my mu-plugins/my-my-plugin/languages folder into the wp-content/languages/pugins folder, which was not the case yet; and now I get the same problem for both the codes shown above: static contents are correctly translated according to the current locale, but dynamic contents in the wp admin (like server responses which contain __() strings, retrieved via AJAX) are always output in the root default language.

Concrete example: Say you have an admin page in your wp admin, added and localized via your custom plugin. When you’re in the wp admin and you change the language of your user to another one for which your custom plugin has a .mo file, all the contents of that admin page, when you load it, are translated. But, as soon as you do any client-server interaction on that admin page, the response always comes back in the root language, although it should be localized too.

0
DevelJoe 1 month 2021-08-13T21:14:22-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse