Mwalker has uploaded a new change for review.

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

Change subject: We can now read JSON I18n files from the hook
......................................................................

We can now read JSON I18n files from the hook

Change-Id: I31f1399073b90015c31dc07c1d18e53cd9e2285b
---
M sites/all/modules/wmf_common/MessageFile.php
1 file changed, 87 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm 
refs/changes/46/125046/1

diff --git a/sites/all/modules/wmf_common/MessageFile.php 
b/sites/all/modules/wmf_common/MessageFile.php
index a01af9c..3cf32c1 100644
--- a/sites/all/modules/wmf_common/MessageFile.php
+++ b/sites/all/modules/wmf_common/MessageFile.php
@@ -9,6 +9,7 @@
  */
 class MessageFile {
 
+  protected $langLoader;
   protected $messages = null;
 
   /**
@@ -23,12 +24,39 @@
       throw new Exception("Message file does not exist: $msg_file");
     }
 
-    // The assumption is made that the included file uses an array called
-    // $messages to store the messages.  This is the MediaWiki default.
+    // It used to be that messages were put into one bit array called
+    // $messages. But now there might also be a shim which will load
+    // messages from an i18n json file. GAH!
     $messages = array();
     include $msg_file;
 
-    $this->messages = $messages;
+    if ( isset( $GLOBALS['wgHooks']['LocalisationCacheRecache'] ) ) {
+      $this->messages = array();
+      $msgs = &$this->messages;
+      $internalLoader = $GLOBALS['wgHooks']['LocalisationCacheRecache'][0];
+      $this->langLoader = function( $langCode ) use ( &$msgs, $internalLoader 
) {
+        $cachedData = array(
+          'fallbackSequence' => array(),
+          'messages' => array()
+        );
+        $internalLoader( null, $langCode, $cachedData );
+        $msgs[$langCode] = $cachedData['messages'];
+        return $cachedData['deps'][0];
+      };
+
+      // Prepopulate english (so that we get the loader)
+      $loader = $this->langLoader;
+      $fileDep = $loader('en');
+      $langs = $fileDep->getOtherI18nLangsFromGlob();
+      foreach( $langs as $lang ) {
+          if ( $lang != 'en' ) {
+              $this->messages[$lang] = array();
+          }
+      }
+    } else {
+      $this->langLoader = function( $langCode ) {};
+      $this->messages = $messages;
+    }
   }
 
   /**
@@ -73,6 +101,14 @@
    * @throws Exception if the MessageFile is not properly initialized
    */
   public function msgExists($key, $language) {
+    if ( !isset( $this->messages[$language] ) ) {
+      return false;
+    }
+    if ( count( $this->messages[$language] ) == 0 ) {
+      $loader = $this->langLoader;
+      $loader( $language );
+    }
+
     return array_key_exists($language, $this->messages)
       && array_key_exists($key, $this->messages[$language]);
   }
@@ -86,4 +122,52 @@
   public function languageList() {
     return array_keys($this->messages);
   }
+
+
+}
+
+/**
+ * Class FormatJson Stub for mediawiki FormatJson
+ */
+class FormatJson {
+    /**
+     * Decodes a JSON string.
+     *
+     * @param string $value The JSON string being decoded
+     * @param bool $assoc When true, returned objects will be converted into 
associative arrays.
+     *
+     * @return mixed: the value encoded in JSON in appropriate PHP type.
+     * `null` is returned if the JSON cannot be decoded or if the encoded data 
is deeper than
+     * the recursion limit.
+     */
+    public static function decode( $value, $assoc = false ) {
+        return json_decode( $value, $assoc );
+    }
+}
+
+/**
+ * Class FileDependency Stub for mediawiki filedependency; just so we dont 
blow up
+ */
+class FileDependency {
+  public function __construct( $fileName ) {
+    $this->fileName = $fileName;
+  }
+  public function getOtherI18nFilesGlob() {
+    // Hopefully all i18n files look like 
/foo/bar/baz/name.stupid.<langcode>.php
+    $path = dirname( $this->fileName );
+    $file = basename( $this->fileName );
+    $parts = explode( '.', $file );
+    $parts[count($parts) -2] = '*';
+    $glob = $path . DIRECTORY_SEPARATOR . implode( '.', $parts );
+    return glob( $glob );
+  }
+  public function getOtherI18nLangsFromGlob() {
+    $files = $this->getOtherI18nFilesGlob();
+    $langs = array();
+    foreach ($files as $file) {
+        $parts = explode( '.', basename( $file ) );
+        $langs[] = $parts[count($parts) -2];
+    }
+    return $langs;
+  }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I31f1399073b90015c31dc07c1d18e53cd9e2285b
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Mwalker <[email protected]>

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

Reply via email to