Make WordPress Core

Opened 22 months ago

Last modified 22 months ago

#56059 new defect (bug)

register_post_type()'s "description" argument can't be translated (gettext)

Reported by: andreacalligaris's profile andreacalligaris Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.0
Component: Posts, Post Types Keywords:
Focuses: Cc:

Description

Calling register_post_type it is possible to use gettext to translate the labels of the post type.
However, while the labels do work, the argument "description" doesn't translate.

<?php
...
$args['labels'] = array(
        'name' => __('Books', 'text_domain'),
        'singular_name' => __('Book', 'text_domain'),
        'add_new' => 'New Book' );
$args['description'] = esc_html__("This is a beautiful description for this post type, but sadly it doesn't get translated", 'text_domain');
$args['has_archive'] = 'books';
...
?>

I'm rather sure that it's not some mistake of mine, because I checked the .po and .mo files multiple times and if I copy and paste that gettext line of code and I echo it anywhere on my website, it gets correctly translated. So it's something about the "description" parameter.

Change History (5)

#1 follow-up: @TobiasBg
22 months ago

How are you generating the .po and .mo files? Does your software maybe not pick up the esc_html__() function as a translation function?
Can you try using esc_html( __( ... ) ); instead?

#2 @johnbillion
22 months ago

  • Keywords reporter-feedback added

#3 in reply to: ↑ 1 ; follow-up: @andreacalligaris
22 months ago

  • Keywords reporter-feedback removed

Replying to TobiasBg:

How are you generating the .po and .mo files?

Poedit

Does your software

It's not about the software, the resulting .po file is correct, I've read it.

Can you try using esc_html( __( ... ) ); instead?

I can use _e(), anything, it doesn't matter, it never works, I've tried everything; If I copy-paste the following code from my first post:

<?php esc_html__("This is a beautiful description for this post type, but sadly it doesn't get translated", 'text_domain');

and I echo it in e.g. my sidebar, it gets correctly translated.
However, when assigned to "description", it is not. It's weird for sure, but I cannot see what I'm doing wrong.

#4 in reply to: ↑ 3 @SergeyBiryukov
22 months ago

Replying to andreacalligaris:

If I copy-paste the following code from my first post:

<?php esc_html__("This is a beautiful description for this post type, but sadly it doesn't get translated", 'text_domain');

and I echo it in e.g. my sidebar, it gets correctly translated.
However, when assigned to "description", it is not. It's weird for sure, but I cannot see what I'm doing wrong.

  1. Can you check whether that string is translated by the time it is assigned to description?
  2. At that time exactly does the register_post_type() call run?

I think this might be a priority issue, e.g. the post type is being registered before the locale is set up or the text domain is loaded.

#5 @andreacalligaris
22 months ago

At what time exactly does the register_post_type() call run?

init, like it's supposed to.

Can you check whether that string is translated by the time it is assigned to description?

Just checked, it is not. Morever, the labels for the custom type, which are correctly translated on the website, are not translated too if I debug-print them in init:

<?php
function add_custom_post_types() {
        register_post_type( ... );
        ...
        error_log(print_r(esc_html__('Example string.', 'my_child_theme_domain'), TRUE)); // doesn't translate
}
add_action('init', 'add_custom_post_types');
?>

So that's where the bug lies.

I think this might be a priority issue, e.g. the post type is being registered before the locale is set up or the text domain is loaded.

It's weird that the labels are correctly translated on the website, though, only the description fails.
Therefore, it sounds like the description is applied early, when the locale is not yet set up, while the labels are applied later in the code.

Last edited 22 months ago by andreacalligaris (previous) (diff)
Note: See TracTickets for help on using tickets.