Skip to content

Commit

Permalink
Translate pattern metadata (title & description) (#40047)
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal authored Apr 6, 2022
1 parent aa31078 commit 10d4367
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/compat/wordpress-6.0/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,17 @@ function gutenberg_register_theme_block_patterns() {
);

// Register patterns for the active theme. If the theme is a child theme,
// let it override any patterns from the parent theme that shares the same
// slug.
foreach ( wp_get_active_and_valid_themes() as $theme ) {
$dirpath = $theme . '/patterns/';
// let it override any patterns from the parent theme that shares the same slug.
$themes = array();
$stylesheet = get_stylesheet();
$template = get_template();
if ( $stylesheet !== $template ) {
$themes[] = wp_get_theme( $stylesheet );
}
$themes[] = wp_get_theme( $template );

foreach ( $themes as $theme ) {
$dirpath = $theme->get_stylesheet_directory() . '/patterns/';
if ( file_exists( $dirpath ) ) {
$files = glob( $dirpath . '*.php' );
if ( $files ) {
Expand Down Expand Up @@ -171,6 +178,15 @@ function gutenberg_register_theme_block_patterns() {
}
}

// Translate the pattern metadata.
$text_domain = $theme->get( 'TextDomain' );
//phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.NonSingularStringLiteralContext, WordPress.WP.I18n.NonSingularStringLiteralDomain, WordPress.WP.I18n.LowLevelTranslationFunction
$pattern_data['title'] = translate_with_gettext_context( $pattern_data['title'], 'Pattern title', $text_domain );
if ( ! empty( $pattern_data['description'] ) ) {
//phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.NonSingularStringLiteralContext, WordPress.WP.I18n.NonSingularStringLiteralDomain, WordPress.WP.I18n.LowLevelTranslationFunction
$pattern_data['description'] = translate_with_gettext_context( $pattern_data['description'], 'Pattern description', $text_domain );
}

// The actual pattern content is the output of the file.
ob_start();
include $file;
Expand Down

0 comments on commit 10d4367

Please sign in to comment.