Brian Wolff has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/95746


Change subject: Add UI to discover translated SVG files.
......................................................................

Add UI to discover translated SVG files.

Currently we support rendering SVGs in multiple languages,
but there is no mechanism for users to discover what languages
a file is available in. Show this information on the image page.

At the moment, if unspecified we always default the language to
english (I believe to avoid mass cache splitting, especially
if most languages wouldn't have a translation of the file in
their language). This code was written in such a way so that
this assumption should be changable in the future if we so
desire.

Long term, Jarry has a super cool svg translation extension
which would take over some of this. However I still believe
we should have an interface for this in core, since we do
support the different language renderings in core.

Change-Id: I84506436514e09d71200aa2db3932aa001b55c71
---
M includes/ImagePage.php
M includes/filerepo/file/File.php
M includes/media/MediaHandler.php
M includes/media/SVG.php
M languages/messages/MessagesEn.php
M languages/messages/MessagesQqq.php
M maintenance/language/messageTypes.inc
M maintenance/language/messages.inc
8 files changed, 196 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/46/95746/1

diff --git a/includes/ImagePage.php b/includes/ImagePage.php
index 7ea06b0..51fa6e4 100644
--- a/includes/ImagePage.php
+++ b/includes/ImagePage.php
@@ -505,6 +505,16 @@
                                );
                        }
 
+                       $renderLangOptions = 
$this->displayImg->getAvailableLanguages();
+                       if ( count( $renderLangOptions ) >= 1 ) {
+                               $currentLanguage = $renderLang;
+                               $defaultLang = 
$this->displayImg->getDefaultRenderLanguage();
+                               if ( is_null( $currentLanguage ) ) {
+                                       $currentLanguage = $defaultLang;
+                               }
+                               $out->addHtml( $this->doRenderLangOpt( 
$renderLangOptions, $currentLanguage, $defaultLang ) );
+                       }
+
                        // Add cannot animate thumbnail warning
                        if ( !$this->displayImg->canAnimateThumbIfAppropriate() 
) {
                                // Include the extension so wiki admins can
@@ -927,6 +937,72 @@
                        ? $wgImageLimits[$option]
                        : array( 800, 600 ); // if nothing is set, fallback to 
a hardcoded default
        }
