diff --git a/plugins/woocommerce/changelog/51843-fix-l10n-too-early b/plugins/woocommerce/changelog/51843-fix-l10n-too-early new file mode 100644 index 00000000000..829701a0c02 --- /dev/null +++ b/plugins/woocommerce/changelog/51843-fix-l10n-too-early @@ -0,0 +1,4 @@ +Significance: minor +Type: fix + +Removes several side effects in the code base that caused translations to be loaded too early. diff --git a/plugins/woocommerce/src/Blocks/BlockPatterns.php b/plugins/woocommerce/src/Blocks/BlockPatterns.php index 5e72bbcfc96..e2e402fc547 100644 --- a/plugins/woocommerce/src/Blocks/BlockPatterns.php +++ b/plugins/woocommerce/src/Blocks/BlockPatterns.php @@ -82,8 +82,6 @@ class BlockPatterns { $this->pattern_registry = $pattern_registry; $this->ptk_patterns_store = $ptk_patterns_store; - $this->dictionary = PatternsHelper::get_patterns_dictionary(); - add_action( 'init', array( $this, 'register_block_patterns' ) ); if ( Features::is_enabled( 'pattern-toolkit-full-composability' ) ) { @@ -91,6 +89,19 @@ class BlockPatterns { } } + /** + * Returns the Patterns dictionary. + * + * @return array|WP_Error + */ + private function get_patterns_dictionary() { + if ( null === $this->dictionary ) { + $this->dictionary = PatternsHelper::get_patterns_dictionary(); + } + + return $this->dictionary; + } + /** * Register block patterns from core. * @@ -103,7 +114,7 @@ class BlockPatterns { $patterns = $this->get_block_patterns(); foreach ( $patterns as $pattern ) { - $this->pattern_registry->register_block_pattern( $pattern['source'], $pattern, $this->dictionary ); + $this->pattern_registry->register_block_pattern( $pattern['source'], $pattern, $this->get_patterns_dictionary() ); } } @@ -212,7 +223,7 @@ class BlockPatterns { $pattern['slug'] = $pattern['name']; $pattern['content'] = $pattern['html']; - $this->pattern_registry->register_block_pattern( $pattern['ID'], $pattern, $this->dictionary ); + $this->pattern_registry->register_block_pattern( $pattern['ID'], $pattern, $this->get_patterns_dictionary() ); } } diff --git a/plugins/woocommerce/src/Blocks/Patterns/PatternRegistry.php b/plugins/woocommerce/src/Blocks/Patterns/PatternRegistry.php index 3af03be5200..61c2528e59d 100644 --- a/plugins/woocommerce/src/Blocks/Patterns/PatternRegistry.php +++ b/plugins/woocommerce/src/Blocks/Patterns/PatternRegistry.php @@ -12,20 +12,15 @@ class PatternRegistry { const SLUG_REGEX = '/^[A-z0-9\/_-]+$/'; const COMMA_SEPARATED_REGEX = '/[\s,]+/'; - /** - * Associates pattern slugs with their localized labels for categorization. + * Returns pattern slugs with their localized labels for categorization. + * * Each key represents a unique pattern slug, while the value is the localized label. * - * @var array $category_labels + * @return array */ - private $category_labels; - - /** - * Constructor. - */ - public function __construct() { - $this->category_labels = [ + private function get_category_labels() { + return [ 'woo-commerce' => __( 'WooCommerce', 'woocommerce' ), 'intro' => __( 'Intro', 'woocommerce' ), 'featured-selling' => __( 'Featured Selling', 'woocommerce' ), @@ -148,10 +143,10 @@ class PatternRegistry { } } - // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction + // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction $pattern_data['title'] = translate_with_gettext_context( $pattern_data['title'], 'Pattern title', 'woocommerce' ); if ( ! empty( $pattern_data['description'] ) ) { - // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction + // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction $pattern_data['description'] = translate_with_gettext_context( $pattern_data['description'], 'Pattern description', 'woocommerce' ); } @@ -186,13 +181,15 @@ class PatternRegistry { } } + $category_labels = $this->get_category_labels(); + if ( ! empty( $pattern_data['categories'] ) ) { foreach ( $pattern_data['categories'] as $key => $category ) { $category_slug = _wp_to_kebab_case( $category ); $pattern_data['categories'][ $key ] = $category_slug; - $label = isset( $this->category_labels[ $category_slug ] ) ? $this->category_labels[ $category_slug ] : self::kebab_to_capital_case( $category_slug ); + $label = $category_labels[ $category_slug ] ?? self::kebab_to_capital_case( $category_slug ); register_block_pattern_category( $category_slug,