From 9d6b04e1e88b05a6bbe2495da0570eb9d61ad8d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Tue, 9 Feb 2021 18:22:07 +0100 Subject: [PATCH] Implement new intermediate representation --- lib/class-wp-theme-json-resolver.php | 52 +++++++++++-------- phpunit/class-wp-theme-json-resolver-test.php | 40 ++++++++------ 2 files changed, 55 insertions(+), 37 deletions(-) diff --git a/lib/class-wp-theme-json-resolver.php b/lib/class-wp-theme-json-resolver.php index e17276ef5ebcb..ac820bb317deb 100644 --- a/lib/class-wp-theme-json-resolver.php +++ b/lib/class-wp-theme-json-resolver.php @@ -89,8 +89,8 @@ private static function get_from_file( $file_path ) { * "settings": { * "*": { * "typography": { - * "fontSizes": [ "name" ], - * "fontStyles": [ "name" ] + * "fontSizes": [ { "name": "Font size name" } ], + * "fontStyles": [ { "name": "Font size name" } ] * } * } * } @@ -100,12 +100,14 @@ private static function get_from_file( $file_path ) { * * [ * 0 => [ - * 'path' => [ 'settings', '*', 'typography', 'fontSizes' ], - * 'translatable_keys' => [ 'name' ] + * 'path' => [ 'settings', '*', 'typography', 'fontSizes' ], + * 'key' => 'name', + * 'context' => 'Font size name' * ], * 1 => [ - * 'path' => [ 'settings', '*', 'typography', 'fontStyles' ], - * 'translatable_keys' => [ 'name'] + * 'path' => [ 'settings', '*', 'typography', 'fontStyles' ], + * 'key' => 'name', + * 'context' => 'Font style name' * ] * ] * @@ -118,12 +120,15 @@ private static function theme_json_i18_file_structure_to_preset_paths( $file_str $result = array(); foreach ( $file_structure_partial as $property => $partial_child ) { if ( is_numeric( $property ) ) { - return array( - array( - 'path' => $current_path, - 'translatable_keys' => $file_structure_partial, - ), - ); + foreach( $partial_child as $key => $context ) { + return array( + array( + 'path' => $current_path, + 'key' => $key, + 'context' => $context, + ), + ); + } } $result = array_merge( $result, @@ -166,22 +171,27 @@ private static function translate_presets( &$theme_json_structure, $domain = 'de } foreach ( $preset_to_translate as $preset ) { - $path = array_slice( $preset['path'], 2 ); - $translatable_keys = $preset['translatable_keys']; + $path = array_slice( $preset['path'], 2 ); + $key = $preset['key']; + $context = $preset['context']; + $array_to_translate = gutenberg_experimental_get( $settings, $path, null ); if ( null === $array_to_translate ) { continue; } foreach ( $array_to_translate as &$item_to_translate ) { - foreach ( $translatable_keys as $translatable_key ) { - if ( empty( $item_to_translate[ $translatable_key ] ) ) { - continue; - } - // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralContext,WordPress.WP.I18n.NonSingularStringLiteralDomain - $item_to_translate[ $translatable_key ] = translate_with_gettext_context( $item_to_translate[ $translatable_key ], sprintf( 'theme %s %s', $path, $translatable_key ), $domain ); - // phpcs:enable + 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 + $item_to_translate[ $key ] = translate_with_gettext_context( + $item_to_translate[ $key ], + $context, + $domain + ); + // phpcs:enable } gutenberg_experimental_set( $settings, $path, $array_to_translate ); diff --git a/phpunit/class-wp-theme-json-resolver-test.php b/phpunit/class-wp-theme-json-resolver-test.php index 69ea2544c1b6b..e4714ad190f10 100644 --- a/phpunit/class-wp-theme-json-resolver-test.php +++ b/phpunit/class-wp-theme-json-resolver-test.php @@ -13,36 +13,44 @@ function test_presets_are_extracted() { $expected = array( array( - 'path' => array( 'settings', '*', 'typography', 'fontSizes' ), - 'translatable_keys' => array( 'name' ), + 'path' => array( 'settings', '*', 'typography', 'fontSizes' ), + 'key' => 'name', + 'context' => 'Font size name', ), array( - 'path' => array( 'settings', '*', 'typography', 'fontStyles' ), - 'translatable_keys' => array( 'name' ), + 'path' => array( 'settings', '*', 'typography', 'fontStyles' ), + 'key' => 'name', + 'context' => 'Font style name', ), array( - 'path' => array( 'settings', '*', 'typography', 'fontWeights' ), - 'translatable_keys' => array( 'name' ), + 'path' => array( 'settings', '*', 'typography', 'fontWeights' ), + 'key' => 'name', + 'context' => 'Font weight name', ), array( - 'path' => array( 'settings', '*', 'typography', 'fontFamilies' ), - 'translatable_keys' => array( 'name' ), + 'path' => array( 'settings', '*', 'typography', 'fontFamilies' ), + 'key' => 'name', + 'context' => 'Font family name', ), array( - 'path' => array( 'settings', '*', 'typography', 'textTransforms' ), - 'translatable_keys' => array( 'name' ), + 'path' => array( 'settings', '*', 'typography', 'textTransforms' ), + 'key' => 'name', + 'context' => 'Text transform name', ), array( - 'path' => array( 'settings', '*', 'typography', 'textDecorations' ), - 'translatable_keys' => array( 'name' ), + 'path' => array( 'settings', '*', 'typography', 'textDecorations' ), + 'key' => 'name', + 'context' => 'Text decoration name', ), array( - 'path' => array( 'settings', '*', 'color', 'palette' ), - 'translatable_keys' => array( 'name' ), + 'path' => array( 'settings', '*', 'color', 'palette' ), + 'key' => 'name', + 'context' => 'Color name', ), array( - 'path' => array( 'settings', '*', 'color', 'gradients' ), - 'translatable_keys' => array( 'name' ), + 'path' => array( 'settings', '*', 'color', 'gradients' ), + 'key' => 'name', + 'context' => 'Gradient name', ), );