+
+       /**
+        * Output a drop-down box for language options for the file
+        *
+        * @param Array $langChoices Array of string language codes
+        * @param String $curLang Language code file is being viewed in.
+        * @param String $defaultLang Language code that image is rendered in 
by default
+        * @return String HTML to insert underneath image.
+        */
+       protected function doRenderLangOpt( array $langChoices, $curLang, 
$defaultLang ) {
+               global $wgScript;
+               sort( $langChoices );
+               $curLang = wfBCP47( $curLang );
+               $defaultLang = wfBCP47( $defaultLang );
+               $opts = '';
+               $haveCurrentLang = false;
+               $haveDefaultLang = false;
+
+               // We make a list of all the language choices in the file.
+               // Additionally if the default language to render this file
+               // is not included as being in this file (for example, in svgs
+               // usually the fallback content is the english content) also
+               // include a choice for that. Last of all, if we're viewing
+               // the file in a language not on the list, add it as a choice.
+               foreach ( $langChoices as $lang ) {
+                       $code = wfBCP47( $lang );
+                       $name = Language::fetchLanguageName( $code, 
$this->getContext()->getLanguage()->getCode() );
+                       if ( $name !== '' ) {
+                               $display = wfMessage( 'img-lang-opt', $code, 
$name )->text();
+                       } else {
+                               $display = $code;
+                       }
+                       $opts .= "\n" . XML::Option( $display, $code, $curLang 
=== $code );
+                       if ( $curLang === $code ) {
+                               $haveCurrentLang = true;
+                       }
+                       if ( $defaultLang === $code ) {
+                               $haveDefaultLang = true;
+                       }
+               }
+               if ( !$haveDefaultLang ) {
+                       // Its hard to know if the content is really in the 
default language, or
+                       // if its just unmarked content that could be in any 
language.
+                       $opts = XML::Option( wfMessage( 'img-lang-default' 
)->text(), '', $defaultLang === $curLang ) . $opts;
+               }
+               if ( !$haveCurrentLang && $defaultLang !== $curLang ) {
+                       $name = Language::fetchLanguageName( $curLang, 
$this->getContext()->getLanguage()->getCode() );
+                       if ( $name !== '' ) {
+                               $display = wfMessage( 'img-lang-opt', $curLang, 
$name )->text();
+                       } else {
+                               $display = $curLang;
+                       }
+                       $opts =  XML::Option( $display, $curLang, true ) . 
$opts;
+               }
+
+               $select = Html::rawElement( 'select', array( 'id' => 
'mw-imglangselector', 'name' => 'lang' ), $opts );
+               $submit = Xml::submitButton( wfMessage( 'img-lang-go' )->text() 
);
+
+               $formContents = wfMessage( 'img-lang-info' )->rawParams( 
$select, $submit )->parse()
+                       . Html::hidden( 'title', 
$this->getTitle()->getPrefixedDBkey() );
+
+               $langSelectLine = Html::rawElement( 'div', array( 'id' => 
'mw-imglangselector-line' ),
+                       Html::rawElement( 'form', array( 'action' => $wgScript 
), $formContents )
+               );
+               return $langSelectLine;
+       }
 }
 
 /**
diff --git a/includes/filerepo/file/File.php b/includes/filerepo/file/File.php
index ec5f927..cf1e09d 100644
--- a/includes/filerepo/file/File.php
+++ b/includes/filerepo/file/File.php
@@ -470,6 +470,40 @@
        }
 
        /**
+        * Gives a (possibly empty) list of languages to render
+        * the file in.
+        *
+        * If the file doesn't have translations, or if the file
+        * format does not support that sort of thing, returns
+        * an empty array.
+        *
+        * @return Array
+        */
+       public function getAvailableLanguages() {
+               $handler = $this->getHandler();
+               if ( $handler ) {
+                       return $handler->getAvailableLanguages( $this );
+               } else {
+                       return array();
+               }
+       }
+
+       /**
+        * In files that support multiple language, what is the default language
+        * to use if none specified.
+        *
+        * @return String lang code, or null if filetype doesn't support 
multiple languages.
+        */
+       public function getDefaultRenderLanguage() {
+               $handler = $this->getHandler();
+               if ( $handler ) {
+                       return $handler->getDefaultRenderLanguage( $this );
+               } else {
+                       return null;
+               }
+       }
+
+       /**
         * Will the thumbnail be animated if one would expect it to be.
         *
         * Currently used to add a warning to the image description page
diff --git a/includes/media/MediaHandler.php b/includes/media/MediaHandler.php
index 779e23c..2bc1711 100644
--- a/includes/media/MediaHandler.php
+++ b/includes/media/MediaHandler.php
@@ -664,4 +664,28 @@
                return 0;
        }
 
+       /**
+        * Get list of languages file can be viewed in.
+        *
+        * @param File $file
+        * @return Array Array of language codes, or empty array if unsupported.
+        */
+       public function getAvailableLanguages( File $file ) {
+               return array();
+       }
+
+       /**
+        * On file types that support renderings in multiple languages,
+        * which language is used by default if unspecified.
+        *
+        * If getAvailableLanguages returns a non-empty array, this must return
+        * a valid language code. Otherwise can return null if files of this
+        * type do not support alternative language renderings.
+        *
+        * @param File $file
+        * @return String language code or null if multi-language not supported 
for filetype.
+        */
+       public function getDefaultRenderLanguage( File $file ) {
+               return null;
+       }
 }
