Skip to content

Commit

Permalink
Merge pull request #254 from nosolosw/extract/strings-from-theme-json
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera authored Jul 19, 2021
2 parents a96ba25 + 57cd153 commit c43831d
Show file tree
Hide file tree
Showing 4 changed files with 466 additions and 0 deletions.
51 changes: 51 additions & 0 deletions assets/theme-i18n.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"settings": {
"typography": {
"fontSizes": [
{
"name": "Font size name"
}
]
},
"color": {
"palette": [
{
"name": "Color name"
}
],
"gradients": [
{
"name": "Gradient name"
}
],
"duotone": [
{
"name": "Duotone name"
}
]
},
"blocks": {
"*": {
"typography": {
"fontSizes": [
{
"name": "Font size name"
}
]
},
"color": {
"palette": [
{
"name": "Color name"
}
],
"gradients": [
{
"name": "Gradient name"
}
]
}
}
}
}
}
121 changes: 121 additions & 0 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3219,3 +3219,124 @@ 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:
"""
{
"version": "1",
"settings": {
"color": {
"palette": [
{ "slug": "black", "color": "#000000", "name": "Black" }
]
}
}
}
"""

When I try `wp i18n make-pot foo-theme --skip-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:
"""
{
"version": "1",
"settings": {
"color": {
"duotone": [
{ "slug": "dark-grayscale", "name": "Dark grayscale", "colors": [] }
],
"gradients": [
{ "slug": "purple-to-yellow", "name": "Purple to yellow" }
],
"palette": [
{ "slug": "black", "color": "#000000", "name": "Black" },
{ "slug": "white", "color": "#000000", "name": "White" }
]
},
"typography": {
"fontSizes": [
{ "name": "Small", "slug": "small", "size": "13px" }
]
}
}
}
"""

When I try `wp i18n make-pot foo-theme`
Then STDOUT should be:
"""
Success: POT file successfully generated!
"""
And the foo-theme/foo-theme.pot file should exist
And the foo-theme/foo-theme.pot file should contain:
"""
msgctxt "Duotone name"
msgid "Dark grayscale"
"""
And the foo-theme/foo-theme.pot file should contain:
"""
msgctxt "Gradient name"
msgid "Purple to yellow"
"""
And the foo-theme/foo-theme.pot file should contain:
"""
msgctxt "Color name"
msgid "White"
"""
And the foo-theme/foo-theme.pot file should contain:
"""
msgctxt "Color name"
msgid "White"
"""
And the foo-theme/foo-theme.pot file should contain:
"""
msgctxt "Font size name"
msgid "Small"
"""

Scenario: Extract strings from the blocks section of theme.json files
Given an empty foo-theme directory
And a foo-theme/theme.json file:
"""
{
"version": "1",
"settings": {
"blocks": {
"core/paragraph": {
"color": {
"palette": [
{ "slug": "black", "color": "#000000", "name": "Black" }
]
}
}
}
}
}
"""

When I try `wp i18n make-pot foo-theme`
Then STDOUT should be:
"""
Success: POT file successfully generated!
"""
And the foo-theme/foo-theme.pot file should exist
And the foo-theme/foo-theme.pot file should contain:
"""
msgctxt "Color name"
msgid "Black"
"""
23 changes: 23 additions & 0 deletions src/MakePotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ class MakePotCommand extends WP_CLI_Command {
*/
protected $skip_block_json = false;

/**
* @var bool
*/
protected $skip_theme_json = false;

/**
* @var bool
*/
Expand Down Expand Up @@ -216,6 +221,9 @@ class MakePotCommand extends WP_CLI_Command {
* [--skip-block-json]
* : Skips string extraction from block.json files.
*
* [--skip-theme-json]
* : Skips string extraction from theme.json files.
*
* [--skip-audit]
* : Skips string audit where it tries to find possible mistakes in translatable strings. Useful when running in an
* automated environment.
Expand Down Expand Up @@ -295,6 +303,7 @@ public function handle_arguments( $args, $assoc_args ) {
$this->skip_js = Utils\get_flag_value( $assoc_args, 'skip-js', $this->skip_js );
$this->skip_php = Utils\get_flag_value( $assoc_args, 'skip-php', $this->skip_php );
$this->skip_block_json = Utils\get_flag_value( $assoc_args, 'skip-block-json', $this->skip_block_json );
$this->skip_theme_json = Utils\get_flag_value( $assoc_args, 'skip-theme-json', $this->skip_theme_json );
$this->skip_audit = Utils\get_flag_value( $assoc_args, 'skip-audit', $this->skip_audit );
$this->headers = Utils\get_flag_value( $assoc_args, 'headers', $this->headers );
$this->file_comment = Utils\get_flag_value( $assoc_args, 'file-comment' );
Expand Down Expand Up @@ -639,6 +648,20 @@ protected function extract_strings() {
]
);
}

if ( ! $this->skip_theme_json ) {
ThemeJsonExtractor::fromDirectory(
$this->source,
$translations,
[
// Only look for theme.json files, nothing else.
'restrictFileNames' => [ 'theme.json' ],
'include' => $this->include,
'exclude' => $this->exclude,
'extensions' => [ 'json' ],
]
);
}
} catch ( \Exception $e ) {
WP_CLI::error( $e->getMessage() );
}
Expand Down
Loading

0 comments on commit c43831d

Please sign in to comment.