Make WordPress Core

Opened 2 years ago

Last modified 2 years 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
2 years 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
2 years ago

  • Keywords reporter-feedback added

#3 in reply to: ↑ 1 ; follow-up: @andreacalligaris
2 years 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
2 years 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
2 years 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. And it's definitely a bug because the labels are correctly translated, so it makes sense to be able to translate the description too.

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, though, only the description fails. Anyway, it's something like that for sure.

Version 0, edited 2 years ago by andreacalligaris (next)
Note: See TracTickets for help on using tickets.