Skip to content

Commit

Permalink
Add basic test for i18n support for custom templates in theme.json
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Mar 15, 2021
1 parent bb36ee8 commit 1747748
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 7 deletions.
26 changes: 25 additions & 1 deletion lib/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,35 @@ public static function get_fields_to_translate() {
* @return array Returns the modified $theme_json_structure.
*/
private static function translate( $theme_json, $domain = 'default' ) {
$fields = self::get_fields_to_translate();
foreach ( $fields as $field ) {
$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 ) {
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, $path, $array_to_translate );
}
}

if ( ! isset( $theme_json['settings'] ) ) {
return $theme_json;
}

$fields = self::get_fields_to_translate();
foreach ( $theme_json['settings'] as $setting_key => $settings ) {
if ( empty( $settings ) ) {
continue;
Expand Down
63 changes: 63 additions & 0 deletions phpunit/class-wp-theme-json-resolver-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,38 @@

class WP_Theme_JSON_Resolver_Test extends WP_UnitTestCase {

function setUp() {
parent::setUp();
$this->theme_root = realpath( __DIR__ . '/data/themedir1' );

$this->orig_theme_dir = $GLOBALS['wp_theme_directories'];

// /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
$GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );

add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );
// Clear caches.
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
}

function tearDown() {
$GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
parent::tearDown();
}

function filter_set_theme_root() {
return $this->theme_root;
}

function filter_set_locale_to_polish() {
return 'pl_PL';
}

function test_fields_are_extracted() {
$actual = WP_Theme_JSON_Resolver::get_fields_to_translate();

Expand Down Expand Up @@ -61,4 +93,35 @@ function test_fields_are_extracted() {

$this->assertEquals( $expected, $actual );
}

function test_translations_are_applied() {
add_filter( 'locale', array( $this, 'filter_set_locale_to_polish' ) );
load_textdomain( 'fse', realpath( __DIR__ . '/data/languages/themes/fse-pl_PL.mo' ) );

switch_theme( 'fse' );

$actual = WP_Theme_JSON_Resolver::get_theme_data();

unload_textdomain( 'fse' );
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(),
array(
'page-home' => array(
'title' => 'Szablon strony głównej',
),
)
);
$this->assertSame(
$actual->get_template_parts(),
array(
'small-header' => array(
'area' => 'header',
),
)
);
}
}
12 changes: 6 additions & 6 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ function test_get_custom_templates() {
'customTemplates' => array(
array(
'name' => 'page-home',
'title' => 'Some title',
'title' => 'Homepage template',
),
),
)
Expand All @@ -827,7 +827,7 @@ function test_get_custom_templates() {
$page_templates,
array(
'page-home' => array(
'title' => 'Some title',
'title' => 'Homepage template',
),
)
);
Expand All @@ -838,8 +838,8 @@ function test_get_template_parts() {
array(
'templateParts' => array(
array(
'name' => 'header',
'area' => 'Some area',
'name' => 'small-header',
'area' => 'header',
),
),
)
Expand All @@ -850,8 +850,8 @@ function test_get_template_parts() {
$this->assertEqualSetsWithIndex(
$template_parts,
array(
'header' => array(
'area' => 'Some area',
'small-header' => array(
'area' => 'header',
),
)
);
Expand Down
Binary file added phpunit/data/languages/themes/fse-pl_PL.mo
Binary file not shown.
23 changes: 23 additions & 0 deletions phpunit/data/languages/themes/fse-pl_PL.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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"
"Language: pl_PL\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.4.2\n"
"X-Poedit-Basepath: .\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Poedit-KeywordsList: __;_e;_x:1,2c;_ex:1,2c;_n:1,2;_nx:1,2,4c;_n_noop:1,2;"
"_nx_noop:1,2,3c;esc_attr__;esc_html__;esc_attr_e;esc_html_e;esc_attr_x:1,2c;"
"esc_html_x:1,2c\n"
"X-Textdomain-Support: yes\n"
"Last-Translator: \n"
"Language-Team: \n"
"X-Poedit-SearchPath-0: .\n"

msgctxt "Custom template name"
msgid "Homepage template"
msgstr "Szablon strony głównej"
17 changes: 17 additions & 0 deletions phpunit/data/themedir1/fse/experimental-theme.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"settings": {
"root": {}
},
"customTemplates": [
{
"name": "page-home",
"title": "Homepage template"
}
],
"templateParts": [
{
"name": "small-header",
"area": "header"
}
]
}
7 changes: 7 additions & 0 deletions phpunit/data/themedir1/fse/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
Theme Name: FSE Theme
Theme URI: https://wordpress.org/
Description: For testing purposes only.
Version: 1.0.0
Text Domain: fse
*/

0 comments on commit 1747748

Please sign in to comment.