WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 6 months ago

Last modified 6 months ago

#38218 closed enhancement (duplicate)

Make post type labels work during locale switching

Reported by: swissspidy Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: needs-patch needs-unit-tests
Focuses: Cc:

Description

This came up while working on #38157 and #26511.

Moving the logic from get_post_type_labels() into its own class would greatly benefit the efforts made to switch locales. It essentially replaces the post type labels object with a new class that returns the labels on the fly (note: includes tests from #38157). The downside is that labels are never stored in an array but always translated upon request (performance problems?).

What's needed for just-in-time translation (in addition to the current patch) is probably allowing passing a callback instead of an array of labels to the function. The same would need to be done for custom taxonomies and perhaps post statuses.

Attachments (2)

38218.diff (14.4 KB) - added by swissspidy 3 years ago.
38218.2.diff (17.4 KB) - added by swissspidy 7 months ago.

Download all attachments as: .zip

Change History (16)

@swissspidy
3 years ago

#1 @swissspidy
2 years ago

  • Type changed from defect (bug) to enhancement

#2 @swissspidy
2 years ago

In 40635:

Posts, Post Types: Add initial tests for get_post_type_labels().

See #38218.
Fixes #38157.

#3 @swissspidy
8 months ago

  • Keywords needs-patch needs-unit-tests added; has-patch has-unit-tests removed
  • Milestone changed from Awaiting Review to Future Release

#4 @dontgo2sleep
7 months ago

What about the idea for lazy loading the labels via callback function?
Should we do it now, as part of this ticket or new one will be raised?

#5 @swissspidy
7 months ago

@dontgo2sleep We can explore that in this ticket, but I see one big problem with this approach: it doesn't work for existing plugins.

There are thousands of plugins registering post types and taxonomies out there. In order for things like the toolbar in the user's locale work, we need to solve this in a backward compatible way for all existing plugins.

The adoption rate for the callback variant would be way too slow without any additional benefit.

Also think about documentation, tutorials, etc. These won't be updated using the callback option for a long time.

However, we probably need both to fully solve this. We'll have to see how it goes.

Last edited 7 months ago by swissspidy (previous) (diff)

#6 @dontgo2sleep
7 months ago

@swissspidy I got your point about the adoption of such as feature.
What bothers me is that I am not quite sure about the technical implementation. What is your exact idea about the callback? What I understand is WP_Post_Type object to have property labels pointing to function returning an array of labels and called on demand by WP_Post_Type_Labels::get() method?

#7 @swissspidy
7 months ago

Yeah so I added a PoC implementation two years ago for such a class: https://core.trac.wordpress.org/attachment/ticket/26511/WP_Post_Type_Labels.diff

I imagined it to work like this:

  1. Register a new post type A
  2. Only define the name label and let get_post_type_labels() provide the rest. (This is what many plugins do, in a nutshell)
  3. WP_Post_Type::$labels is a magic method that returns an array containing name plus any other labels provided on the fly by get_post_type_labels(), either using a WP_Post_Type_Label class or something else.
  4. Register a new post type B
  5. Pass a callback function to labels that returns an array with a single key, name.
  6. WP_Post_Type::$labels is a magic method that returns a merged array of the callback's result + any missing labels from get_post_type_labels(). Again, the specifics of that are TBD.

#8 @swissspidy
7 months ago

  • Milestone changed from Future Release to 5.0
  • Summary changed from Introduce a WP_Post_Type_Labels class to Make post type labels work during locale switching

@swissspidy
7 months ago

#9 @swissspidy
7 months ago

Just took another stab at this, using a magic getter and callbacks to make things work.

Includes some basic tests for post type labels already.

Note that you need to generate the de_DE.mo file in the test directory yourself before running the tests.

#10 @swissspidy
7 months ago

Feels like we should close this in favor of #41305. Somehow I totally forgot about that ticket...

#11 @pento
6 months ago

  • Milestone 5.0 deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #41305.

This ticket was mentioned in Slack in #core-restapi by timothybjacobs. View the logs.


6 months ago

This ticket was mentioned in Slack in #core by pento. View the logs.


6 months ago

This ticket was mentioned in Slack in #core-restapi by timothybjacobs. View the logs.


6 months ago

Note: See TracTickets for help on using tickets.