From def6ac9f29ab45a705e160546a3c29362b149945 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=28Greg=29=20Zi=C3=B3=C5=82kowski?= Date: Fri, 15 Jan 2021 18:49:49 +0100 Subject: [PATCH 1/8] Use context when translating entries in theme.json --- lib/class-wp-theme-json-resolver.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/class-wp-theme-json-resolver.php b/lib/class-wp-theme-json-resolver.php index 770ad800826e9..0360b37de7f5b 100644 --- a/lib/class-wp-theme-json-resolver.php +++ b/lib/class-wp-theme-json-resolver.php @@ -178,9 +178,8 @@ private static function translate_presets( &$theme_json_structure, $domain = 'de if ( empty( $item_to_translate[ $translatable_key ] ) ) { continue; } - - // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain - $item_to_translate[ $translatable_key ] = translate( $item_to_translate[ $translatable_key ], $domain ); + // 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 } } From 2e20ae23a766d117a3321857dbc7735f03eaab22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Tue, 9 Feb 2021 17:52:28 +0100 Subject: [PATCH 2/8] Add test --- lib/class-wp-theme-json-resolver.php | 2 +- phpunit/class-wp-theme-json-resolver-test.php | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 phpunit/class-wp-theme-json-resolver-test.php diff --git a/lib/class-wp-theme-json-resolver.php b/lib/class-wp-theme-json-resolver.php index 0360b37de7f5b..e17276ef5ebcb 100644 --- a/lib/class-wp-theme-json-resolver.php +++ b/lib/class-wp-theme-json-resolver.php @@ -138,7 +138,7 @@ private static function theme_json_i18_file_structure_to_preset_paths( $file_str * * @return array An array of theme.json paths that are translatable and the keys that are translatable */ - private static function get_presets_to_translate() { + public static function get_presets_to_translate() { static $theme_json_i18n = null; if ( null === $theme_json_i18n ) { $file_structure = self::get_from_file( __DIR__ . '/experimental-i18n-theme.json' ); diff --git a/phpunit/class-wp-theme-json-resolver-test.php b/phpunit/class-wp-theme-json-resolver-test.php new file mode 100644 index 0000000000000..69ea2544c1b6b --- /dev/null +++ b/phpunit/class-wp-theme-json-resolver-test.php @@ -0,0 +1,52 @@ + array( 'settings', '*', 'typography', 'fontSizes' ), + 'translatable_keys' => array( 'name' ), + ), + array( + 'path' => array( 'settings', '*', 'typography', 'fontStyles' ), + 'translatable_keys' => array( 'name' ), + ), + array( + 'path' => array( 'settings', '*', 'typography', 'fontWeights' ), + 'translatable_keys' => array( 'name' ), + ), + array( + 'path' => array( 'settings', '*', 'typography', 'fontFamilies' ), + 'translatable_keys' => array( 'name' ), + ), + array( + 'path' => array( 'settings', '*', 'typography', 'textTransforms' ), + 'translatable_keys' => array( 'name' ), + ), + array( + 'path' => array( 'settings', '*', 'typography', 'textDecorations' ), + 'translatable_keys' => array( 'name' ), + ), + array( + 'path' => array( 'settings', '*', 'color', 'palette' ), + 'translatable_keys' => array( 'name' ), + ), + array( + 'path' => array( 'settings', '*', 'color', 'gradients' ), + 'translatable_keys' => array( 'name' ), + ), + ); + + $this->assertEquals( $expected, $actual ); + } + +} \ No newline at end of file From 20897810bb5d3eae55d7a639244305d13801e3f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Tue, 9 Feb 2021 17:29:34 +0100 Subject: [PATCH 3/8] Update format --- lib/experimental-i18n-theme.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/experimental-i18n-theme.json b/lib/experimental-i18n-theme.json index cd8d4f2c76148..80c7011208d53 100644 --- a/lib/experimental-i18n-theme.json +++ b/lib/experimental-i18n-theme.json @@ -2,16 +2,16 @@ "settings": { "*": { "typography": { - "fontSizes": [ "name" ], - "fontStyles": [ "name" ], - "fontWeights": [ "name" ], - "fontFamilies": [ "name" ], - "textTransforms": [ "name" ], - "textDecorations": [ "name" ] + "fontSizes": [ { "name": "Font size name" } ], + "fontStyles": [ { "name": "Font style name" } ], + "fontWeights": [ { "name": "Font weight name" } ], + "fontFamilies": [ { "name": "Font family name" } ], + "textTransforms": [ { "name": "Text transform name" } ], + "textDecorations": [ { "name": "Text decoration name" } ] }, "color": { - "palette": [ "name" ], - "gradients": [ "name" ] + "palette": [ { "name": "Color name" } ], + "gradients": [ { "name": "Gradient name" } ] } } } 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 4/8] 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', ), ); From bfd244036cf766ca8e80b927e460c601f03ee4a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Tue, 9 Feb 2021 18:42:51 +0100 Subject: [PATCH 5/8] Fix name typo --- phpunit/class-wp-theme-json-resolver-test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/class-wp-theme-json-resolver-test.php b/phpunit/class-wp-theme-json-resolver-test.php index e4714ad190f10..accb1df8181c6 100644 --- a/phpunit/class-wp-theme-json-resolver-test.php +++ b/phpunit/class-wp-theme-json-resolver-test.php @@ -6,7 +6,7 @@ * @package Gutenberg */ -class WP_Theme_JSON_Test extends WP_UnitTestCase { +class WP_Theme_JSON_Resolver_Test extends WP_UnitTestCase { function test_presets_are_extracted() { $actual = WP_Theme_JSON_Resolver::get_presets_to_translate(); From 4c92f42ce1827d3ad0f3cccd7093cfd09e98831e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Tue, 9 Feb 2021 18:43:37 +0100 Subject: [PATCH 6/8] Linter: use tabs --- phpunit/class-wp-theme-json-resolver-test.php | 92 +++++++++---------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/phpunit/class-wp-theme-json-resolver-test.php b/phpunit/class-wp-theme-json-resolver-test.php index accb1df8181c6..4e83b59ba7d1b 100644 --- a/phpunit/class-wp-theme-json-resolver-test.php +++ b/phpunit/class-wp-theme-json-resolver-test.php @@ -8,53 +8,53 @@ class WP_Theme_JSON_Resolver_Test extends WP_UnitTestCase { - function test_presets_are_extracted() { - $actual = WP_Theme_JSON_Resolver::get_presets_to_translate(); + function test_presets_are_extracted() { + $actual = WP_Theme_JSON_Resolver::get_presets_to_translate(); - $expected = array( - array( - 'path' => array( 'settings', '*', 'typography', 'fontSizes' ), - 'key' => 'name', - 'context' => 'Font size name', - ), - array( - 'path' => array( 'settings', '*', 'typography', 'fontStyles' ), - 'key' => 'name', - 'context' => 'Font style name', - ), - array( - 'path' => array( 'settings', '*', 'typography', 'fontWeights' ), - 'key' => 'name', - 'context' => 'Font weight name', - ), - array( - 'path' => array( 'settings', '*', 'typography', 'fontFamilies' ), - 'key' => 'name', - 'context' => 'Font family name', - ), - array( - 'path' => array( 'settings', '*', 'typography', 'textTransforms' ), - 'key' => 'name', - 'context' => 'Text transform name', - ), - array( - 'path' => array( 'settings', '*', 'typography', 'textDecorations' ), - 'key' => 'name', - 'context' => 'Text decoration name', - ), - array( - 'path' => array( 'settings', '*', 'color', 'palette' ), - 'key' => 'name', - 'context' => 'Color name', - ), - array( - 'path' => array( 'settings', '*', 'color', 'gradients' ), - 'key' => 'name', - 'context' => 'Gradient name', - ), - ); + $expected = array( + array( + 'path' => array( 'settings', '*', 'typography', 'fontSizes' ), + 'key' => 'name', + 'context' => 'Font size name', + ), + array( + 'path' => array( 'settings', '*', 'typography', 'fontStyles' ), + 'key' => 'name', + 'context' => 'Font style name', + ), + array( + 'path' => array( 'settings', '*', 'typography', 'fontWeights' ), + 'key' => 'name', + 'context' => 'Font weight name', + ), + array( + 'path' => array( 'settings', '*', 'typography', 'fontFamilies' ), + 'key' => 'name', + 'context' => 'Font family name', + ), + array( + 'path' => array( 'settings', '*', 'typography', 'textTransforms' ), + 'key' => 'name', + 'context' => 'Text transform name', + ), + array( + 'path' => array( 'settings', '*', 'typography', 'textDecorations' ), + 'key' => 'name', + 'context' => 'Text decoration name', + ), + array( + 'path' => array( 'settings', '*', 'color', 'palette' ), + 'key' => 'name', + 'context' => 'Color name', + ), + array( + 'path' => array( 'settings', '*', 'color', 'gradients' ), + 'key' => 'name', + 'context' => 'Gradient name', + ), + ); - $this->assertEquals( $expected, $actual ); - } + $this->assertEquals( $expected, $actual ); + } } \ No newline at end of file From b4a8c8dca2e95b696f7d375d3d3514b499e26978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Tue, 9 Feb 2021 18:44:16 +0100 Subject: [PATCH 7/8] Make linter happy --- phpunit/class-wp-theme-json-resolver-test.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/phpunit/class-wp-theme-json-resolver-test.php b/phpunit/class-wp-theme-json-resolver-test.php index 4e83b59ba7d1b..80d8d9b4568f6 100644 --- a/phpunit/class-wp-theme-json-resolver-test.php +++ b/phpunit/class-wp-theme-json-resolver-test.php @@ -9,7 +9,7 @@ class WP_Theme_JSON_Resolver_Test extends WP_UnitTestCase { function test_presets_are_extracted() { - $actual = WP_Theme_JSON_Resolver::get_presets_to_translate(); + $actual = WP_Theme_JSON_Resolver::get_presets_to_translate(); $expected = array( array( @@ -18,7 +18,7 @@ function test_presets_are_extracted() { 'context' => 'Font size name', ), array( - 'path' => array( 'settings', '*', 'typography', 'fontStyles' ), + 'path' => array( 'settings', '*', 'typography', 'fontStyles' ), 'key' => 'name', 'context' => 'Font style name', ), @@ -56,5 +56,4 @@ function test_presets_are_extracted() { $this->assertEquals( $expected, $actual ); } - } \ No newline at end of file From 77943f3374d40828f58ac6b73a365c75fe2282d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Tue, 9 Feb 2021 18:46:08 +0100 Subject: [PATCH 8/8] Make linter happy --- lib/class-wp-theme-json-resolver.php | 8 ++------ phpunit/class-wp-theme-json-resolver-test.php | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/class-wp-theme-json-resolver.php b/lib/class-wp-theme-json-resolver.php index ac820bb317deb..51d63feb193cb 100644 --- a/lib/class-wp-theme-json-resolver.php +++ b/lib/class-wp-theme-json-resolver.php @@ -120,7 +120,7 @@ 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 ) ) { - foreach( $partial_child as $key => $context ) { + foreach ( $partial_child as $key => $context ) { return array( array( 'path' => $current_path, @@ -186,11 +186,7 @@ private static function translate_presets( &$theme_json_structure, $domain = 'de } // 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 - ); + $item_to_translate[ $key ] = translate_with_gettext_context( $item_to_translate[ $key ], $context, $domain ); // phpcs:enable } diff --git a/phpunit/class-wp-theme-json-resolver-test.php b/phpunit/class-wp-theme-json-resolver-test.php index 80d8d9b4568f6..6c2fa216bc9b0 100644 --- a/phpunit/class-wp-theme-json-resolver-test.php +++ b/phpunit/class-wp-theme-json-resolver-test.php @@ -56,4 +56,4 @@ function test_presets_are_extracted() { $this->assertEquals( $expected, $actual ); } -} \ No newline at end of file +}