From b7d53ee176e16ff5b603f8956641906f9a4080d3 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 19 Dec 2024 17:23:50 +0100 Subject: [PATCH] make-pot: scan any `theme.json` file in any level --- features/makepot.feature | 97 ++++++++++++++++++++++++++++++++++++++ src/MakePotCommand.php | 20 ++++---- src/ThemeJsonExtractor.php | 29 ------------ 3 files changed, 107 insertions(+), 39 deletions(-) delete mode 100644 src/ThemeJsonExtractor.php diff --git a/features/makepot.feature b/features/makepot.feature index 9ff975a..6039374 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -3651,6 +3651,35 @@ Feature: Generate a POT file of a WordPress project msgid "Black" """ + Scenario: Skips theme.json file if excluding it + Given an empty foo-theme directory + And a foo-theme/theme.json file: + """ + { + "version": "1", + "settings": { + "color": { + "palette": [ + { "slug": "black", "color": "#000000", "name": "Black" } + ] + } + } + } + """ + + When I try `wp i18n make-pot foo-theme --exclude=theme.json` + Then STDOUT should be: + """ + Success: POT file successfully generated. + """ + And the foo-theme/foo-theme.pot file should exist + But the foo-theme/foo-theme.pot file should not contain: + """ + msgctxt "Color name" + msgid "Black" + """ + + Scenario: Extract strings from the top-level section of theme.json files Given an empty foo-theme directory And a foo-theme/theme.json file: @@ -3972,3 +4001,71 @@ Feature: Generate a POT file of a WordPress project """ msgid "foo-plugin/longertests/foo-plugin.php" """ + + Scenario: Extract strings from theme.json files in any level + Given an empty foo-project directory + And a foo-project/theme.json file: + """ + { + "version": "1", + "title": "My style variation", + "description": "My style variation description" + } + """ + + And a foo-project/nested/theme.json file: + """ + { + "version": "1", + "title": "Nested style variation", + "description": "Nested style variation description" + } + """ + + And a foo-project/nested/notatheme.json file: + """ + { + "version": "1", + "title": "Not extracted style variation", + "description": "Not extracted style variation description" + } + """ + + When I try `wp i18n make-pot foo-project` + Then STDOUT should be: + """ + Success: POT file successfully generated. + """ + And the foo-project/foo-project.pot file should exist + And the foo-project/foo-project.pot file should contain: + """ + #: theme.json + msgctxt "Style variation name" + msgid "My style variation" + """ + And the foo-project/foo-project.pot file should contain: + """ + #: theme.json + msgctxt "Style variation description" + msgid "My style variation description" + """ + And the foo-project/foo-project.pot file should contain: + """ + #: nested/theme.json + msgctxt "Style variation name" + msgid "Nested style variation" + """ + And the foo-project/foo-project.pot file should contain: + """ + #: nested/theme.json + msgctxt "Style variation description" + msgid "Nested style variation description" + """ + And the foo-project/foo-project.pot file should not contain: + """ + msgid "Not extract style variation" + """ + And the foo-project/foo-project.pot file should not contain: + """ + msgid "Not extracted style variation description" + """ diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index f3e84de..6575010 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -693,7 +693,7 @@ protected function extract_strings() { [ 'schema' => JsonSchemaExtractor::BLOCK_JSON_SOURCE, 'schemaFallback' => JsonSchemaExtractor::BLOCK_JSON_FALLBACK, - // Only look for block.json files, nothing else. + // Only look for block.json files in any folder, nothing else. 'restrictFileNames' => [ 'block.json' ], 'include' => $this->include, 'exclude' => $this->exclude, @@ -704,18 +704,18 @@ protected function extract_strings() { } if ( ! $this->skip_theme_json ) { - // This will look for the top-level theme.json file, as well as - // any JSON file within the top-level styles/ directory. - ThemeJsonExtractor::fromDirectory( + JsonSchemaExtractor::fromDirectory( $this->source, $translations, [ - 'schema' => JsonSchemaExtractor::THEME_JSON_SOURCE, - 'schemaFallback' => JsonSchemaExtractor::THEME_JSON_FALLBACK, - 'include' => $this->include, - 'exclude' => $this->exclude, - 'extensions' => [ 'json' ], - 'addReferences' => $this->location, + // Only look for theme.json files in any folder, nothing else. + 'restrictFileNames' => [ 'theme.json' ], + 'schema' => JsonSchemaExtractor::THEME_JSON_SOURCE, + 'schemaFallback' => JsonSchemaExtractor::THEME_JSON_FALLBACK, + 'include' => $this->include, + 'exclude' => $this->exclude, + 'extensions' => [ 'json' ], + 'addReferences' => $this->location, ] ); } diff --git a/src/ThemeJsonExtractor.php b/src/ThemeJsonExtractor.php deleted file mode 100644 index c8981aa..0000000 --- a/src/ThemeJsonExtractor.php +++ /dev/null @@ -1,29 +0,0 @@ -