Make WordPress Core


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.