Skip to content

Commit

Permalink
Implement new intermediate representation
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Feb 9, 2021
1 parent 2089781 commit 9d6b04e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 37 deletions.
52 changes: 31 additions & 21 deletions lib/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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" } ]
* }
* }
* }
Expand All @@ -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'
* ]
* ]
*
Expand All @@ -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,
Expand Down Expand Up @@ -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 );
Expand Down
40 changes: 24 additions & 16 deletions phpunit/class-wp-theme-json-resolver-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
);

Expand Down

0 comments on commit 9d6b04e

Please sign in to comment.