最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

New plugin translation does not work

programmeradmin0浏览0评论

I´m trying to resolve one matter for 2 days without success.

I don´t know why but for some mysterious reasons, new translations in my plugin project did not work but it works perfectly for old translated labels.

My translation is visible inside .po and .mo

msgctxt "misc_postbox_label"
msgid "Visibility"
msgstr "Visibilité"

... connexion Abonnement État Visibilité...

In my php code :

_x( "Visibility", "misc_postbox_label", $this->current_plugin_domain ),

To debug this I tried to follow this.

Something could be the problem : I added this following code :

function debug_load_textdomain( $domain , $mofile  ){
    echo "Trying ",$domain," at ",$mofile,"<br />\n";
    exit;
}

add_action('load_textdomain','debug_load_textdomain');

And the hook load_textdomain is called but I got a blank page.

Someone knows why?

Someone has got an idea to resolve the entire problem ?

I´m trying to resolve one matter for 2 days without success.

I don´t know why but for some mysterious reasons, new translations in my plugin project did not work but it works perfectly for old translated labels.

My translation is visible inside .po and .mo

msgctxt "misc_postbox_label"
msgid "Visibility"
msgstr "Visibilité"

... connexion Abonnement État Visibilité...

In my php code :

_x( "Visibility", "misc_postbox_label", $this->current_plugin_domain ),

To debug this I tried to follow this.

Something could be the problem : I added this following code :

function debug_load_textdomain( $domain , $mofile  ){
    echo "Trying ",$domain," at ",$mofile,"<br />\n";
    exit;
}

add_action('load_textdomain','debug_load_textdomain');

And the hook load_textdomain is called but I got a blank page.

Someone knows why?

Someone has got an idea to resolve the entire problem ?

Share Improve this question edited Apr 26, 2019 at 12:57 J.BizMai asked Apr 25, 2019 at 16:41 J.BizMaiJ.BizMai 9002 gold badges10 silver badges30 bronze badges 7
  • So this may sound silly, but have you actually uploaded the updated MO and PO files to your site?.. Could it be because of caching? How did you create/regenerate the files? Do you use Loco Translate? – Sally CJ Commented Apr 25, 2019 at 17:26
  • Yes, I checked about the MO and PO on the server. It seems to have no cache because it is on a developement environment without cache plugins. I use Poedit to do translations. When I removed the MO and Po on the server, translated labels are still translated, so weird... – J.BizMai Commented Apr 25, 2019 at 17:30
  • I reset the wordpress and I add my plugin again and that works. It seems to be a cache system but I would like to know more anout what Wordpress does exaclty with po and mo files because it seems to have a cache system. – J.BizMai Commented Apr 25, 2019 at 17:56
  • I haven't checked that, probably because I have never run into such an issue and that I normally use the Loco Translate plugin when creating/editing the translations (from a POT/.pot file). But if I can, I'll look at that translation caching.. if you're sure it wasn't a server-side caching (e.g. Varnish).. try replicating the issue to be sure the translations were indeed being cached by WordPress, and do it on a site that's 101% cache-disabled... – Sally CJ Commented Apr 25, 2019 at 18:19
  • I did some tests without understanding. Do you think it could be a link with Project-Id-Version will should change each time you do new translation ? Maybe PO-Revision-Date could be the problem because I´m writing my .po and .mo in Brasil and the server is in Europe so the date is always old for the european server. – J.BizMai Commented Apr 26, 2019 at 12:05
 |  Show 2 more comments

1 Answer 1

Reset to default 2

After a heavy investigation, I understood what is happening.

The solution to debug this was partially here but there were some php errors that´s why I got a blank page.

So, after to test :

  1. Which language is called thanks with get_locale()
  2. Display WP_LANG_DIR to know where are language files are stored

I tried to know exactly which MO file was loaded for my plugin with this code ( code with no php/wordpress errors ):

function debug_load_textdomain( $domain , $mofile  ){
    echo "Trying " . $domain . " at " . $mofile . "<br />";
}

add_action('load_textdomain','debug_load_textdomain', 10, 2 ); 

I could see on my site this path for my plugin concerned:

my-plugin at /path-to-worpdress/wp-content/languages/plugins/my-plugin-fr_FR.mo

and... SURPRISE !!!!

This file does not contain my translation. Even if I uninstall my plugin and install again.

The reason is :

when you uninstall the plugin, the file .../wp-content/languages/plugins/my-plugin-fr_FR.mo is removed correctly but if you install your plugin again, the Wordpress core will search the .po and .mo files from the lastest stable version uploaded on wordpress and does not use your .po and .mo inside your current plugin folder in development.

.../wp-content/languages/plugins/my-plugin-fr_FR.po

# Translation of Plugins - My Plugin - Stable (latest release) in French (France)
# This file is distributed under the same license as the Plugins - My Plugin - Stable (latest release) package.
msgid ""
msgstr ""
"PO-Revision-Date: 2018-12-12 23:16:54+0000\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n > 1;\n"
"X-Generator: GlotPress/2.4.0-alpha\n"
"Language: fr\n"
"Project-Id-Version: Plugins - My Plugin - Stable (latest release)\n"

Conclusion

If your plugin was already uploaded on wordpress, and you want to test your new .po and .mo files, you have to delete /path-to-worpdress/wp-content/languages/plugins/my-plugin-fr_FR.mo and Wordpress will automatically get the .mo file inside your plugin folder.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论