Skip to content

Commit

Permalink
Merge pull request #306 from oandregal/add/name-field-to-theme-json
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy authored Mar 31, 2022
2 parents 06c48c2 + 7a8e7ba commit 85c9abe
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions assets/theme-i18n.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"title": "Style variation name",
"settings": {
"typography": {
"fontSizes": [
Expand Down
7 changes: 6 additions & 1 deletion features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -3536,6 +3535,7 @@ Feature: Generate a POT file of a WordPress project
"""
{
"version": "1",
"title": "My style variation",
"settings": {
"color": {
"duotone": [
Expand Down Expand Up @@ -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
Expand Down
25 changes: 23 additions & 2 deletions src/ThemeJsonExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
Expand Down Expand Up @@ -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[] = [
Expand Down

0 comments on commit 85c9abe

Please sign in to comment.