Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MintCxserverWebService.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\WebService;
5
14 private const WIKITEXT_REGEX = [
15 '/\b(PLURAL|GRAMMAR|GENDER)\b/',
16 '/==\s*([^=]+)\s*==/', // heading
17 "/''.+?''/", // italics & bold
18 '/\[\[([^\[\]]+)\]\]/', // links
19 '/\{\{([^{}]+)\}\}/', // templates
20 ];
21 private const EXCLUDED_TARGET_LANGUAGES = [ 'zh' ];
22
23 protected function handlePairsForService( array $response ): array {
24 $pairs = [];
25 foreach ( $response[$this->getServiceName()] as $source => $targets ) {
26 $filteredTargets = array_diff( $targets, self::EXCLUDED_TARGET_LANGUAGES );
27 foreach ( $filteredTargets as $target ) {
28 $pairs[$source][$target] = true;
29 }
30 }
31
32 return $pairs;
33 }
34
35 protected function getServiceName(): string {
36 return 'MinT';
37 }
38
39 protected function handleServiceResponse( array $responseBody ): string {
40 return trim( $this->unwrapUntranslatable( $responseBody[ 'contents' ] ) );
41 }
42
43 protected function wrapUntranslatable( string $text ): string {
44 // Check if at least one instance of the patterns exists in the source string
45 foreach ( self::WIKITEXT_REGEX as $pattern ) {
46 if ( preg_match( $pattern, $text ) ) {
48 'Wikitext instance(s) in source string. See T349375'
49 );
50 }
51 }
52
53 return $text;
54 }
55}
Used for interacting with translation services supported by Cxserver.
Implements support for MinT translation service via the Cxserver API.
wrapUntranslatable(string $text)
Some mangling that tries to keep some parts of the message unmangled by the translation service.
Used to signal that the requested input is rejected and cannot be used with an external web service.
unwrapUntranslatable(string $text)
Undo the hopyfully untouched mangling done by wrapUntranslatable.