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

[Blueprints] setSiteLanguage step – download the latest RC translations for Nightly and Beta builds of WordPress #1987

Merged
merged 14 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { StepHandler } from '.';
import { unzipFile } from '@wp-playground/common';
import { logger } from '@php-wasm/logger';
import { versionStringToLoadedWordPressVersion } from '@wp-playground/wordpress';
import { MinifiedWordPressVersions } from '@wp-playground/wordpress-builds';

/**
* @inheritDoc setSiteLanguage
* @hasRunnableExample
Expand All @@ -19,6 +22,40 @@ export interface SetSiteLanguageStep {
language: string;
}

/**
* Returns the URL to download a WordPress translation package.
*
* If the WordPress version doesn't have a translation package,
* the latest "RC" version will be used instead.
*/
const getWordPressTranslationUrl = (wpVersion: string, language: string) => {
/**
* The translation API provides translations for all WordPress releases
* including patch releases.
*
* RC and beta versions don't have individual translation packages.
* They all share the same "RC" translation package.
*
* Nightly versions don't have a "nightly" translation package.
* So, the best we can do is download the RC translation package,
* because it contains the latest available translations.
*
* The WordPress.org translation API uses "RC" instead of
* "RC1", "RC2", "BETA1", "BETA2", etc.
*
* For example translations for WordPress 6.6-BETA1 or 6.6-RC1 are found under
* https://downloads.wordpress.org/translation/core/6.6-RC/en_GB.zip
*/
const wpVersionString = versionStringToLoadedWordPressVersion(wpVersion);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Note that if wpVersion is "woah-this-is-cool" then wpVersionString will also be "woah-this-is-cool". What would be a sensible fallback here? Perhaps a dedicated version conversion logic would make more sense here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

8cf6394 will default translations to the latest WP version to ensure we can always have a valid URL.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this already uses involved patterns, calling versionStringToLoadedWordPressVersion seem to add complexity. What would this look like without that call?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It would look something like this f1f5842

I like this approach more, relying on versionStringToLoadedWordPressVersion could lead to issues if the function is changed, especially without tests.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Lovely, looking good. Thank you!

if (wpVersionString === 'nightly' || wpVersionString === 'beta') {
wpVersion = MinifiedWordPressVersions['beta'].replace(
/(rc|beta).*$/i,
'RC'
);
}
return `https://downloads.wordpress.org/translation/core/${wpVersion}/${language}.zip`;
};

/**
* Sets the site language and download translations.
*/
Expand All @@ -44,7 +81,7 @@ export const setSiteLanguage: StepHandler<SetSiteLanguageStep> = async (

const translations = [
{
url: `https://downloads.wordpress.org/translation/core/${wpVersion}/${language}.zip`,
url: getWordPressTranslationUrl(wpVersion, language),
type: 'core',
},
];
Expand Down
18 changes: 18 additions & 0 deletions packages/playground/website/playwright/e2e/blueprints.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,21 @@ test('should correctly redirect to a multisite wp-admin url', async ({
await website.goto(`./#${encodedBlueprint}`);
await expect(wordpress.locator('body')).toContainText('General Settings');
});

['latest', 'nightly', 'beta'].forEach((version) => {
test(`should translate WP-admin to Spanish for the ${version} WordPress build`, async ({
website,
wordpress,
}) => {
const blueprint: Blueprint = {
landingPage: '/wp-admin/',
preferredVersions: {
wp: version,
},
steps: [{ step: 'setSiteLanguage', language: 'es_ES' }],
};
const encodedBlueprint = JSON.stringify(blueprint);
await website.goto(`./#${encodedBlueprint}`);
await expect(wordpress.locator('body')).toContainText('Escritorio');
});
});
8 changes: 8 additions & 0 deletions packages/playground/website/playwright/e2e/query-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ test('should not login the user in if the login query parameter is set to no', a
});
});

test('should translate WP-admin to Spanish using the language query parameter', async ({
website,
wordpress,
}) => {
await website.goto('./?language=es_ES&url=/wp-admin/');
await expect(wordpress.locator('body')).toContainText('Escritorio');
});

/**
* There is no reason to remove encoded control characters from the URL.
* For example, the html-api-debugger accepts markup with newlines encoded
Expand Down
20 changes: 20 additions & 0 deletions packages/playground/wordpress/src/version-detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ export async function getLoadedWordPressVersion(
return versionStringToLoadedWordPressVersion(versionString);
}

/**
* Returns a WordPress build version string supported by Playground,
* for a given WordPress version string.
*
* Playground supports the last 4 major.minor versions of WordPress,
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is incorrect, though. There's nothing in this function that limits the returned version to the last 4 major releases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

True, I wanted to explain what supported means with that sentence, but it's actually not relevant.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I removed the mention of support in 0d062ea because it's not relevant.

* the latest beta or RC release (depending on which is newer),
* and nightly releases.
* You can find the full list of supported build versions in
* packages/playground/wordpress-builds/src/wordpress/wp-versions.json
*
* Each released version will be converted to the major.minor format.
* For example 6.6.1 will be converted to 6.6.
*
* Release candidates (RC) and beta releases are converted to "beta".
*
* Nightly releases are converted to "nightly".
*
* @param wpVersionString - A WordPress version string.
* @returns A Playground WordPress build version.
*/
export function versionStringToLoadedWordPressVersion(
wpVersionString: string
): string {
Expand Down
Loading