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?
2
Answers
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 generatedtdn.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 renametdn.php
tointerface.php
(and delete the oldinterface.php
) so I can use it, or to copy its content and paste in intointerface.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 !
all the code you shared with us is correct. You do not even need the function
my_plugin_load_my_own_textdomain
but I assume that it is used for debugging only.The problem should be in a part of your code that we do not see. If well configured, translations must work on your local environment, even if the plugin is not yet distributed.
Check the following things:
die($mofile)
you see something that looks like the right file, please check twice with something likeis_file($mofile)
. If the file is missing, in fact, translation fails silently.<text_domain>-<locale>.mo
, in your case the file must be namedstr-sticky-header-fr_FR.mo
. This should be already this way, because you are seeing the name inmy_plugin_load_my_own_textdomain
While trying to figure out your issue, I wrote a simple PHP script that – once placed in a directory along your .mo translations – checks if they work. Hopefully this script could help you and other people investigating similar issues. You may find it here: https://github.com/DanieleAlessandra/wp-translation-check
[Edit: adding a snippet to investigate translation loading state]
After several attempts we still don’t know if your translation file has being loaded. Please add the following snippet anywhere to have an output of loaded strings for the current language in your text domain:
The perfect place to put this snippet is immediatelu after a failed translation, so you can have the un-translated string in front of your eyes along with this snippet output.
The output should be something similar to the following:
Hopefully you will have more entries.
With this output you will be able to undestand what is happening:
__()
,_e()
, or any other translation, you may have a typo there.Please keep me posted with results.