Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
InOtherLanguagesAid
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 2
90
0.00% covered (danger)
0.00%
0 / 1
 getData
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
 getFallbacks
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorInterface\Aid;
5
6use MediaWiki\MediaWikiServices;
7
8/**
9 * Translation aid that provides the "in other languages" suggestions.
10 * @author Niklas Laxström
11 * @license GPL-2.0-or-later
12 * @since 2013-01-01
13 * @ingroup TranslationAids
14 */
15class InOtherLanguagesAid extends TranslationAid {
16    public function getData(): array {
17        $suggestions = [
18            '**' => 'suggestion',
19        ];
20
21        // Fuzzy translations are not included in these
22        $translations = $this->dataProvider->getGoodTranslations();
23        $code = $this->handle->getCode();
24
25        $sourceLanguage = $this->handle->getGroup()->getSourceLanguage();
26
27        foreach ( $this->getFallbacks( $code ) as $fallbackCode ) {
28            if ( !isset( $translations[$fallbackCode] ) || $fallbackCode === $sourceLanguage ) {
29                continue;
30            }
31
32            $suggestions[] = [
33                'language' => $fallbackCode,
34                'value' => $translations[$fallbackCode],
35            ];
36        }
37
38        return $suggestions;
39    }
40
41    /**
42     * Get the languages for "in other languages". That would be translation
43     * assistant languages with defined language fallbacks additionally.
44     * @param string $code
45     * @return string[] List of language codes
46     */
47    protected function getFallbacks( string $code ): array {
48        global $wgTranslateLanguageFallbacks;
49        $mwServices = MediaWikiServices::getInstance();
50
51        // User preference has the final say
52        $userOptionLookup = $mwServices->getUserOptionsLookup();
53        $preference = $userOptionLookup->getOption( $this->context->getUser(), 'translate-editlangs' );
54        if ( $preference !== 'default' ) {
55            $fallbacks = array_map( 'trim', explode( ',', $preference ) );
56            foreach ( $fallbacks as $k => $v ) {
57                if ( $v === $code ) {
58                    unset( $fallbacks[$k] );
59                }
60            }
61
62            return $fallbacks;
63        }
64
65        // Global configuration settings
66        $fallbacks = [];
67        if ( isset( $wgTranslateLanguageFallbacks[$code] ) ) {
68            $fallbacks = (array)$wgTranslateLanguageFallbacks[$code];
69        }
70
71        $list = $mwServices->getLanguageFallback()->getAll( $code );
72        $fallbacks = array_merge( $list, $fallbacks );
73
74        return array_unique( $fallbacks );
75    }
76}