|
|
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[]; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 > ); |
|
|
|
|
|
|
|
|
delete translations.originals_not_found; |
|
|
|
|
|
return Object.values( 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 ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function validatePageTranslations( page: Page, locale: string ): Promise< void > { |
|
|
|
|
|
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' ); |
|
|
|
|
|
|
|
|
const params = await element.getAttribute( 'data-e2e-string-params' ); |
|
|
|
|
|
|
|
|
let translation = |
|
|
translations?.find( ( entry: Translation ) => entry.original.singular === singular ) |
|
|
?.translations?.[ 0 ]?.translation_0 || null; |
|
|
|
|
|
if ( ! translation ) { |
|
|
translation = ''; |
|
|
} |
|
|
|
|
|
if ( params ) { |
|
|
translation = sprintf( translation, ...JSON.parse( params ) ); |
|
|
} |
|
|
|
|
|
await page.waitForFunction( |
|
|
( { element, translation } ) => { |
|
|
const elementText = ( element as HTMLElement )?.innerText; |
|
|
|
|
|
if ( ! elementText ) { |
|
|
throw new Error( `Element rendered with empty inner text: \n${ element.outerHTML }` ); |
|
|
} |
|
|
|
|
|
if ( ! elementText?.trim().includes( translation?.trim() || '' ) ) { |
|
|
throw new Error( |
|
|
`Element text did not match translation! Expected: "${ translation }" -- Actual: "${ elementText }"` |
|
|
); |
|
|
} |
|
|
|
|
|
return true; |
|
|
}, |
|
|
{ element, translation } |
|
|
); |
|
|
} |
|
|
} |
|
|
|