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 int $wikitextCount;
15 private const WIKITEXT_REGEX = '/{?{(PLURAL|GRAMMAR|GENDER):/';
16 private const EXCLUDED_TARGET_LANGUAGES = [ 'zh' ];
17
18 protected function handlePairsForService( array $response ): array {
19 $pairs = [];
20 foreach ( $response[$this->getServiceName()] as $source => $targets ) {
21 $filteredTargets = array_diff( $targets, self::EXCLUDED_TARGET_LANGUAGES );
22 foreach ( $filteredTargets as $target ) {
23 $pairs[$source][$target] = true;
24 }
25 }
26
27 return $pairs;
28 }
29
30 protected function getServiceName(): string {
31 return 'MinT';
32 }
33
34 protected function handleServiceResponse( array $responseBody ): string {
35 return trim( $this->unwrapUntranslatable( $responseBody[ 'contents' ] ) );
36 }
37
38 protected function wrapUntranslatable( string $text ): string {
39 // Keep track of the number of wikitext instances in the source string.
40 $this->wikitextCount = preg_match_all( self::WIKITEXT_REGEX, $text );
41 return $text;
42 }
43
44 protected function unwrapUntranslatable( string $text ): string {
45 if ( $this->wikitextCount !== 0 ) {
46 // Verify that the wikitext instances are the same as before translation
47 $postWikitextCount = preg_match_all( self::WIKITEXT_REGEX, $text );
48 if ( $postWikitextCount !== $this->wikitextCount ) {
49 throw new TranslationWebServiceException( 'Missing wikitext in response from MinT' );
50 }
51 }
52 return $text;
53 }
54}
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.
unwrapUntranslatable(string $text)
Undo the hopyfully untouched mangling done by wrapUntranslatable.