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

19701 try to come up with a solution where html tags in translations are not reliant on the order #21938

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from

Conversation

vraja-pro
Copy link
Contributor

@vraja-pro vraja-pro commented Dec 31, 2024

Context

  • We want to catch translation errors from breaking our UI.

Summary

This PR can be summarized in the following changelog entry:

  • [@yoast/helpers 0.1.0] Adds helper to catch errors in translations.
  • Implements helper function to catch errors in translations.

Relevant technical choices:

Test instructions

Test instructions for the acceptance test before the PR gets merged

This PR can be acceptance tested by following these steps:

Relevant test scenarios

  • Changes should be tested with the browser console open
  • Changes should be tested on different posts/pages/taxonomies/custom post types/custom taxonomies
  • Changes should be tested on different editors (Default Block/Gutenberg/Classic/Elementor/other)
  • Changes should be tested on different browsers
  • Changes should be tested on multisite

Test instructions for QA when the code is in the RC

  • QA should use the same steps as above.

QA can test this PR by following these steps:

Impact check

This PR affects the following parts of the plugin, which may require extra testing:

UI changes

  • This PR changes the UI in the plugin. I have added the 'UI change' label to this PR.

Other environments

  • This PR also affects Shopify. I have added a changelog entry starting with [shopify-seo], added test instructions for Shopify and attached the Shopify label to this PR.

Documentation

  • I have written documentation for this change. For example, comments in the Relevant technical choices, comments in the code, documentation on Confluence / shared Google Drive / Yoast developer portal, or other.

Quality assurance

  • I have tested this code to the best of my abilities.
  • During testing, I had activated all plugins that Yoast SEO provides integrations for.
  • I have added unit tests to verify the code works as intended.
  • If any part of the code is behind a feature flag, my test instructions also cover cases where the feature flag is switched off.
  • I have written this PR in accordance with my team's definition of done.
  • I have checked that the base branch is correctly set.

Innovation

  • No innovation project is applicable for this PR.
  • This PR falls under an innovation project. I have attached the innovation label.
  • I have added my hours to the WBSO document.

Fixes Try to come up with a solution where HTML tags in translations are not reliant on the order

@vraja-pro vraja-pro added the changelog: non-user-facing Needs to be included in the 'Non-userfacing' category in the changelog label Dec 31, 2024
Copy link

@vraja-pro Please be aware that following packages have been abandoned and are not actively maintained anymore:

Package name Path
@yoast/babel-preset packages/babel-preset
@yoast/components packages/components
@yoast/e2e-tests packages/e2e-tests
@yoast/helpers packages/helpers
@yoast/jest-preset packages/jest-preset
@yoast/style-guide packages/style-guide

Please consider using the other packages instead.

Copy link

@vraja-pro Please be aware that following packages have been abandoned and are not actively maintained anymore:

Package name Path
@yoast/babel-preset packages/babel-preset
@yoast/components packages/components
@yoast/e2e-tests packages/e2e-tests
@yoast/helpers packages/helpers
@yoast/jest-preset packages/jest-preset
@yoast/style-guide packages/style-guide

Please consider using the other packages instead.

Copy link

@vraja-pro Please be aware that following packages have been abandoned and are not actively maintained anymore:

Package name Path
@yoast/babel-preset packages/babel-preset
@yoast/components packages/components
@yoast/e2e-tests packages/e2e-tests
@yoast/helpers packages/helpers
@yoast/jest-preset packages/jest-preset
@yoast/style-guide packages/style-guide

Please consider using the other packages instead.

Copy link

@vraja-pro Please be aware that following packages have been abandoned and are not actively maintained anymore:

Package name Path
@yoast/babel-preset packages/babel-preset
@yoast/components packages/components
@yoast/e2e-tests packages/e2e-tests
@yoast/helpers packages/helpers
@yoast/jest-preset packages/jest-preset
@yoast/style-guide packages/style-guide

Please consider using the other packages instead.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 6f62ddda7c77c1b82f39a56ab18fe791914123d5

Details

  • 5 of 6 (83.33%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.003%) to 54.629%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/js/src/settings/routes/site-basics.js 0 1 0.0%
Totals Coverage Status
Change from base Build 01c6afdf2feaddb2d89d45d0f50dfbce38e34b57: 0.003%
Covered Lines: 30110
Relevant Lines: 55472

💛 - Coveralls

@leonidasmi leonidasmi changed the base branch from trunk to feature/plugin-fixes January 2, 2025 09:55
Copy link
Member

@igorschoester igorschoester left a comment

Choose a reason for hiding this comment

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

CR 🏗️

