diff --git a/lib/class-wp-theme-json-resolver.php b/lib/class-wp-theme-json-resolver.php index d1429ff1544fd5..199a1eccfc5da4 100644 --- a/lib/class-wp-theme-json-resolver.php +++ b/lib/class-wp-theme-json-resolver.php @@ -151,6 +151,34 @@ public static function get_fields_to_translate() { return $theme_json_i18n; } + /** + * Translates a chunk of the loaded theme.json structure. + * + * @param array $array_to_translate The chunk of theme.json to translate. + * @param string $key The key of the field that contains the string to translate. + * @param string $context The context to apply in the translation call. + * @param string $domain Text domain. Unique identifier for retrieving translated strings. + * + * @return array Returns the modified $theme_json chunk. + */ + private static function translate_theme_json_chunk( $array_to_translate, $key, $context, $domain ) { + if ( null === $array_to_translate ) { + return $array_to_translate; + } + + foreach ( $array_to_translate as $item_key => $item_to_translate ) { + if ( empty( $item_to_translate[ $key ] ) ) { + continue; + } + + // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralContext,WordPress.WP.I18n.NonSingularStringLiteralDomain + $array_to_translate[ $item_key ][ $key ] = translate_with_gettext_context( $array_to_translate[ $item_key ][ $key ], $context, $domain ); + // phpcs:enable + } + + return $array_to_translate; + } + /** * Given a theme.json structure modifies it in place * to update certain values by its translated strings @@ -168,56 +196,20 @@ private static function translate( $theme_json, $domain = 'default' ) { $path = $field['path']; $key = $field['key']; $context = $field['context']; - if ( 'customTemplates' === $path[0] ) { - $array_to_translate = _wp_array_get( $theme_json, $path, null ); - if ( null === $array_to_translate ) { + if ( 'settings' === $path[0] ) { + if ( empty( $theme_json['settings'] ) ) { continue; } - - foreach ( $array_to_translate as $item_key => $item_to_translate ) { - if ( empty( $item_to_translate[ $key ] ) ) { - continue; - } - - // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralContext,WordPress.WP.I18n.NonSingularStringLiteralDomain - $array_to_translate[ $item_key ][ $key ] = translate_with_gettext_context( $array_to_translate[ $item_key ][ $key ], $context, $domain ); - // phpcs:enable + $path = array_slice( $path, 2 ); + foreach ( $theme_json['settings'] as $setting_key => $setting ) { + $array_to_translate = _wp_array_get( $setting, $path, null ); + $translated_array = self::translate_theme_json_chunk( $array_to_translate, $key, $context, $domain ); + gutenberg_experimental_set( $theme_json['settings'][ $setting_key ], $path, $translated_array ); } - - gutenberg_experimental_set( $theme_json, $path, $array_to_translate ); - } - } - - if ( ! isset( $theme_json['settings'] ) ) { - return $theme_json; - } - - foreach ( $theme_json['settings'] as $setting_key => $settings ) { - if ( empty( $settings ) ) { - continue; - } - - foreach ( $fields as $field ) { - $path = array_slice( $field['path'], 2 ); - $key = $field['key']; - $context = $field['context']; - - $array_to_translate = _wp_array_get( $theme_json['settings'][ $setting_key ], $path, null ); - if ( null === $array_to_translate ) { - continue; - } - - foreach ( $array_to_translate as $item_key => $item_to_translate ) { - if ( empty( $item_to_translate[ $key ] ) ) { - continue; - } - - // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralContext,WordPress.WP.I18n.NonSingularStringLiteralDomain - $array_to_translate[ $item_key ][ $key ] = translate_with_gettext_context( $array_to_translate[ $item_key ][ $key ], $context, $domain ); - // phpcs:enable - } - - gutenberg_experimental_set( $theme_json['settings'][ $setting_key ], $path, $array_to_translate ); + } else { + $array_to_translate = _wp_array_get( $theme_json, $path, null ); + $translated_array = self::translate_theme_json_chunk( $array_to_translate, $key, $context, $domain ); + gutenberg_experimental_set( $theme_json, $path, $translated_array ); } } diff --git a/phpunit/class-wp-theme-json-resolver-test.php b/phpunit/class-wp-theme-json-resolver-test.php index e088930c46778e..52c5e040469d2f 100644 --- a/phpunit/class-wp-theme-json-resolver-test.php +++ b/phpunit/class-wp-theme-json-resolver-test.php @@ -106,20 +106,26 @@ function test_translations_are_applied() { remove_filter( 'locale', array( $this, 'filter_set_locale_to_polish' ) ); $this->assertSame( wp_get_theme()->get( 'TextDomain' ), 'fse' ); - $this->assertSame( $actual->get_settings(), array() ); $this->assertSame( - $actual->get_custom_templates(), + $actual->get_settings()['root']['color']['palette'], array( - 'page-home' => array( - 'title' => 'Szablon strony głównej', + array( + 'slug' => 'light', + 'name' => 'Jasny', + 'color' => '#f5f7f9', + ), + array( + 'slug' => 'dark', + 'name' => 'Ciemny', + 'color' => '#000', ), ) ); $this->assertSame( - $actual->get_template_parts(), + $actual->get_custom_templates(), array( - 'small-header' => array( - 'area' => 'header', + 'page-home' => array( + 'title' => 'Szablon strony głównej', ), ) ); diff --git a/phpunit/data/languages/themes/fse-pl_PL.mo b/phpunit/data/languages/themes/fse-pl_PL.mo index 39f78524c49671..0ff620e64ae4ad 100644 Binary files a/phpunit/data/languages/themes/fse-pl_PL.mo and b/phpunit/data/languages/themes/fse-pl_PL.mo differ diff --git a/phpunit/data/languages/themes/fse-pl_PL.po b/phpunit/data/languages/themes/fse-pl_PL.po index c635bc0d447ac5..d55e02b8b54682 100644 --- a/phpunit/data/languages/themes/fse-pl_PL.po +++ b/phpunit/data/languages/themes/fse-pl_PL.po @@ -2,7 +2,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "POT-Creation-Date: 2015-12-31 16:31+0100\n" -"PO-Revision-Date: 2021-03-15 12:22+0100\n" +"PO-Revision-Date: 2021-03-15 13:10+0100\n" "Language: pl_PL\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" @@ -21,3 +21,11 @@ msgstr "" msgctxt "Custom template name" msgid "Homepage template" msgstr "Szablon strony głównej" + +msgctxt "Color name" +msgid "Light" +msgstr "Jasny" + +msgctxt "Color name" +msgid "Dark" +msgstr "Ciemny" diff --git a/phpunit/data/themedir1/fse/experimental-theme.json b/phpunit/data/themedir1/fse/experimental-theme.json index 5d5391182980e1..3cfe4accc5bebe 100644 --- a/phpunit/data/themedir1/fse/experimental-theme.json +++ b/phpunit/data/themedir1/fse/experimental-theme.json @@ -1,6 +1,22 @@ { "settings": { - "root": {} + "root": { + "color": { + "palette": [ + { + "slug": "light", + "name": "Light", + "color": "#f5f7f9" + }, + { + "slug": "dark", + "name": "Dark", + "color": "#000" + } + ], + "custom": false + } + } }, "customTemplates": [ {