admin管理员组文章数量:1122832
Inside a custom plugin with a custom post type page entirely rework, I would use wordpress native translation to translate the post statut and other wordpress default informations.
I try to use the basic __();
as written inside wordpress/wp-admin/post.php but it doesn't work.
Does Wordpress use a text domain for it's own translation.
$statut = get_post_status();
echo __($statut);
I also tried :
echo __($statut, 'post');
and with alternative : _e()
,_x()
, _n()
Inside a custom plugin with a custom post type page entirely rework, I would use wordpress native translation to translate the post statut and other wordpress default informations.
I try to use the basic __();
as written inside wordpress/wp-admin/post.php but it doesn't work.
Does Wordpress use a text domain for it's own translation.
$statut = get_post_status();
echo __($statut);
I also tried :
echo __($statut, 'post');
and with alternative : _e()
,_x()
, _n()
2 Answers
Reset to default 1How to use native wordpress translation domain inside a custom plugin?
You would use __()
without a textdomain, but this won't work for you because that's not how post status labels work.
get_post_status
doesn't return the name of the post status, it returns a slug, e.g. pending
not Pending
. The solution is in the user contributions for the get_post_status
documentation:
https://developer.wordpress.org/reference/functions/get_post_status/#comment-5484
echo get_post_status_object( get_post_status( ) )->label;
As for the usage of __
:
echo __($statut);
This should never be done, these should always be used with hardcoded strings, never variables. This also means that the way you pass data from the user or database into __
is simple, it is never done.
__
etc are there to translate hardcoded strings in your code, so that things like this: <p>Published date: ...
can be translated. It's not intended for dynamic/generated/database/user data. Translation scanning tools to generate PO and MO files depend on these containing hardcoded strings e.g. _( "Published Date:....
to detect translatable strings.
WordPress with translation work on domains. Best approach when you're developing a custom plugin is to set your own domain for translation.
_e( 'Some String', 'your-domain' );
But to make this work, you need to define a domain for your plugin, which you do in the plugin definition comment:
/*
* Plugin Name: My Plugin
* Author: Plugin Author
* Text Domain: your-domain
* Domain Path: /languages
*/
Text Domain is the string that you need to use for your static strings, and domain path is the place, where translation files will be stored, for translation plugins.
You can read more in documentation: https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/
But in your case exactly this won't work, because you are gathering a variable stored in database, those are not translatable in this way, but translation plugins should be able to handle those. Methods like _e()
, __()
are for static texts.
You can of course use variables by using printf
function:
$statut = get_post_status_object( get_post_status( ) )->label;
printf(
/* translators: %s: Status */
__( 'Your status is %s.', 'your-domain' ),
$statut
);
But this won't be translating status itself.
Also, please remember to escape texts when you print them, you can use for example: esc_html_e()
for this.
本文标签: wp adminHow to use native wordpress translation domain inside a custom plugin
版权声明:本文标题:wp admin - How to use native wordpress translation domain inside a custom plugin? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736298017a1930136.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
__
functions, those are only for static hardcoded strings – Tom J Nowell ♦ Commented Aug 13, 2024 at 11:16