diff --git a/assets/theme-i18n.json b/assets/theme-i18n.json index 348dfbf0..65281b13 100644 --- a/assets/theme-i18n.json +++ b/assets/theme-i18n.json @@ -1,4 +1,5 @@ { + "title": "Style variation name", "settings": { "typography": { "fontSizes": [ diff --git a/features/makepot.feature b/features/makepot.feature index 53bcfd4f..691791eb 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -3501,7 +3501,6 @@ Feature: Generate a POT file of a WordPress project msgid "Notice" """ - Scenario: Skips theme.json file if skip-theme-json flag provided Given an empty foo-theme directory And a foo-theme/theme.json file: @@ -3536,6 +3535,7 @@ Feature: Generate a POT file of a WordPress project """ { "version": "1", + "title": "My style variation", "settings": { "color": { "duotone": [ @@ -3589,6 +3589,11 @@ Feature: Generate a POT file of a WordPress project msgctxt "Font size name" msgid "Small" """ + And the foo-theme/foo-theme.pot file should contain: + """ + msgctxt "Style variation name" + msgid "My style variation" + """ Scenario: Extract strings from the blocks section of theme.json files Given an empty foo-theme directory diff --git a/src/ThemeJsonExtractor.php b/src/ThemeJsonExtractor.php index b36ec325..50e6a8a8 100644 --- a/src/ThemeJsonExtractor.php +++ b/src/ThemeJsonExtractor.php @@ -51,7 +51,8 @@ public static function fromString( $string, Translations $translations, array $o * One example of such a path would be: * [ 'settings', 'blocks', '*', 'color', 'palette' ] */ - $nodes_to_iterate = array_keys( $path, '*', true ); + $nodes_to_iterate = array_keys( $path, '*', true ); + $array_to_translate = null; if ( ! empty( $nodes_to_iterate ) ) { /* * At the moment, we only need to support one '*' in the path, so take it directly. @@ -77,7 +78,19 @@ public static function fromString( $string, Translations $translations, array $o } } } else { - $array_to_translate = self::array_get( $theme_json, $path ); + // We want to translate a top-level key, so we extract it if it exists. + if ( empty( $path ) && ! empty( $theme_json[ $key ] ) ) { + $array_to_translate = [ + [ + $key => $theme_json[ $key ], + ], + ]; + } + + if ( ! empty( $path ) ) { + $array_to_translate = self::array_get( $theme_json, $path ); + } + if ( null === $array_to_translate ) { continue; } @@ -207,6 +220,14 @@ private static function get_fields_to_translate() { private static function extract_paths_to_translate( $i18n_partial, $current_path = [] ) { $result = []; foreach ( $i18n_partial as $property => $partial_child ) { + if ( is_string( $partial_child ) ) { + $result[] = [ + 'path' => $current_path, + 'key' => $property, + 'context' => $partial_child, + ]; + continue; + } if ( is_numeric( $property ) ) { foreach ( $partial_child as $key => $context ) { $result[] = [