Search code examples
wordpresscustom-wordpress-pageswordpress-plugin-creation

WordPress Plugin translation not working EN to FR, what's the reason?


I'm creating a new plugin. I generated the PO and MO files for my plugin using POedit and I followed all the steps as described on https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/, from writing a perfect header in the main plugin file till adding php code in the same file in order to load text domain and the MO file.

When I change the site language from English to French the plugin still displaying EN strings.

What could be the issue? Is it because the plugin is not on wordpress.org yet?

Here's my laptop config :

locahost : wampserver
Windows 10
Php version : 8.1

Thanks in advance!

Note:

In the main plugin file, in order to load the PO and MO files, I put this :

/**
 * wpdocs_load_textdomain() loads Text Domain for the translation
 */
function wpdocs_load_textdomain() {
    load_plugin_textdomain( 'str-sticky-header', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' ); 
}


/**
 * my_plugin_load_my_own_textdomain( $mofile, $domain )  loads the MO file for the translation
 * @param $mofile
 * @param $domain
 */
function my_plugin_load_my_own_textdomain( $mofile, $domain ) {
    if ( 'str-sticky-header' === $domain && false !== strpos( $mofile, WP_LANG_DIR . '/plugins/' ) ) {
        $locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
        $mofile = WP_PLUGIN_DIR . '/' . dirname( plugin_basename( __FILE__ ) ) . '/lang/' . $domain . '-' . $locale . '.mo';
    }
    die($mofile);
    return $mofile;
}

add_action( 'init', 'wpdocs_load_textdomain' );
add_filter( 'load_textdomain_mofile', 'my_plugin_load_my_own_textdomain', 10, 2 );

The die($mofile) function is displaying the right MO file. Is the error in the translation itself?


Solution

  • Here's the solution : I'm talking about my case here, When I added the Text Domain Name to Gettext functions in interface.php file, I used this command line : php add-textdomain.php TextDomainNameOfThePlugin PathToInterfaceFile/interface.php > tdn.php , So I generated tdn.php as you can see in that command line, and constructed my POT, PO and MO files based on this UNUSED NEW FILE, and I forgot to either rename tdn.php to interface.php (and delete the old interface.php) so I can use it, or to copy its content and paste in into interface.php file that didn't contain Text Domain Name in its Gettext functions.

    So now, I just copied tdn.php content to interface.php, and the translation works perfectly.

    Thanks @DanieleAlessandra !