summaryrefslogtreecommitdiff
blob: a8eafcfac4c11fbeb3c992c840b9311e80a2d938 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
declare( strict_types = 1 );

namespace MediaWiki\Extension\Translate\TranslatorInterface\Aid;

use MediaWiki\MediaWikiServices;

/**
 * Translation aid that provides the "in other languages" suggestions.
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 * @since 2013-01-01
 * @ingroup TranslationAids
 */
class InOtherLanguagesAid extends TranslationAid {
	public function getData(): array {
		$suggestions = [
			'**' => 'suggestion',
		];

		// Fuzzy translations are not included in these
		$translations = $this->dataProvider->getGoodTranslations();
		$code = $this->handle->getCode();

		$sourceLanguage = $this->handle->getGroup()->getSourceLanguage();

		foreach ( $this->getFallbacks( $code ) as $fallbackCode ) {
			if ( !isset( $translations[$fallbackCode] ) ) {
				continue;
			}

			if ( $fallbackCode === $sourceLanguage ) {
				continue;
			}

			$suggestions[] = [
				'language' => $fallbackCode,
				'value' => $translations[$fallbackCode],
			];
		}

		return $suggestions;
	}

	/**
	 * Get the languages for "in other languages". That would be translation
	 * assistant languages with defined language fallbacks additionally.
	 * @param string $code
	 * @return string[] List of language codes
	 */
	protected function getFallbacks( string $code ): array {
		global $wgTranslateLanguageFallbacks;
		$mwServices = MediaWikiServices::getInstance();

		// User preference has the final say
		$userOptionLookup = $mwServices->getUserOptionsLookup();
		$preference = $userOptionLookup->getOption( $this->context->getUser(), 'translate-editlangs' );
		if ( $preference !== 'default' ) {
			$fallbacks = array_map( 'trim', explode( ',', $preference ) );
			foreach ( $fallbacks as $k => $v ) {
				if ( $v === $code ) {
					unset( $fallbacks[$k] );
				}
			}

			return $fallbacks;
		}

		// Global configuration settings
		$fallbacks = [];
		if ( isset( $wgTranslateLanguageFallbacks[$code] ) ) {
			$fallbacks = (array)$wgTranslateLanguageFallbacks[$code];
		}

		$list = $mwServices->getLanguageFallback()->getAll( $code );
		$fallbacks = array_merge( $list, $fallbacks );

		return array_unique( $fallbacks );
	}
}