Jdlrobson has uploaded a new change for review.

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

Change subject: Allow localization of LESS files on a per module basis
......................................................................

Allow localization of LESS files on a per module basis

This allows ResourceLoader modules to define less variables that vary
depending on the current language. The immediate use case is for varying
the default font for MediaWiki however there are other use cases
e.g. quotation marks [1]
[1] https://en.wikipedia.org/wiki/International_variation_in_quotation_marks

Change-Id: I00f88cafeade9a34ebd75590491500e59b097ead
---
M includes/resourceloader/ResourceLoaderFileModule.php
M resources/Resources.php
M skins/vector/variables.less
3 files changed, 47 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/60/125760/1

diff --git a/includes/resourceloader/ResourceLoaderFileModule.php 
b/includes/resourceloader/ResourceLoaderFileModule.php
index 68bfe59..772bb01 100644
--- a/includes/resourceloader/ResourceLoaderFileModule.php
+++ b/includes/resourceloader/ResourceLoaderFileModule.php
@@ -90,6 +90,15 @@
         */
        protected $skinStyles = array();
        /**
+        * Array: List of variables that can be used in LESS that are specific
+        * to a certain language. Defaults to the 'default' keyword
+        * @par Usage:
+        * @code
+        * array( [less-variable-name] => array( [language-code] => [value],)
+        * @endcode
+        */
+       protected $lessVariables = array();
+       /**
         * Array: List of modules this module depends on
         * @par Usage:
         * @code
@@ -113,6 +122,8 @@
        protected $debugRaw = true;
        /** Boolean: Whether mw.loader.state() call should be omitted */
        protected $raw = false;
+       /** String: The language code representing the language that the module 
is being compiled under */
+       protected $languageCode;
        protected $targets = array( 'desktop' );
 
        /**
@@ -220,6 +231,7 @@
                                // Collated lists of file paths
                                case 'languageScripts':
                                case 'skinScripts':
+                               case 'lessVariables':
                                case 'skinStyles':
                                        if ( !is_array( $option ) ) {
                                                throw new MWException(
@@ -315,6 +327,7 @@
         * @return string: CSS code for $context
         */
        public function getStyles( ResourceLoaderContext $context ) {
+               $this->languageCode = $context->getLanguage();
                $styles = $this->readStyleFiles(
                        $this->getStyleFiles( $context ),
                        $this->getFlip( $context )
@@ -587,6 +600,26 @@
        }
 
        /**
+        * Gets a string that match a key, optionally using a fallback key.
+        * Similar to tryForKey only deals with strings
+        * @param array $list List of lists to select from
+        * @param string $key Key to look for in $map
+        * @param string $fallback Key to look for in $list if $key doesn't 
exist
+        * @return string: String for the given key
+        */
+       protected static function tryForKeyValue( array $list, $key, $fallback 
= null ) {
+               if ( isset( $list[$key] ) && is_string( $list[$key] ) ) {
+                       return $list[$key];
+               } elseif ( is_string( $fallback )
+                       && isset( $list[$fallback] )
+                       && is_string( $list[$fallback] )
+               ) {
+                       return $list[$fallback];
+               }
+               return '';
+       }
+
+       /**
         * Gets a list of file paths for all scripts in this module, in order 
of propper execution.
         *
         * @param ResourceLoaderContext $context
@@ -804,6 +837,11 @@
                }
 
                $compiler = ResourceLoader::getLessCompiler();
+               $localVars = array();
+               foreach( $this->lessVariables as $key => $values ) {
+                       $localVars[$key] = self::tryForKeyValue( $values, 
$this->languageCode, 'default' );
+               }
+               $compiler->setVariables( $localVars );
                $result = null;
 
                $result = $compiler->cachedCompile( $source );
diff --git a/resources/Resources.php b/resources/Resources.php
index 642897c..cc1f3c7 100644
--- a/resources/Resources.php
+++ b/resources/Resources.php
@@ -139,6 +139,15 @@
                ),
                'remoteBasePath' => $GLOBALS['wgStylePath'],
                'localBasePath' => $GLOBALS['wgStyleDirectory'],
+               'lessVariables' => array(
+                       'content-font-family' => array(
+                               'en' => '"Nimbus Sans L", "Helvetica Neue", 
Arial, Helvetica, sans-serif',
+                               'default' => 'sans-serif',
+                       ),
+                       'content-heading-font-family' => array(
+                               'default' => '"Linux Libertine", Georgia, 
Times, serif',
+                       ),
+               ),
        ),
        'skins.monobook.styles' => array(
                'styles' => array(
diff --git a/skins/vector/variables.less b/skins/vector/variables.less
index 438fbcf..485f30b 100644
--- a/skins/vector/variables.less
+++ b/skins/vector/variables.less
@@ -3,14 +3,11 @@
 // Page content
 // FIXME: Use global variable since Echo and CentralNotice use this variable
 @content-border-color: #a7d7f9;
-// FIXME: Find an open font that works with this stack and is readable by 
Windows users
-@content-font-family: sans-serif;
 @content-font-color: #252525;
 @content-font-size: 0.875em;
 @content-line-height: 1.6;
 @content-padding: 1em;
 @content-heading-font-size: 1.8em;
-@content-heading-font-family: "Linux Libertine", Georgia, Times, serif;
 @body-background-color: #fff;
 @heading-line-height: 1.3;
 

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

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

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

Reply via email to