Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add theme.json i18n mechanism and JSON file specifying which theme.json paths are translatable #27380

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions lib/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,85 @@ private static function get_from_file( $file_path ) {
return $config;
}

/**
* Processes a tree from i18n-theme.json into a linear array
* containing the a translatable path from theme.json and an array
* of properties that are translatable.
*
* @param array $file_structure_partial A part of a theme.json i18n tree.
* @param array $current_path An array with a path on the theme.json i18n tree.
*
* @return array An array of arrays each one containing a translatable path and an array of properties that are translatable.
*/
private static function theme_json_i18_file_structure_to_preset_paths( $file_structure_partial, $current_path = array() ) {
$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,
),
);
}
$result = array_merge(
$result,
self::theme_json_i18_file_structure_to_preset_paths( $partial_child, array_merge( $current_path, array( $property ) ) )
);
}
return $result;
}

/**
* Returns a data structure used in theme.json translation.
*
* @return array An array of theme.json paths that are translatable and the keys that are translatable
*/
private 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' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the plan to always keep a local copy of the file with the schema for translations? It usually gives us more flexibility when there is a hook introduced upfront. This way the filters let to add only new keys that need to be translated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the plan to always keep a local copy of the file with the schema for translations?

Yes, the plan is for the file to always exist locally.

It usually gives us more flexibility when there is a hook introduced upfront. This way the filters let to add only new keys that need to be translated.

If we had a hook that allowed to add new keys to be translatable, wp-cli would not be aware of the hook and would not extract the strings anyway, so the strings would not be translatable anyway.

The keys that are translatable are something static like in https://github.com/WordPress/wordpress-develop/blob/d2c8fae049e80f5726653c61361d467fad1a8a0b/src/wp-admin/includes/plugin.php#L149-L194 a plugin can not add other $plugin_data keys to be translatable.

If a plugin wants to inject translatable information in the theme.json structure, the plugin would use the filters that allow to change the structure and do something like $theme_json['settings'][mycustomPreset]= array( array( 'customPorperty' => __( 'translatable string' ) ) ); Given that it is done in code the string extraction would work as expected. So it would be possible for a plugin to inject translatable information using code not requiring to filter this file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure that some sort of filter is going to be necessary. Once you have the definition of translatable fields in the WordPress core, you might end up willing to expand it in Gutenberg when a new section is added to theme.json file. You can always simply override experimental-i18n-theme.json in Gutenberg and call it the day, but we should account for that when backporting logic to the WP core 😄

$theme_json_i18n = self::theme_json_i18_file_structure_to_preset_paths( $file_structure );

}
return $theme_json_i18n;
}

/**
* Translates a theme.json structure.
*
* @param array $theme_json_structure A theme.json structure that is going to be translatable.
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
* Default 'default'.
*/
private static function translate_presets( &$theme_json_structure, $domain = 'default' ) {
$preset_to_translate = self::get_presets_to_translate();
foreach ( $theme_json_structure as &$context_value ) {
if ( empty( $context_value ) ) {
continue;
}
foreach ( $preset_to_translate as $preset ) {
$path = $preset['path'];
$translatable_keys = $preset['translatable_keys'];
$array_to_translate = gutenberg_experimental_get( $context_value, $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.NonSingularStringLiteralDomain
$item_to_translate[ $translatable_key ] = translate( $item_to_translate[ $translatable_key ], $domain );
oandregal marked this conversation as resolved.
Show resolved Hide resolved
// phpcs:enable
}
}
gutenberg_experimental_set( $context_value, $path, $array_to_translate );
}
}
}

/**
* Return core's origin config.
*
Expand All @@ -82,6 +161,7 @@ private static function get_core_origin() {
}

$config = self::get_from_file( __DIR__ . '/experimental-default-theme.json' );
self::translate_presets( $config );

// Start i18n logic to remove when JSON i18 strings are extracted.
$default_colors_i18n = array(
Expand Down Expand Up @@ -155,6 +235,7 @@ private static function get_core_origin() {
*/
private function get_theme_origin( $theme_support_data = array() ) {
$theme_json_data = self::get_from_file( locate_template( 'experimental-theme.json' ) );
self::translate_presets( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );

/*
* We want the presets and settings declared in theme.json
Expand Down
32 changes: 32 additions & 0 deletions lib/experimental-i18n-theme.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
oandregal marked this conversation as resolved.
Show resolved Hide resolved
"settings": {
"typography": {
"fontSizes": [
"name"
],
"fontStyles": [
"name"
],
"fontWeights": [
"name"
],
"fontFamilies": [
"name"
],
"textTransforms": [
"name"
],
"textDecorations": [
"name"
]
},
"color": {
"palette": [
"name"
],
"gradients": [
"name"
]
}
}
}