import { sprintf } from '@wordpress/i18n'; import { Page, ElementHandle } from 'playwright'; const GLOTPRESS_ORIGINALS_ENDPOINT = 'https://translate.wordpress.com/api/translations/-query-by-originals'; const GLOTPRESS_PROJECT = 'wpcom'; const selectors = { translatableElement: '[data-e2e-string]', }; interface OriginalString { singular: string | null; plural?: string; context?: string; } interface Translation { original: OriginalString; translations: { translation_0: string; }[]; } interface TranslationsResponse { [ key: number ]: Translation; originals_not_found?: OriginalString[]; } /** * Fetch translations for originals. */ async function fetchTranslations( originals: OriginalString[], locale: string ): Promise< Translation[] | null > { const payload = new URLSearchParams( { project: GLOTPRESS_PROJECT, locale_slug: locale, original_strings: JSON.stringify( originals ), } ); const translations = await fetch( GLOTPRESS_ORIGINALS_ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, body: payload.toString(), } ).then( ( response ) => response.json() as Promise< TranslationsResponse > ); // Not found originals are not needed in the payload. delete translations.originals_not_found; return Object.values( translations ); } /** * Get elements translations. */ async function getElementsTranslations( elements: ElementHandle[], locale: string ): Promise< Translation[] | null > { const originals: OriginalString[] = await Promise.all( elements.map( async ( element ) => ( { singular: await element.getAttribute( 'data-e2e-string' ), } ) ) ); return fetchTranslations( originals, locale ); } /** * Validate translations on current page. * * @param {Page} page The underlying page */ export async function validatePageTranslations( page: Page, locale: string ): Promise< void > { // Default locale doesn't have translations. if ( locale === 'en' ) { return; } const translatableElements = await page.$$( selectors.translatableElement ); const translations = await getElementsTranslations( translatableElements, locale ); for ( const element of translatableElements ) { const singular = await element.getAttribute( 'data-e2e-string' ); // In order to test translations with placeholders, parameters must be passed as JSON encoded string. // For example, `