Merge "Check language fallbacks in isLanguageRequiringReplyIcon"

This commit is contained in:
jenkins-bot 2023-12-01 21:29:23 +00:00 committed by Gerrit Code Review
commit d4d595c1b8
2 changed files with 42 additions and 11 deletions

View file

@ -555,14 +555,14 @@
"description": "Override availability of DiscussionTools visual enhancements's reply buttons sub-feature. 'default', 'available', or 'unavailable'."
},
"DiscussionTools_visualenhancements_reply_icon_languages": {
"value": [
"ar",
"zh",
"yue",
"ja",
"nod"
],
"description": "A list of language codes which will receive an icon in their enhanced reply-button treatment, due to translation being very short (T323537).",
"value": {
"ar": true,
"zh": true,
"yue": true,
"ja": true,
"nod": true
},
"description": "A dictionary of language codes which will receive an icon in their enhanced reply-button treatment, due to translation being very short (T323537). Language fallbacks will be checked unless a language is explicitly set to `false` (T352289). Normalized language codes are lower case.",
"merge_strategy": "provide_default"
},
"DiscussionTools_visualenhancements_newsectionlink_enable": {

View file

@ -2,6 +2,7 @@
namespace MediaWiki\Extension\DiscussionTools;
use ConfigException;
use Html;
use IContextSource;
use Language;
@ -932,10 +933,40 @@ class CommentFormatter {
return $pout->getExtensionData( 'DiscussionTools-hasCommentsInLedeContent' ) === true;
}
public static function isLanguageRequiringReplyIcon( Language $lang ): bool {
$dtConfig = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'discussiontools' );
/**
* Check if the language requires an icon for the reply button
*
* @param Language $userLang Language
* @return bool
*/
public static function isLanguageRequiringReplyIcon( Language $userLang ): bool {
$services = MediaWikiServices::getInstance();
$dtConfig = $services->getConfigFactory()->makeConfig( 'discussiontools' );
$languages = $dtConfig->get( 'DiscussionTools_visualenhancements_reply_icon_languages' );
return in_array( $lang->getCode(), $languages, true );
if ( array_is_list( $languages ) ) {
// Detect legacy list format
throw new ConfigException(
'DiscussionTools_visualenhancements_reply_icon_languages must be an associative array'
);
}
// User language matched exactly and is explicitly set to true or false
if ( isset( $languages[ $userLang->getCode() ] ) ) {
return (bool)$languages[ $userLang->getCode() ];
}
// Check fallback languages
$fallbackLanguages = $userLang->getFallbackLanguages();
foreach ( $fallbackLanguages as $fallbackLangauge ) {
if ( isset( $languages[ $fallbackLangauge ] ) ) {
return (bool)$languages[ $fallbackLangauge ];
}
}
// Language not listed, default is to show no icon
return false;
}
}