diff --git a/includes/media/SVG.php b/includes/media/SVG.php
index 72a9696..55dc583 100644
--- a/includes/media/SVG.php
+++ b/includes/media/SVG.php
@@ -64,6 +64,43 @@
        }
 
        /**
+        * Which languages (systemLanguage attribute) is supported.
+        *
+        * @note This list is not guaranteed to be exhaustive.
+        * To avoid OOM errors, we only look at first bit of a file.
+        * Thus all languages on this list are present in the file,
+        * but its possible for the file to have a language not on
+        * this list.
+        *
+        * @param File $file
+        * @return Array of language codes, or empty if no language switching 
supported.
+        */
+       public function getAvailableLanguages( File $file ) {
+               $metadata = $file->getMetadata();
+               $langList = array();
+               if ( $metadata ) {
+                       $metadata = $this->unpackMetadata( $metadata );
+                       if ( isset( $metadata['translations'] ) ) {
+                               foreach( $metadata['translations'] as $lang => 
$langType ) {
+                                       if ( $langType === 
SvgReader::LANG_FULL_MATCH ) {
+                                               $langList[] = $lang;
+                                       }
+                               }
+                       }
+               }
+               return $langList;
+       }
+
+       /**
+        * What language to render file in if none selected.
+        *
+        * @return String language code.
+        */
+       public function getDefaultRenderLanguage( File $file ) {
+               return 'en';
+       }
+
+       /**
         * We do not support making animated svg thumbnails
         */
        function canAnimateThumb( $file ) {
@@ -115,7 +152,7 @@
                $clientHeight = $params['height'];
                $physicalWidth = $params['physicalWidth'];
                $physicalHeight = $params['physicalHeight'];
-               $lang = isset( $params['lang'] ) ? $params['lang'] : 'en';
+               $lang = isset( $params['lang'] ) ? $params['lang'] : 
$this->getDefaultRenderLanguage( $image );
 
                if ( $flags & self::TRANSFORM_LATER ) {
                        return new ThumbnailImage( $image, $dstUrl, $dstPath, 
$params );
diff --git a/languages/messages/MessagesEn.php 
b/languages/messages/MessagesEn.php
index 0c1b560..b0a27f7 100644
--- a/languages/messages/MessagesEn.php
+++ b/languages/messages/MessagesEn.php
@@ -4654,6 +4654,11 @@
 'imgmultigo'       => 'Go!',
 'imgmultigoto'     => 'Go to page $1',
 
+'img-lang-opt' => '$2 ($1)',
+'img-lang-default' => '(default language)',
+'img-lang-info' => 'Render this image in $1 $2.',
+'img-lang-go' => 'Go',
+
 # Table pager
 'ascending_abbrev'         => 'asc',
 'descending_abbrev'        => 'desc',
diff --git a/languages/messages/MessagesQqq.php 
b/languages/messages/MessagesQqq.php
index bbe797c..ae1dd6e 100644
--- a/languages/messages/MessagesQqq.php
+++ b/languages/messages/MessagesQqq.php
@@ -9558,6 +9558,17 @@
 See also:
 * {{msg-mw|Imgmultigo|Submit button text}}',
 
+'img-lang-opt' => '{{optional}} Items in the language drop down on the image 
page for a translated SVG file. For an example see [[:File:Gerrit patchset 
25838 test.svg]]. See also {{msg-mw|img-lang-info}}
+
+* $1 Language code
+* $2 Language name (Either in the language in question, or the name of that 
language translated to the current users interface language)',
+'img-lang-default' => 'An option in the drop down of a translatable file. For 
example see [[:File:Gerrit patchset 25838 test.svg]]. Used when it cannot be 
determined what the default fallback language is. However it should be noted 
that most of the time, the content displayed for this option would be in 
English.',
+'img-lang-info' => 'Label for drop down box. Appears underneath the image on 
the image description page. See [[:File:Gerrit patchset 25838 test.svg]] for an 
example.
+
+* $1 is a drop down box with language options (See also 
{{msg-mw|img-lang-opt}})
+* $2 is a submit button, which uses the text from {{msg-mw|img-lang-go}}',
+'img-lang-go' => 'Go button for the language select for translatable files. 
See [[:File:Gerrit patchset 25838 test.svg]] for an example. See 
{{msg-mw|img-lang-info}}.',
+
 # Table pager
 'ascending_abbrev' => 'Abbreviation of ascending order.
 See also:
diff --git a/maintenance/language/messageTypes.inc 
b/maintenance/language/messageTypes.inc
index 8676d74..aebbb25 100644
--- a/maintenance/language/messageTypes.inc
+++ b/maintenance/language/messageTypes.inc
@@ -486,6 +486,7 @@
        'limitreport-expansiondepth-value',
        'limitreport-expensivefunctioncount-value',
        'tooltip-iwiki',
+       'img-lang-opt',
 );
 
 /** Exif messages, which may be set as optional in several checks, but are 
generally mandatory */
diff --git a/maintenance/language/messages.inc 
b/maintenance/language/messages.inc
index a4fc922..da55a96 100644
--- a/maintenance/language/messages.inc
+++ b/maintenance/language/messages.inc
@@ -3532,6 +3532,12 @@
                'imgmultigo',
                'imgmultigoto',
        ),
+       'img-lang' => array(
+               'img-lang-opt',
+               'img-lang-default',
+               'img-lang-info',
+               'img-lang-go',
+       ),
        'tablepager' => array(
                'ascending_abbrev',
                'descending_abbrev',
@@ -4195,6 +4201,7 @@
        'watch-unwatch'         => 'action=watch/unwatch',
        'separators'            => 'Separators for various lists, etc.',
        'imgmulti'              => 'Multipage image navigation',
+       'img-lang'              => 'Language selector for translatable SVGs',
        'tablepager'            => 'Table pager',
        'autosumm'              => 'Auto-summaries',
        'autoblock_whitelist'   => 'Autoblock whitelist',

-- 
To view, visit https://gerrit.wikimedia.org/r/95746
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I84506436514e09d71200aa2db3932aa001b55c71
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Brian Wolff <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to