settings->get(); if ( ! $settings->is_language( $lang ) || $settings->default_code() === $lang ) { return new WP_Error( 'blocklingo_bad_lang', __( 'Unknown language.', 'blocklingo' ) ); } $filter = array( 'lang' => $lang, 'status' => 'untranslated', 'domain' => $domain, ); $page = $this->catalog->query( $filter + array( 'per_page' => max( 1, min( 100, $limit ) ) ) ); if ( ! $page['items'] ) { return array( 'done' => 0, 'remaining' => 0, ); } $texts = array(); $kinds = array(); $hints = array(); foreach ( $page['items'] as $i => $row ) { $texts[ $i ] = (string) $row->original; $kinds[ $i ] = preg_match( '/<[a-z\/!]/i', $texts[ $i ] ) ? 'html' : 'text'; $hints[ $i ] = trim( 'UI string' . ( $row->context ? ', context: ' . $row->context : '' ) . ( $row->domain ? ', from ' . $row->domain : '' ) ); } $out = $this->translator->translate_strings( $texts, $kinds, $settings->default_code(), $lang, null, array( 'hints' => $hints, 'context' => array( 'title' => 'Website interface strings', 'type' => 'buttons, labels, notices and messages of the theme and plugins; keep them short and keep placeholders like %s or %1$s', ), ) ); if ( is_wp_error( $out ) ) { return $out; } $done = 0; foreach ( $page['items'] as $i => $row ) { if ( isset( $out[ $i ] ) && '' !== trim( $out[ $i ] ) ) { $this->catalog->set_translation( (int) $row->id, $lang, wp_kses_post( trim( $out[ $i ] ) ) ); // Same rule as saving by hand. ++$done; } } $left = $this->catalog->query( $filter + array( 'per_page' => 1 ) ); return array( 'done' => $done, 'remaining' => $left['total'], ); } /** * Delete untranslated strings the current rules would not collect (core, * editor metadata). Runs before each "Scan site". * * @return int Strings deleted. */ public function prune(): int { $ids = array(); foreach ( $this->catalog->untranslated() as $r ) { if ( ! $this->rules->collectable( (string) $r->original, (string) $r->domain, (string) $r->context ) ) { $ids[] = (int) $r->id; } } return $ids ? $this->catalog->delete( $ids ) : 0; } }