Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
TranslateInterfacePreferences
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 2
12
0.00% covered (danger)
0.00%
0 / 1
 translationAssistLanguages
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 languageSelector
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorInterface;
5
6use MediaWiki\Context\RequestContext;
7use MediaWiki\Extension\Translate\Utilities\HTMLJsSelectToInputField;
8use MediaWiki\Extension\Translate\Utilities\JsSelectToInput;
9use MediaWiki\MediaWikiServices;
10use MediaWiki\User\User;
11use MediaWiki\Xml\XmlSelect;
12
13/**
14 * Contains classes for addition of extension specific preference settings.
15 *
16 * @author Siebrand Mazeland
17 * @author Niklas Laxström
18 * @copyright Copyright © 2008-2010 Siebrand Mazeland, Niklas Laxström
19 * @license GPL-2.0-or-later
20 */
21class TranslateInterfacePreferences {
22    /**
23     * Add 'translate-editlangs' preference.
24     * These are the languages also shown when translating.
25     */
26    public static function translationAssistLanguages( User $user, array &$preferences ): void {
27        // Get selector.
28        $select = self::languageSelector();
29        // Set target ID.
30        $select->setTargetId( 'mw-input-translate-editlangs' );
31        // Get available languages.
32        $languages = MediaWikiServices::getInstance()->getLanguageNameUtils()->getLanguageNames();
33
34        $preferences['translate-editlangs'] = [
35            'class' => HTMLJsSelectToInputField::class,
36            // prefs-translate
37            'section' => 'editing/translate',
38            'label-message' => 'translate-pref-editassistlang',
39            'help-message' => 'translate-pref-editassistlang-help',
40            'select' => $select,
41            'valid-values' => array_keys( $languages ),
42            'name' => 'translate-editlangs',
43        ];
44    }
45
46    /** JavaScript selector for language codes. */
47    private static function languageSelector(): JsSelectToInput {
48        $lang = RequestContext::getMain()->getLanguage();
49        $languages = MediaWikiServices::getInstance()->getLanguageNameUtils()->getLanguageNames( $lang->getCode() );
50        ksort( $languages );
51
52        $selector = new XmlSelect( false, 'mw-language-selector' );
53        foreach ( $languages as $code => $name ) {
54            $selector->addOption( "$code - $name", $code );
55        }
56
57        return new JsSelectToInput( $selector );
58    }
59}