Edit: I think the changelog entries should be user facing. Maybe not the technical one that is there now. But maybe improvement that translations will now fall back to English?

Copy link
Member

Choose a reason for hiding this comment

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

As you see in the comments you got, the helpers package should not be added to.
The reason for this is that we don't want to maintain a random assortment of helpers in a helper package. If there is a need for a overarching package. You could create a specific one. The smaller and targeted, the less maintenance it needs?

Why did you add it to helpers in the first place?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking it would be easier to use in the rest of the addon/premium

Copy link
Member

Choose a reason for hiding this comment

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

Easier than what?

Copy link
Member

Choose a reason for hiding this comment

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

We also skipped this package when improving Jest. There are missing dev deps there.

Comment on lines +6 to +12
jest.mock( "@wordpress/element", () => ( {
createInterpolateElement: jest.fn(),
} ) );

jest.mock( "@wordpress/i18n", () => ( {
sprintf: jest.fn(),
} ) );
Copy link
Member

Choose a reason for hiding this comment

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

Why mock? That seems to defeat the whole purpose.

Comment on lines +21 to +23
const args = [ "world" ];
const conversionMap = { world: <span>world</span> };
const interpolatedElement = <span>Hello world</span>;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const args = [ "world" ];
const conversionMap = { world: <span>world</span> };
const interpolatedElement = <span>Hello world</span>;
const args = [ "<world />" ];
const conversionMap = { world: <span>world</span> };
const interpolatedElement = <>Hello <span>world</span></>;


expect( sprintf ).toHaveBeenCalledWith( translatedString, ...args );
expect( createInterpolateElement ).toHaveBeenCalledWith( "Hello world", conversionMap );
expect( result ).toBe( interpolatedElement );
Copy link
Member

Choose a reason for hiding this comment

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

Then this would need to be toEqual

Comment on lines +36 to +42
const translatedString = "Hello %s";
const args = [ "world" ];
const conversionMap = { world: <span>world</span> };

sprintf.mockImplementation( () => {
throw new Error( "Test error" );
} );
Copy link
Member

Choose a reason for hiding this comment

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

Why mock? We have the perfect use case already, as per the issue.

Suggested change
const translatedString = "Hello %s";
const args = [ "world" ];
const conversionMap = { world: <span>world</span> };
sprintf.mockImplementation( () => {
throw new Error( "Test error" );
} );
const translatedString = "Hello %2$sworld%1$s";
const args = [ "<world>", "</world>" ];
const conversionMap = { world: <span>world</span> };

* Create an interpolated element from a translated string and catches errors.
*
* @param {string} translatedString The translated string to be interpolated.
* @param {[string]} args The arguments to be interpolated into the translated string.
Copy link
Member

Choose a reason for hiding this comment

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

sprintf does not need to be string only. You can use numbers and the like with %d.

Links:

*
* @returns {object} The interpolated element.
*/
export const i18nCreateInterpolateElement = ( translatedString, args, conversionMap ) => {
Copy link
Member

Choose a reason for hiding this comment

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

I think the variable naming can be improved here.
Also, the needing to write args in array form is a bit meh?

export const i18nCreateInterpolateElement = ( translatedString, args, conversionMap ) => {
try {
return createInterpolateElement(
sprintf(
Copy link
Member

Choose a reason for hiding this comment

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

You changed this to include sprintf.
While making the fallback easier, it does add complexity with doing two things.
This means we no longer have the option to not sprintf when using this safer translation.
Also, if possible in the future, we could just add the HTML in the translation string? I'm not sure why we always use sprintf now. Did you investigate this?

Can you explain a bit your reasoning and thoughts here? Or rather in the issue tech choices? 😬
I would love some more details on your choices.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The breaking error is caused when using sprintf.

Copy link
Member

Choose a reason for hiding this comment

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

I highly doubt that, since the source includes a try/catch around it: https://github.com/WordPress/gutenberg/blob/trunk/packages/i18n/src/sprintf.js

@@ -23,6 +23,7 @@
"license": "GPL-3.0",
"private": false,
"dependencies": {
"@wordpress/element": "^6.14.0",
Copy link
Member

Choose a reason for hiding this comment

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

Why this version?

Our lowest support WP version is 6.5:
https://github.com/WordPress/wordpress-develop/blob/6.5.0/package.json#L113C3-L113C31

@igorschoester igorschoester changed the base branch from feature/plugin-fixes to trunk January 7, 2025 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: non-user-facing Needs to be included in the 'Non-userfacing' category in the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Try to come up with a solution where HTML tags in translations are not reliant on the order
3 participants