Make WordPress Core

Changeset 53058


Ignore:
Timestamp:
04/04/2022 03:48:08 AM (3 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.

Location:
trunk/src/wp-includes
Files:
4 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}
  • trunk/src/wp-includes/class-wp-taxonomy.php

    r52652 r53058  
    4242     */
    4343    public $labels;
     44
     45    /**
     46     * Default labels.
     47     *
     48     * @since 6.0.0
     49     * @var (string|null)[][] $default_labels
     50     */
     51    protected static $default_labels = array();
    4452
    4553    /**
     
    563571        return $this->rest_controller;
    564572    }
     573
     574    /**
     575     * Returns the default labels for taxonomies.
     576     *
     577     * @since 6.0.0
     578     *
     579     * @return (string|null)[][] The default labels for taxonomies.
     580     */
     581    public static function get_default_labels() {
     582        if ( ! empty( self::$default_labels ) ) {
     583            return self::$default_labels;
     584        }
     585
     586        $name_field_description   = __( 'The name is how it appears on your site.' );
     587        $slug_field_description   = __( 'The “slug” is the URL-friendly version of the name. It is usually all lowercase and contains only letters, numbers, and hyphens.' );
     588        $parent_field_description = __( 'Assign a parent term to create a hierarchy. The term Jazz, for example, would be the parent of Bebop and Big Band.' );
     589        $desc_field_description   = __( 'The description is not prominent by default; however, some themes may show it.' );
     590
     591        self::$default_labels = array(
     592            'name'                       => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ),
     593            'singular_name'              => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ),
     594            'search_items'               => array( __( 'Search Tags' ), __( 'Search Categories' ) ),
     595            'popular_items'              => array( __( 'Popular Tags' ), null ),
     596            'all_items'                  => array( __( 'All Tags' ), __( 'All Categories' ) ),
     597            'parent_item'                => array( null, __( 'Parent Category' ) ),
     598            'parent_item_colon'          => array( null, __( 'Parent Category:' ) ),
     599            'name_field_description'     => array( $name_field_description, $name_field_description ),
     600            'slug_field_description'     => array( $slug_field_description, $slug_field_description ),
     601            'parent_field_description'   => array( null, $parent_field_description ),
     602            'desc_field_description'     => array( $desc_field_description, $desc_field_description ),
     603            'edit_item'                  => array( __( 'Edit Tag' ), __( 'Edit Category' ) ),
     604            'view_item'                  => array( __( 'View Tag' ), __( 'View Category' ) ),
     605            'update_item'                => array( __( 'Update Tag' ), __( 'Update Category' ) ),
     606            'add_new_item'               => array( __( 'Add New Tag' ), __( 'Add New Category' ) ),
     607            'new_item_name'              => array( __( 'New Tag Name' ), __( 'New Category Name' ) ),
     608            'separate_items_with_commas' => array( __( 'Separate tags with commas' ), null ),
     609            'add_or_remove_items'        => array( __( 'Add or remove tags' ), null ),
     610            'choose_from_most_used'      => array( __( 'Choose from the most used tags' ), null ),
     611            'not_found'                  => array( __( 'No tags found.' ), __( 'No categories found.' ) ),
     612            'no_terms'                   => array( __( 'No tags' ), __( 'No categories' ) ),
     613            'filter_by_item'             => array( null, __( 'Filter by category' ) ),
     614            'items_list_navigation'      => array( __( 'Tags list navigation' ), __( 'Categories list navigation' ) ),
     615            'items_list'                 => array( __( 'Tags list' ), __( 'Categories list' ) ),
     616            /* translators: Tab heading when selecting from the most used terms. */
     617            'most_used'                  => array( _x( 'Most Used', 'tags' ), _x( 'Most Used', 'categories' ) ),
     618            'back_to_items'              => array( __( '← Go to Tags' ), __( '← Go to Categories' ) ),
     619            'item_link'                  => array(
     620                _x( 'Tag Link', 'navigation link block title' ),
     621                _x( 'Category Link', 'navigation link block title' ),
     622            ),
     623            'item_link_description'      => array(
     624                _x( 'A link to a tag.', 'navigation link block description' ),
     625                _x( 'A link to a category.', 'navigation link block description' ),
     626            ),
     627        );
     628
     629        return self::$default_labels;
     630    }
     631
     632    /**
     633     * Resets the cache for the default labels.
     634     *
     635     * @since 6.0.0
     636     */
     637    public static function reset_default_labels() {
     638        self::$default_labels = array();
     639    }
    565640}
  • trunk/src/wp-includes/post.php

    r53042 r53058  
    1919 */
    2020function create_initial_post_types() {
     21    WP_Post_Type::reset_default_labels();
     22
    2123    register_post_type(
    2224        'post',
     
    19561958 */
    19571959function get_post_type_labels( $post_type_object ) {
    1958     $nohier_vs_hier_defaults = array(
    1959         'name'                     => array( _x( 'Posts', 'post type general name' ), _x( 'Pages', 'post type general name' ) ),
    1960         'singular_name'            => array( _x( 'Post', 'post type singular name' ), _x( 'Page', 'post type singular name' ) ),
    1961         'add_new'                  => array( _x( 'Add New', 'post' ), _x( 'Add New', 'page' ) ),
    1962         'add_new_item'             => array( __( 'Add New Post' ), __( 'Add New Page' ) ),
    1963         'edit_item'                => array( __( 'Edit Post' ), __( 'Edit Page' ) ),
    1964         'new_item'                 => array( __( 'New Post' ), __( 'New Page' ) ),
    1965         'view_item'                => array( __( 'View Post' ), __( 'View Page' ) ),
    1966         'view_items'               => array( __( 'View Posts' ), __( 'View Pages' ) ),
    1967         'search_items'             => array( __( 'Search Posts' ), __( 'Search Pages' ) ),
    1968         'not_found'                => array( __( 'No posts found.' ), __( 'No pages found.' ) ),
    1969         'not_found_in_trash'       => array( __( 'No posts found in Trash.' ), __( 'No pages found in Trash.' ) ),
    1970         'parent_item_colon'        => array( null, __( 'Parent Page:' ) ),
    1971         'all_items'                => array( __( 'All Posts' ), __( 'All Pages' ) ),
    1972         'archives'                 => array( __( 'Post Archives' ), __( 'Page Archives' ) ),
    1973         'attributes'               => array( __( 'Post Attributes' ), __( 'Page Attributes' ) ),
    1974         'insert_into_item'         => array( __( 'Insert into post' ), __( 'Insert into page' ) ),
    1975         'uploaded_to_this_item'    => array( __( 'Uploaded to this post' ), __( 'Uploaded to this page' ) ),
    1976         'featured_image'           => array( _x( 'Featured image', 'post' ), _x( 'Featured image', 'page' ) ),
    1977         'set_featured_image'       => array( _x( 'Set featured image', 'post' ), _x( 'Set featured image', 'page' ) ),
    1978         'remove_featured_image'    => array( _x( 'Remove featured image', 'post' ), _x( 'Remove featured image', 'page' ) ),
    1979         'use_featured_image'       => array( _x( 'Use as featured image', 'post' ), _x( 'Use as featured image', 'page' ) ),
    1980         'filter_items_list'        => array( __( 'Filter posts list' ), __( 'Filter pages list' ) ),
    1981         'filter_by_date'           => array( __( 'Filter by date' ), __( 'Filter by date' ) ),
    1982         'items_list_navigation'    => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ),
    1983         'items_list'               => array( __( 'Posts list' ), __( 'Pages list' ) ),
    1984         'item_published'           => array( __( 'Post published.' ), __( 'Page published.' ) ),
    1985         'item_published_privately' => array( __( 'Post published privately.' ), __( 'Page published privately.' ) ),
    1986         'item_reverted_to_draft'   => array( __( 'Post reverted to draft.' ), __( 'Page reverted to draft.' ) ),
    1987         'item_scheduled'           => array( __( 'Post scheduled.' ), __( 'Page scheduled.' ) ),
    1988         'item_updated'             => array( __( 'Post updated.' ), __( 'Page updated.' ) ),
    1989         'item_link'                => array(
    1990             _x( 'Post Link', 'navigation link block title' ),
    1991             _x( 'Page Link', 'navigation link block title' ),
    1992         ),
    1993         'item_link_description'    => array(
    1994             _x( 'A link to a post.', 'navigation link block description' ),
    1995             _x( 'A link to a page.', 'navigation link block description' ),
    1996         ),
    1997     );
     1960    $nohier_vs_hier_defaults = WP_Post_Type::get_default_labels();
    19981961
    19991962    $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
  • trunk/src/wp-includes/taxonomy.php

    r52992 r53058  
    2525function create_initial_taxonomies() {
    2626    global $wp_rewrite;
     27
     28    WP_Taxonomy::reset_default_labels();
    2729
    2830    if ( ! did_action( 'init' ) ) {
     
    660662    }
    661663
    662     $name_field_description   = __( 'The name is how it appears on your site.' );
    663     $slug_field_description   = __( 'The “slug” is the URL-friendly version of the name. It is usually all lowercase and contains only letters, numbers, and hyphens.' );
    664     $parent_field_description = __( 'Assign a parent term to create a hierarchy. The term Jazz, for example, would be the parent of Bebop and Big Band.' );
    665     $desc_field_description   = __( 'The description is not prominent by default; however, some themes may show it.' );
    666 
    667     $nohier_vs_hier_defaults = array(
    668         'name'                       => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ),
    669         'singular_name'              => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ),
    670         'search_items'               => array( __( 'Search Tags' ), __( 'Search Categories' ) ),
    671         'popular_items'              => array( __( 'Popular Tags' ), null ),
    672         'all_items'                  => array( __( 'All Tags' ), __( 'All Categories' ) ),
    673         'parent_item'                => array( null, __( 'Parent Category' ) ),
    674         'parent_item_colon'          => array( null, __( 'Parent Category:' ) ),
    675         'name_field_description'     => array( $name_field_description, $name_field_description ),
    676         'slug_field_description'     => array( $slug_field_description, $slug_field_description ),
    677         'parent_field_description'   => array( null, $parent_field_description ),
    678         'desc_field_description'     => array( $desc_field_description, $desc_field_description ),
    679         'edit_item'                  => array( __( 'Edit Tag' ), __( 'Edit Category' ) ),
    680         'view_item'                  => array( __( 'View Tag' ), __( 'View Category' ) ),
    681         'update_item'                => array( __( 'Update Tag' ), __( 'Update Category' ) ),
    682         'add_new_item'               => array( __( 'Add New Tag' ), __( 'Add New Category' ) ),
    683         'new_item_name'              => array( __( 'New Tag Name' ), __( 'New Category Name' ) ),
    684         'separate_items_with_commas' => array( __( 'Separate tags with commas' ), null ),
    685         'add_or_remove_items'        => array( __( 'Add or remove tags' ), null ),
    686         'choose_from_most_used'      => array( __( 'Choose from the most used tags' ), null ),
    687         'not_found'                  => array( __( 'No tags found.' ), __( 'No categories found.' ) ),
    688         'no_terms'                   => array( __( 'No tags' ), __( 'No categories' ) ),
    689         'filter_by_item'             => array( null, __( 'Filter by category' ) ),
    690         'items_list_navigation'      => array( __( 'Tags list navigation' ), __( 'Categories list navigation' ) ),
    691         'items_list'                 => array( __( 'Tags list' ), __( 'Categories list' ) ),
    692         /* translators: Tab heading when selecting from the most used terms. */
    693         'most_used'                  => array( _x( 'Most Used', 'tags' ), _x( 'Most Used', 'categories' ) ),
    694         'back_to_items'              => array( __( '← Go to Tags' ), __( '← Go to Categories' ) ),
    695         'item_link'                  => array(
    696             _x( 'Tag Link', 'navigation link block title' ),
    697             _x( 'Category Link', 'navigation link block title' ),
    698         ),
    699         'item_link_description'      => array(
    700             _x( 'A link to a tag.', 'navigation link block description' ),
    701             _x( 'A link to a category.', 'navigation link block description' ),
    702         ),
    703     );
     664    $nohier_vs_hier_defaults = WP_Taxonomy::get_default_labels();
    704665
    705666    $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
Note: See TracChangeset for help on using the changeset viewer.