Skip to content

Commit

Permalink
Refactor translate implementation in theme.json resolver class
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Mar 15, 2021
1 parent 1747748 commit b416996
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 56 deletions.
86 changes: 39 additions & 47 deletions lib/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 );
}
}

Expand Down
20 changes: 13 additions & 7 deletions phpunit/class-wp-theme-json-resolver-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
)
);
Expand Down
Binary file modified phpunit/data/languages/themes/fse-pl_PL.mo
Binary file not shown.
10 changes: 9 additions & 1 deletion phpunit/data/languages/themes/fse-pl_PL.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
18 changes: 17 additions & 1 deletion phpunit/data/themedir1/fse/experimental-theme.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
{
"settings": {
"root": {}
"root": {
"color": {
"palette": [
{
"slug": "light",
"name": "Light",
"color": "#f5f7f9"
},
{
"slug": "dark",
"name": "Dark",
"color": "#000"
}
],
"custom": false
}
}
},
"customTemplates": [
{
Expand Down

0 comments on commit b416996

Please sign in to comment.