Skip to content

Commit

Permalink
[Blueprints] setSiteLanguage step – download the latest RC translatio…
Browse files Browse the repository at this point in the history
…ns for Nightly and Beta builds of WordPress (WordPress#1987)

This PR enables using `setSiteLanguage` with beta and nightly WordPress
versions:

```json
{
	"preferredVersions": {
		"php": "8.0",
		"wp": "beta"
	},
	"steps": [
		{
			"step": "setSiteLanguage",
			"language": "en_GB"
		}
	]
}
```

Before this PR, the above Blueprint would fetch translations from
`https://downloads.wordpress.org/translation/core/6.7-RC2/es_ES.zip` and
fail after a 404 API response from the WordPress.org translations API.
The `6.7-RC2` version string came directly from WordPress's version.php.

However, the WordPress translations API only offers translations for
released versions of WordPress (e.g. `6.6`, `6.6.1`)
and one RC translation for each minor version (e.g. `6.6-RC`, `6.7-RC`).
Beta, and RC versions of WordPress all use a single RC translation
version. Development, versions don't have a translation version, so the
best we can do is download the latest RC translations.

This PR transforms the raw version string, such as `6.7-RC2` or
`6.8-alpha-59341`, into one recognized by the API, such as `6.7-RC` or
`6.8-RC`. As a result, the above Blueprint now fetches the translations
from
`https://downloads.wordpress.org/translation/core/6.7-RC/es_ES.zip`.

## Testing Instructions (or ideally a Blueprint)

- CI

### Manual testing instructions

- Open Nightly in Spanish
http://127.0.0.1:5400/website-server/?wp=nightly&language=es_ES&url=%2Fwp-admin%2F
- Confirm WP-admin is in Spanish
- Open Beta in Spanish
http://127.0.0.1:5400/website-server/?wp=beta&language=es_ES&url=%2Fwp-admin%2F
- Confirm WP-admin is in Spanish
- Open 6.5 in Spanish
http://127.0.0.1:5400/website-server/?wp=6.5&language=es_ES&url=%2Fwp-admin%2F
- Confirm WP-admin is in Spanish

---------

Co-authored-by: Adam Zieliński <[email protected]>
  • Loading branch information
bgrgicak and adamziel authored Nov 19, 2024
1 parent 3f0ccf3 commit 3a3f2b6
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { MinifiedWordPressVersions } from '@wp-playground/wordpress-builds';
import { getWordPressTranslationUrl } from './set-site-language';

describe('getTranslationUrl()', () => {
it('should return a major.minor translation URL for a major.minor version', () => {
expect(getWordPressTranslationUrl('6.6', 'en_US')).toBe(
'https://downloads.wordpress.org/translation/core/6.6/en_US.zip'
);
});

it('should return a major.minor.patch translation URL for a major.minor.patch version', () => {
expect(getWordPressTranslationUrl('6.5.1', 'es_ES')).toBe(
'https://downloads.wordpress.org/translation/core/6.5.1/es_ES.zip'
);
});

[
{
version: '6.6-RC1',
description:
'should return the latest RC translation URL for a RC version',
},
{
version: '6.6-beta2',
description:
'should return the latest RC translation URL for a beta version',
},
{
version: '6.6-nightly',
description:
'should return the latest RC translation URL for a nightly version',
},
{
version: '6.8-alpha-59408',
description:
'should return the latest RC translation URL for an alpha version',
},
].forEach(({ version, description }) => {
it(description, () => {
const latestBetaVersion =
MinifiedWordPressVersions['beta'].split('-')[0];
expect(getWordPressTranslationUrl(version, 'en_US')).toBe(
`https://downloads.wordpress.org/translation/core/${latestBetaVersion}-RC/en_US.zip`
);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { StepHandler } from '.';
import { unzipFile } from '@wp-playground/common';
import { logger } from '@php-wasm/logger';
import {
LatestMinifiedWordPressVersion,
MinifiedWordPressVersions,
} from '@wp-playground/wordpress-builds';

/**
* @inheritDoc setSiteLanguage
* @hasRunnableExample
Expand All @@ -19,6 +24,48 @@ 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.
*/
export 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
*/
if (wpVersion.match(/(\d.\d(.\d)?)-(alpha|beta|nightly|rc).*$/i)) {
wpVersion = MinifiedWordPressVersions['beta'].replace(
/(rc|beta).*$/i,
'RC'
);
} else if (!wpVersion.match(/^(\d+\.\d+)(?:\.\d+)?$/)) {
/**
* If the WordPress version string isn't a major.minor or major.minor.patch,
* the latest available WordPress build version will be used instead.
*/
wpVersion = LatestMinifiedWordPressVersion;
}
return `https://downloads.wordpress.org/translation/core/${wpVersion}/${language}.zip`;
};

/**
* Sets the site language and download translations.
*/
Expand All @@ -44,7 +91,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
16 changes: 16 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,22 @@ export async function getLoadedWordPressVersion(
return versionStringToLoadedWordPressVersion(versionString);
}

/**
* Returns a WordPress build version string, for a given WordPress version string.
*
* You can find the full list of supported build version strings 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

0 comments on commit 3a3f2b6

Please sign in to comment.