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

Translate title field from theme.json #306

Merged
merged 8 commits into from
Mar 31, 2022
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
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