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()

Share Improve this question asked Aug 13, 2024 at 10:33 imagIneimagIne 10511 bronze badges 1
  • you're not supposed to pass variables to __ functions, those are only for static hardcoded strings – Tom J Nowell Commented Aug 13, 2024 at 11:16
Add a comment  | 

2 Answers 2

Reset to default 1

How 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