mediawiki-skins-Vector/includes/Components/VectorComponentMenuVariants.php
Jon Robson bfeb729f84 Component: VariantsMenu
Bug: T319355
Change-Id: I10a8e796f6f6744fcc79ff6b1eb8f1d42389acba
2023-01-11 23:05:41 +00:00

47 lines
1.3 KiB
PHP

<?php
namespace MediaWiki\Skins\Vector\Components;
use Language;
use MediaWiki\MediaWikiServices;
use MediaWiki\StubObject\StubUserLang;
/**
* VectorComponentMenuVariants component
*/
class VectorComponentMenuVariants extends VectorComponentMenu {
/**
* @param array $data
* @param Language|StubUserLang $pageLang
* @param string $ariaLabel
*/
public function __construct( array $data, $pageLang, string $ariaLabel ) {
parent::__construct( $this->updateVariantsMenuLabel( $data, $pageLang, $ariaLabel ) );
}
/**
* Change the portlets menu so the label is the selected variant
* @param array $portletData
* @param Language|StubUserLang $pageLang
* @param string $ariaLabel
* @return array
*/
private function updateVariantsMenuLabel( array $portletData, $pageLang, string $ariaLabel ): array {
$languageConverterFactory = MediaWikiServices::getInstance()->getLanguageConverterFactory();
$converter = $languageConverterFactory->getLanguageConverter( $pageLang );
$portletData['label'] = $pageLang->getVariantname(
$converter->getPreferredVariant()
);
// T289523 Add aria-label data to the language variant switcher.
$portletData['aria-label'] = $ariaLabel;
return $portletData;
}
/**
* @inheritDoc
*/
public function getTemplateData(): array {
return parent::getTemplateData();
}
}