Click here now

* becomes the template `Click here now`. Inline elements are * reduced to `` placeholders so attributes are never sent for * translation, cost nothing, and can't be mangled. Translating whole * sentences (instead of loose text nodes) is what gives natural grammar. * - text: a standalone text node. * - attr: a translatable attribute (alt, title, placeholder, meta description…). * * Each template is hashed and looked up in the local translation memory; only * unknown segments go to Langferry Cloud. If a provider returns a block with * broken placeholders, that block falls back to per-text-node segments. * * @package Langferry */ namespace Langferry; defined( 'ABSPATH' ) || exit; final class Translator { const ELEMENT = 1; const TEXT = 3; const COMMENT = 8; /** Never translated, never descended into. */ const SKIP = array( 'script' => 1, 'style' => 1, 'noscript' => 1, 'template' => 1, 'code' => 1, 'pre' => 1, 'textarea' => 1, 'svg' => 1, 'math' => 1, 'iframe' => 1, 'object' => 1, 'embed' => 1, 'canvas' => 1, 'kbd' => 1, 'samp' => 1, 'var' => 1, 'audio' => 1, 'video' => 1, 'base' => 1, 'link' => 1, ); /** Phrasing elements that may live inside a translated sentence. */ const INLINE = array( 'a' => 1, 'abbr' => 1, 'b' => 1, 'bdi' => 1, 'bdo' => 1, 'cite' => 1, 'data' => 1, 'dfn' => 1, 'em' => 1, 'i' => 1, 'mark' => 1, 'q' => 1, 's' => 1, 'small' => 1, 'span' => 1, 'strong' => 1, 'sub' => 1, 'sup' => 1, 'time' => 1, 'u' => 1, 'del' => 1, 'ins' => 1, 'font' => 1, 'big' => 1, 'tt' => 1, 'acronym' => 1, 'label' => 1, 'button' => 1, 'output' => 1, 'meter' => 1, 'picture' => 1, ); /** Inline elements kept verbatim as placeholders (no closing content sent). */ const VOID_INLINE = array( 'br' => 1, 'img' => 1, 'wbr' => 1, 'input' => 1, 'source' => 1, ); const TRANSLATABLE_ATTRS = array( 'title', 'alt', 'placeholder', 'aria-label' ); const META_CONTENT = array( 'description' => 1, 'og:title' => 1, 'og:description' => 1, 'og:image:alt' => 1, 'twitter:title' => 1, 'twitter:description' => 1, 'twitter:image:alt' => 1, ); /** Safety limits per page. */ const MAX_SEGMENTS = 6000; /** * Longest segment (in characters) the cloud accepts: it refuses a batch holding a longer one. * Such a segment is left in the original language and never sent. */ const MAX_SEGMENT_CHARS = 10000; /** @var string */ private $source; /** @var string */ private $target; /** @var array */ private $config; /** @var Html */ private $html; /** @var \SplObjectStorage Nodes excluded by selector. */ private $excluded; /** @var \SplObjectStorage memo for inline_only(). */ private $inline_memo; /** @var array */ private $segments = array(); /** @var array */ private $stats = array( 'segments' => 0, 'translated' => 0, 'chars' => 0, 'translated_chars' => 0, 'cloud_calls' => 0, 'fallbacks' => 0, 'pending' => 0, // Segments with no translation after this render, oversized ones not counted. 'oversized' => 0, // Segments over MAX_SEGMENT_CHARS: left untranslated, never sent. 'error' => null, 'new_text' => 'sent', // sent | cache_only (see Traffic) ); /** @var bool Whether text not in the translation memory may be sent to the cloud. */ private $allow_new = true; /** @var string page | email: what the text is for (Langferry Cloud translates email text on some plans). */ private $purpose = 'page'; /** @var bool Personal data: kept out of the cloud's shared cache and expired here (see Privacy). */ private $private = false; /** @var string Who asked for this page (Traffic): person | search_engine | pretranslate. */ private $requester = 'person'; /** @var bool Pre-translation of a main page: its request's signed value says m (Fetch_Signature, ruling R66). */ private $main = false; /** * Transients set by a paced answer from the cloud, one per requester (docs/qa/scenarios/plugin-traffic.md, * ruling R67 as amended): people's stops everyone; search engines' stops only them; pre-translation's * is kept per tier, a main page's (main: true) apart from the other pages', so a paced answer for the * deep pages never holds back the main pages the cloud still serves. 2.8.0's shared * langferry_paced_crawlers is read by no one and expires on its own. */ const PACED_PEOPLE = 'langferry_paced_people'; const PACED_SEARCH = 'langferry_paced_search'; const PACED_PRETRANSLATE = 'langferry_paced_pretranslate'; const PACED_PRETRANSLATE_MAIN = 'langferry_paced_pretranslate_main'; const PLAN_PAUSE = 'langferry_not_in_plan_'; public function __construct( $source, $target, array $config ) { $this->source = $source; $this->target = $target; $this->config = $config; } /** * Translate a parsed document in place. * * @param Html $html Parsed document. * @param string $url Request path, stored with new strings for editor context. * @return array Stats (segments, translated, chars, translated_chars, error…). */ /** * Cache only: translations already in the memory are applied, new text stays as it is. */ public function allow_new_text( $allow, $requester = 'person' ) { $this->allow_new = (bool) $allow; $this->stats['new_text'] = $this->allow_new ? 'sent' : 'cache_only'; $this->requester = in_array( $requester, array( 'person', 'search_engine', 'pretranslate' ), true ) ? $requester : 'person'; // The cloud lets a main page's pre-translation use the part of the budget kept for people (ruling // R66): only pre-translation, and only when this request's signed value says it is a main page. $this->main = 'pretranslate' === $this->requester && Fetch_Signature::request_is_main(); } private function is_crawler() { return 'person' !== $this->requester; } /** * The paced transient of this render's requester (ruling R67 as amended): people's, search engines', * or pre-translation's for its tier. */ private function paced_transient() { if ( 'pretranslate' === $this->requester ) { return $this->main ? self::PACED_PRETRANSLATE_MAIN : self::PACED_PRETRANSLATE; } return 'search_engine' === $this->requester ? self::PACED_SEARCH : self::PACED_PEOPLE; } public function for_purpose( $purpose ) { $this->purpose = 'email' === $purpose ? 'email' : 'page'; if ( 'email' === $this->purpose ) { $this->private = true; // order emails carry names, addresses and order details } } public function mark_private() { $this->private = true; } /** * Forget "not in your plan" answers, e.g. after a plan change. */ public static function clear_plan_pauses() { delete_transient( self::PLAN_PAUSE . 'email' ); delete_transient( self::PLAN_PAUSE . 'slug' ); } public function translate( Html $html, $url = '' ) { $this->html = $html; $this->excluded = new \SplObjectStorage(); $this->inline_memo = new \SplObjectStorage(); $selectors = array_merge( array( '#wpadminbar', '.notranslate', '.lf-notranslate', '[translate=no]', '[data-no-translation]' ), (array) $this->config['exclusions']['selectors'] ); foreach ( $selectors as $selector ) { foreach ( $html->select( $selector ) as $node ) { $this->excluded->offsetSet( $node ); // attach() is deprecated in PHP 8.5 } } $root = $html->root(); if ( ! $root ) { return $this->stats; } $this->translate_templates( $url ); $this->collect_attributes( $root ); $this->walk( $root ); if ( empty( $this->segments ) ) { return $this->stats; } $translations = $this->resolve( $this->segments, $url ); // Attributes first: block restoration clones inline elements, and the // clones must carry the already-translated attributes. $failed_blocks = array(); foreach ( array( 'attr', 'text', 'block' ) as $type ) { foreach ( $this->segments as $segment ) { if ( $segment['type'] !== $type ) { continue; } $translation = $translations[ $segment['source'] ] ?? null; if ( null === $translation ) { $this->count_untranslated( $segment ); continue; } if ( ! $this->apply( $segment, $translation ) ) { $failed_blocks[] = $segment; } else { ++$this->stats['translated']; $this->stats['translated_chars'] += $segment['len']; } } } if ( $failed_blocks ) { $this->fallback( $failed_blocks, $url ); } return $this->stats; } /* ------------------------------------------------------------------ */ /* Collection */ /* ------------------------------------------------------------------ */ private function is_excluded( $el ) { if ( $this->excluded->offsetExists( $el ) ) { // contains() is deprecated in PHP 8.5 return true; } if ( 'no' === strtolower( (string) $el->getAttribute( 'translate' ) ) ) { return true; } return false; } /** * Translate what's inside