Make WordPress Core


Ignore:
Timestamp:
04/04/2022 03:48:08 AM (2 years ago)
Author:
peterwilsoncc
Message:

Posts, Post Types; Taxonomy: Translate default labels once.

Improve the translation of post type and taxonomy labels by caching the translations during runtime. To account for internationalisation plugins, the runtime cache is cleared as the post types/taxonomies are reinitiated on change_local hook.

The same property and methods are added to both WP_Post_Type and WP_Taxonomy:

  • $default_labels: for storing the translated strings at runtime
  • get_default_labels(): for getting the default labels, these are translated on the first run and stored in the new property.
  • reset_default_labels(): to clear the runtime cache and force a re-translation of the default labels

Props Chouby, nacin, SergeyBiryukov, Rarst, chriscct7, ocean90, audrasjb, costdev.
Fixes #26746.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-post-type.php

    r52652 r53058  
    4444     */
    4545    public $labels;
     46
     47    /**
     48     * Default labels.
     49     *
     50     * @since 6.0.0
     51     * @var (string|null)[][] $default_labels
     52     */
     53    protected static $default_labels = array();
    4654
    4755    /**
     
    786794        return $this->rest_controller;
    787795    }
     796
     797    /**
     798     * Returns the default labels for post types.
     799     *
     800     * @since 6.0.0
     801     *
     802     * @return (string|null)[][] The default labels for post types.
     803     */
     804    public static function get_default_labels() {
     805        if ( ! empty( self::$default_labels ) ) {
     806            return self::$default_labels;
     807        }
     808
     809        self::$default_labels = array(
     810            'name'                     => array( _x( 'Posts', 'post type general name' ), _x( 'Pages', 'post type general name' ) ),
     811            'singular_name'            => array( _x( 'Post', 'post type singular name' ), _x( 'Page', 'post type singular name' ) ),
     812            'add_new'                  => array( _x( 'Add New', 'post' ), _x( 'Add New', 'page' ) ),
     813            'add_new_item'             => array( __( 'Add New Post' ), __( 'Add New Page' ) ),
     814            'edit_item'                => array( __( 'Edit Post' ), __( 'Edit Page' ) ),
     815            'new_item'                 => array( __( 'New Post' ), __( 'New Page' ) ),
     816            'view_item'                => array( __( 'View Post' ), __( 'View Page' ) ),
     817            'view_items'               => array( __( 'View Posts' ), __( 'View Pages' ) ),
     818            'search_items'             => array( __( 'Search Posts' ), __( 'Search Pages' ) ),
     819            'not_found'                => array( __( 'No posts found.' ), __( 'No pages found.' ) ),
     820            'not_found_in_trash'       => array( __( 'No posts found in Trash.' ), __( 'No pages found in Trash.' ) ),
     821            'parent_item_colon'        => array( null, __( 'Parent Page:' ) ),
     822            'all_items'                => array( __( 'All Posts' ), __( 'All Pages' ) ),
     823            'archives'                 => array( __( 'Post Archives' ), __( 'Page Archives' ) ),
     824            'attributes'               => array( __( 'Post Attributes' ), __( 'Page Attributes' ) ),
     825            'insert_into_item'         => array( __( 'Insert into post' ), __( 'Insert into page' ) ),
     826            'uploaded_to_this_item'    => array( __( 'Uploaded to this post' ), __( 'Uploaded to this page' ) ),
     827            'featured_image'           => array( _x( 'Featured image', 'post' ), _x( 'Featured image', 'page' ) ),
     828            'set_featured_image'       => array( _x( 'Set featured image', 'post' ), _x( 'Set featured image', 'page' ) ),
     829            'remove_featured_image'    => array( _x( 'Remove featured image', 'post' ), _x( 'Remove featured image', 'page' ) ),
     830            'use_featured_image'       => array( _x( 'Use as featured image', 'post' ), _x( 'Use as featured image', 'page' ) ),
     831            'filter_items_list'        => array( __( 'Filter posts list' ), __( 'Filter pages list' ) ),
     832            'filter_by_date'           => array( __( 'Filter by date' ), __( 'Filter by date' ) ),
     833            'items_list_navigation'    => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ),
     834            'items_list'               => array( __( 'Posts list' ), __( 'Pages list' ) ),
     835            'item_published'           => array( __( 'Post published.' ), __( 'Page published.' ) ),
     836            'item_published_privately' => array( __( 'Post published privately.' ), __( 'Page published privately.' ) ),
     837            'item_reverted_to_draft'   => array( __( 'Post reverted to draft.' ), __( 'Page reverted to draft.' ) ),
     838            'item_scheduled'           => array( __( 'Post scheduled.' ), __( 'Page scheduled.' ) ),
     839            'item_updated'             => array( __( 'Post updated.' ), __( 'Page updated.' ) ),
     840            'item_link'                => array(
     841                _x( 'Post Link', 'navigation link block title' ),
     842                _x( 'Page Link', 'navigation link block title' ),
     843            ),
     844            'item_link_description'    => array(
     845                _x( 'A link to a post.', 'navigation link block description' ),
     846                _x( 'A link to a page.', 'navigation link block description' ),
     847            ),
     848        );
     849
     850        return self::$default_labels;
     851    }
     852
     853    /**
     854     * Resets the cache for the default labels.
     855     *
     856     * @since 6.0.0
     857     */
     858    public static function reset_default_labels() {
     859        self::$default_labels = array();
     860    }
    788861}
Note: See TracChangeset for help on using the changeset viewer.