Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
GoogleTranslateWebService.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\WebService;
5
6use FormatJson;
7use MediaWiki\Http\HttpRequestFactory;
8use Sanitizer;
9
19 private const PUBLIC_API = 'https://translation.googleapis.com/language/translate/v2';
21 private $httpRequestFactory;
22
23 public function __construct(
24 HttpRequestFactory $httpRequestFactory,
25 string $serviceName,
26 array $config
27 ) {
28 parent::__construct( $serviceName, $config );
29 $this->httpRequestFactory = $httpRequestFactory;
30 }
31
33 public function getType(): string {
34 return 'mt';
35 }
36
38 protected function mapCode( string $code ): string {
40 $map = [
41 'be-tarask' => 'be',
42 'zh-cn' => 'zh-CN',
43 'zh-hans' => 'zh-CN',
44 'zh-hant' => 'zh-TW',
45 'zh-tw' => 'zh-TW',
46 ];
47
48 return $map[$code] ?? $code;
49 }
50
52 public function isSupportedLanguagePair( string $sourceLanguage, string $targetLanguage ): bool {
53 $pairs = $this->getSupportedLanguagePairs();
54 $from = $this->mapCode( $sourceLanguage );
55 $to = $this->mapCode( $targetLanguage );
56
57 // As long as the source & target language exist at Google it is fine
58 return isset( $pairs[$from] ) && isset( $pairs[$to] ) && $from !== $to;
59 }
60
62 protected function doPairs(): array {
63 if ( !isset( $this->config['key'] ) ) {
64 throw new TranslationWebServiceConfigurationException( 'API key is not set' );
65 }
66
67 $api = $this->config['pairs'] ?? self::PUBLIC_API . '/languages';
68 $params = [
69 'key' => $this->config['key'],
70 ];
71
72 $json = $this->httpRequestFactory->get(
73 wfAppendQuery( $api, wfArrayToCgi( $params ) ),
74 [ 'timeout' => $this->config['timeout'] ],
75 __METHOD__
76 );
77 $response = FormatJson::decode( $json );
78
79 if ( !is_object( $response ) ) {
80 $exception = 'Malformed reply from remote server: ' . (string)$json;
81 throw new TranslationWebServiceException( $exception );
82 }
83
84 $pairs = [];
85 foreach ( $response->data->languages as $language ) {
86 // Google can translate from any language to any language
87 $pairs[$language->language] = true;
88 }
89
90 return $pairs;
91 }
92
94 protected function getQuery( string $text, string $sourceLanguage, string $targetLanguage ): TranslationQuery {
95 if ( !isset( $this->config['key'] ) ) {
96 throw new TranslationWebServiceConfigurationException( 'API key is not set' );
97 }
98 # https://cloud.google.com/translate/docs/reference/translate
99 if ( strlen( $text ) > 10000 ) {
100 // There is no limitation but we don't want the translation service to be abused, don't we?
101 throw new TranslationWebServiceInvalidInputException( 'Source text too long' );
102 }
103
104 $url = $this->config['url'] ?? self::PUBLIC_API;
105 $text = trim( $text );
106 $text = $this->wrapUntranslatable( $text );
107
108 return TranslationQuery::factory( $url )
109 ->timeout( intval( $this->config['timeout'] ?? 3 ) )
110 ->postWithData( wfArrayToCgi( [
111 'key' => $this->config['key'],
112 'q' => $text,
113 'target' => $targetLanguage,
114 'source' => $sourceLanguage,
115 'format' => 'html',
116 ] ) );
117 }
118
120 protected function parseResponse( TranslationQueryResponse $reply ): string {
121 $body = $reply->getBody();
122 $response = FormatJson::decode( $body );
123 if ( !is_object( $response ) ) {
124 throw new TranslationWebServiceException( 'Invalid json: ' . serialize( $body ) );
125 }
126 $text = Sanitizer::decodeCharReferences( $response->data->translations[0]->translatedText );
127 $text = $this->unwrapUntranslatable( $text );
128
129 return trim( $text );
130 }
131}
return[ 'Translate:ConfigHelper'=> static function():ConfigHelper { return new ConfigHelper();}, 'Translate:CsvTranslationImporter'=> static function(MediaWikiServices $services):CsvTranslationImporter { return new CsvTranslationImporter( $services->getWikiPageFactory());}, 'Translate:EntitySearch'=> static function(MediaWikiServices $services):EntitySearch { return new EntitySearch($services->getMainWANObjectCache(), $services->getCollationFactory() ->makeCollation( 'uca-default-u-kn'), MessageGroups::singleton(), $services->getNamespaceInfo(), $services->get( 'Translate:MessageIndex'), $services->getTitleParser(), $services->getTitleFormatter());}, 'Translate:ExternalMessageSourceStateImporter'=> static function(MediaWikiServices $services):ExternalMessageSourceStateImporter { return new ExternalMessageSourceStateImporter($services->getMainConfig(), $services->get( 'Translate:GroupSynchronizationCache'), $services->getJobQueueGroup(), LoggerFactory::getInstance( 'Translate.GroupSynchronization'), MessageIndex::singleton());}, 'Translate:GroupSynchronizationCache'=> static function(MediaWikiServices $services):GroupSynchronizationCache { return new GroupSynchronizationCache( $services->get( 'Translate:PersistentCache'));}, 'Translate:MessageBundleStore'=> static function(MediaWikiServices $services):MessageBundleStore { return new MessageBundleStore(new RevTagStore(), $services->getJobQueueGroup(), $services->getLanguageNameUtils(), $services->get( 'Translate:MessageIndex'));}, 'Translate:MessageGroupReview'=> static function(MediaWikiServices $services):MessageGroupReview { return new MessageGroupReview($services->getDBLoadBalancer(), $services->getHookContainer());}, 'Translate:MessageIndex'=> static function(MediaWikiServices $services):MessageIndex { $params=$services->getMainConfig() ->get( 'TranslateMessageIndex');if(is_string( $params)) { $params=(array) $params;} $class=array_shift( $params);return new $class( $params);}, 'Translate:ParsingPlaceholderFactory'=> static function():ParsingPlaceholderFactory { return new ParsingPlaceholderFactory();}, 'Translate:PersistentCache'=> static function(MediaWikiServices $services):PersistentCache { return new PersistentDatabaseCache($services->getDBLoadBalancer(), $services->getJsonCodec());}, 'Translate:ProgressStatsTableFactory'=> static function(MediaWikiServices $services):ProgressStatsTableFactory { return new ProgressStatsTableFactory($services->getLinkRenderer(), $services->get( 'Translate:ConfigHelper'));}, 'Translate:SubpageListBuilder'=> static function(MediaWikiServices $services):SubpageListBuilder { return new SubpageListBuilder($services->get( 'Translate:TranslatableBundleFactory'), $services->getLinkBatchFactory());}, 'Translate:TranslatableBundleFactory'=> static function(MediaWikiServices $services):TranslatableBundleFactory { return new TranslatableBundleFactory($services->get( 'Translate:TranslatablePageStore'), $services->get( 'Translate:MessageBundleStore'));}, 'Translate:TranslatableBundleMover'=> static function(MediaWikiServices $services):TranslatableBundleMover { return new TranslatableBundleMover($services->getMovePageFactory(), $services->getJobQueueGroup(), $services->getLinkBatchFactory(), $services->get( 'Translate:TranslatableBundleFactory'), $services->get( 'Translate:SubpageListBuilder'), $services->getMainConfig() ->get( 'TranslatePageMoveLimit'));}, 'Translate:TranslatablePageParser'=> static function(MediaWikiServices $services):TranslatablePageParser { return new TranslatablePageParser($services->get( 'Translate:ParsingPlaceholderFactory'));}, 'Translate:TranslatablePageStore'=> static function(MediaWikiServices $services):TranslatablePageStore { return new TranslatablePageStore($services->get( 'Translate:MessageIndex'), $services->getJobQueueGroup(), new RevTagStore(), $services->getDBLoadBalancer());}, 'Translate:TranslationStashReader'=> static function(MediaWikiServices $services):TranslationStashReader { $db=$services->getDBLoadBalancer() ->getConnectionRef(DB_REPLICA);return new TranslationStashStorage( $db);}, 'Translate:TranslationStatsDataProvider'=> static function(MediaWikiServices $services):TranslationStatsDataProvider { return new TranslationStatsDataProvider(new ServiceOptions(TranslationStatsDataProvider::CONSTRUCTOR_OPTIONS, $services->getMainConfig()), $services->getObjectFactory());}, 'Translate:TranslationUnitStoreFactory'=> static function(MediaWikiServices $services):TranslationUnitStoreFactory { return new TranslationUnitStoreFactory( $services->getDBLoadBalancer());}, 'Translate:TranslatorActivity'=> static function(MediaWikiServices $services):TranslatorActivity { $query=new TranslatorActivityQuery($services->getMainConfig(), $services->getDBLoadBalancer());return new TranslatorActivity($services->getMainObjectStash(), $query, $services->getJobQueueGroup());}, 'Translate:TtmServerFactory'=> static function(MediaWikiServices $services):TtmServerFactory { $config=$services->getMainConfig();$default=$config->get( 'TranslateTranslationDefaultService');if( $default===false) { $default=null;} return new TtmServerFactory( $config->get( 'TranslateTranslationServices'), $default);}]
@phpcs-require-sorted-array
isSupportedLanguagePair(string $sourceLanguage, string $targetLanguage)
@inheritDoc
getQuery(string $text, string $sourceLanguage, string $targetLanguage)
@inheritDoc
Value object that represents a HTTP(S) query response.
Mutable objects that represents a HTTP(S) query.
Used to signal that the requested input is rejected and cannot be used with an external web service.