Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract strings for translation from theme.json #254

Merged
merged 22 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -3155,3 +3155,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 @@ -206,6 +211,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 @@ -285,6 +293,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 @@ -624,6 +633,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