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

Show translated strings coming from global styles variations #39960

Merged
merged 3 commits into from
Apr 1, 2022

Conversation

oandregal
Copy link
Member

@oandregal oandregal commented Mar 31, 2022

Follow-up to #39322

What?

This PR shows the translated strings of the global styles variations according to the language of the user.

Why?

Style variations can contain strings that are shown to the user, such as the style variation title or the names of the presets (colors, font sizes, etc).

How?

  • Centralizes the logic to read variations into WP_Theme_JSON_Resolver::get_style_variations.
  • Makes them use the translate method we use for any other theme.json file.

Testing Instructions

I've created a couple of style variations for the TwentyTwentyTwo theme and each contains the following translatable strings 1) the variation title and 2) one color in the color palette with a name. This is the result (I've set the screen to Spanish and so the expectation is that the variation title and the color defined by it should be translated):

style-variations-translated.mp4

Step by step, this is what I've done:

1 - Create the variations

  • Activate TwentyTwentyTwo.
  • Within the theme folder, create a file at styles/redish.json with the following contents:
{
  "version": 2,
  "title": "Red style",
  "styles": {
    "color": {
      "background": "red",
      "text": "white"
    }
  },
  "settings": {
    "color": {
      "palette": [
        { "slug": "red", "name": "Red", "color": "red" }
      ]
    }
  }
}
  • Within the theme folder, create a file at styles/bluish.json with the following contents:
{
  "version": 2,
  "title": "Blue style",
  "styles": {
    "color": {
      "background": "blue",
      "text": "white"
    }
  },
  "settings": {
    "color": {
      "palette": [
        { "slug": "blue", "name": "Blue", "color": "blue" }
      ]
    }
  }
}

2 - Provide a translation

Unzip the .mo file I've created for TwentyTwentyTwo twentytwentytwo-es_ES.mo.zip at wp-content/languages/themes/. This provides the strings of the style variations translated to Spanish.

Alternatively, you can do this yourself by (expand to see how).
  • Go to "Settings > General" and set the "Site Language" to Spanish.
  • Go to "Dashboard > Updates" and make sure you've updated the translations (both for the site and the active theme).
  • Go to wp-content/languages/themes/ and paste the following at the end of the twentytwentytwo-es_ES.po file:
#: styles/bluish.json
msgctxt "Style variation name"
msgid "Blue style"
msgstr "Estilo azul"

#: styles/bluish.json
msgctxt "Color name"
msgid "Blue"
msgstr "Azul"

#: styles/redish.json
msgctxt "Style variation name"
msgid "Red style"
msgstr "Estilo rojo"

#: styles/redish.json
msgctxt "Color name"
msgid "Red"
msgstr "Rojo"
  • Run the wp i18n make-mo <path-to-your-po-file> <path-to-the-directory> command to convert the .po file into the .mo one in the same directory.

3 - Do the testing

  • Check that the variation title is translated accordingly to the site language (switch between English and Spanish).
  • Check that the color names for the variations are translated accordingly to the site language (switch between English and Spanish).

@oandregal oandregal self-assigned this Mar 31, 2022
@oandregal oandregal added [Status] In Progress Tracking issues with work in progress Internationalization (i18n) Issues or PRs related to internationalization efforts Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json labels Mar 31, 2022
@oandregal oandregal force-pushed the update/translate-global-styles-variations branch from 20fd0ad to ba83d4e Compare March 31, 2022 17:19
Note this needs the code to be ported to 6.0 to make sure
we still load the schema file when the minimum WordPress version
for the plugin is 5.9.
@oandregal oandregal force-pushed the update/translate-global-styles-variations branch from ba83d4e to b94824f Compare April 1, 2022 12:21
foreach ( $nested_html_files as $path => $file ) {
$decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) );
if ( is_array( $decoded_file ) ) {
$translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) );
Copy link
Member Author

@oandregal oandregal Apr 1, 2022

Choose a reason for hiding this comment

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

The key point of this PR is this line: passing the decode JSON to translate. The rest of the code remains the same as it was.

Copy link
Member Author

Choose a reason for hiding this comment

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

get_style_variations had to be moved here to use translate, which is not a public method of the WP_Theme_JSON_Resolver_Gutenberg class.

Alternatively, we could make translate public and use it in the rest API. Though I think this approach is better because it encapsulates all the logic for consumers: they just want to read the style variations.

* Default 'default'.
* @return array Returns the modified $theme_json_structure.
*/
protected static function translate( $theme_json, $domain = 'default' ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This method is not modified with respect to WP_Theme_JSON_Resolver_5_9::translate, though it needs to live here to read the theme-i18n.json from the lib/compat/6.0 folder.

@oandregal oandregal removed the [Status] In Progress Tracking issues with work in progress label Apr 1, 2022
@oandregal oandregal marked this pull request as ready for review April 1, 2022 13:03
@oandregal oandregal requested review from youknowriad, a team and getdave and removed request for a team April 1, 2022 13:07
Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

Makes sense

@oandregal oandregal merged commit e238e65 into trunk Apr 1, 2022
@oandregal oandregal deleted the update/translate-global-styles-variations branch April 1, 2022 13:40
@github-actions github-actions bot added this to the Gutenberg 13.0 milestone Apr 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json Internationalization (i18n) Issues or PRs related to internationalization efforts
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants