jenkins-bot has submitted this change and it was merged. Change subject: ResourceLoader Module for serving json based localization messages ......................................................................
ResourceLoader Module for serving json based localization messages ApiULSLocalization is still present since we have to provide live language preview feature. Moved the loading of json file to includes/JsonMessageLoader.php Also moved all RL modules to includes folder. Bug: 56509 Change-Id: Ic39dec1c484982fb07edd167e83794c0b5f470ee --- M Resources.php M UniversalLanguageSelector.php M api/ApiULSLocalization.php A includes/JsonMessageLoader.php A includes/ResourceLoaderULSJsonMessageModule.php R includes/ResourceLoaderULSModule.php M resources/js/ext.uls.i18n.js 7 files changed, 165 insertions(+), 39 deletions(-) Approvals: Amire80: Looks good to me, approved Santhosh: Looks good to me, but someone else must approve jenkins-bot: Verified diff --git a/Resources.php b/Resources.php index 41ae36d..7edce26 100644 --- a/Resources.php +++ b/Resources.php @@ -15,6 +15,10 @@ 'class' => 'ResourceLoaderULSModule' ); +$wgResourceModules['ext.uls.messages'] = array( + 'class' => 'ResourceLoaderULSJsonMessageModule' +); + $wgResourceModules['ext.uls.buttons'] = array( 'styles' => 'resources/css/ext.uls.buttons.css', ) + $resourcePaths; @@ -68,7 +72,7 @@ 'jquery.json', 'jquery.cookie', 'jquery.uls', - 'ext.uls.i18n', + 'ext.uls.messages', ), 'position' => 'top', ) + $resourcePaths; diff --git a/UniversalLanguageSelector.php b/UniversalLanguageSelector.php index 35a39c8..5334d28 100644 --- a/UniversalLanguageSelector.php +++ b/UniversalLanguageSelector.php @@ -152,9 +152,12 @@ // Register auto load for the page class $wgAutoloadClasses += array( 'UniversalLanguageSelectorHooks' => __DIR__ . '/UniversalLanguageSelector.hooks.php', - 'ResourceLoaderULSModule' => __DIR__ . '/ResourceLoaderULSModule.php', + 'ResourceLoaderULSModule' => __DIR__ . '/includes/ResourceLoaderULSModule.php', + 'ResourceLoaderULSJsonMessageModule' => + __DIR__ . '/includes/ResourceLoaderULSJsonMessageModule.php', 'ApiLanguageSearch' => __DIR__ . '/api/ApiLanguageSearch.php', 'ApiULSLocalization' => __DIR__ . '/api/ApiULSLocalization.php', + 'JsonMessageLoader' => __DIR__ . '/includes/JsonMessageLoader.php', 'LanguageNameSearch' => __DIR__ . '/data/LanguageNameSearch.php', ); diff --git a/api/ApiULSLocalization.php b/api/ApiULSLocalization.php index 17504ac..ff96ce0 100644 --- a/api/ApiULSLocalization.php +++ b/api/ApiULSLocalization.php @@ -32,45 +32,10 @@ if ( !Language::isValidCode( $language ) ) { $this->dieUsage( 'Invalid language', 'invalidlanguage' ); } - - $contents = array(); - // jQuery.uls localization - $contents += $this->loadI18nFile( __DIR__ . '/../lib/jquery.uls/i18n', $language ); - // mediaWiki.uls localization - $contents += $this->loadI18nFile( __DIR__ . '/../i18n', $language ); - + $contents = JsonMessageLoader::getMessages( $language ); // Output the file's contents raw $this->getResult()->addValue( null, 'text', json_encode( $contents ) ); $this->getResult()->addValue( null, 'mime', 'application/json' ); - } - - /** - * Load messages from the jquery.i18n json file and from - * fallback languages. - * @param string $dir Directory of the json file. - * @param string $language Language code. - * @return array - */ - protected function loadI18nFile( $dir, $language ) { - $languages = Language::getFallbacksFor( $language ); - // Prepend the requested language code - // to load them all in one loop - array_unshift( $languages, $language ); - $messages = array(); - - foreach ( $languages as $language ) { - $filename = "$dir/$language.json"; - - if ( !file_exists( $filename ) ) { - continue; - } - - $contents = file_get_contents( $filename ); - $messagesForLanguage = json_decode( $contents, true ); - $messages = array_merge( $messagesForLanguage, $messages ); - } - - return $messages; } public function getCustomPrinter() { diff --git a/includes/JsonMessageLoader.php b/includes/JsonMessageLoader.php new file mode 100644 index 0000000..0c509a6 --- /dev/null +++ b/includes/JsonMessageLoader.php @@ -0,0 +1,79 @@ +<?php +/** + * Json formatted MessageLoader for ULS + * + * Copyright (C) 2013 Alolita Sharma, Amir Aharoni, Arun Ganesh, Brandon Harris, + * Niklas Laxström, Pau Giner, Santhosh Thottingal, Siebrand Mazeland and other + * contributors. See CREDITS for a list. + * + * UniversalLanguageSelector is dual licensed GPLv2 or later and MIT. You don't + * have to do anything special to choose one license or the other and you don't + * have to notify anyone which license you are using. You are free to use + * UniversalLanguageSelector in commercial projects as long as the copyright + * header is left intact. See files GPL-LICENSE and MIT-LICENSE for details. + * + * @file + * @ingroup Extensions + * @licence GNU General Public Licence 2.0 or later + * @licence MIT License + * @since 2013.11 + */ + +class JsonMessageLoader { + /** + * Returns all message files that are used to load messages for the given + * language. + * @param string $language Language code. + * @return string[] + */ + public static function getFilenames( $language ) { + $filenames = array(); + + $languages = Language::getFallbacksFor( $language ); + // Prepend the requested language code + // to load them all in one loop + array_unshift( $languages, $language ); + + // jQuery.uls localization + foreach ( $languages as $language ) { + $filenames[] = __DIR__ . "/../lib/jquery.uls/i18n/$language.json"; + } + + // mediaWiki.uls localization + foreach ( $languages as $language ) { + $filenames[] = __DIR__ . "/../i18n/$language.json"; + } + + $filenames = array_filter( $filenames, function( $filename ) { + return file_exists( $filename ); + } ); + + return $filenames; + } + + /** + * Get messages for the given language. + * @param string $language Language code. + * @return array + */ + public static function getMessages( $language ) { + $contents = array(); + + foreach ( self::getFilenames( $language ) as $filename ) { + $contents += self::loadI18nFile( $filename ); + } + + return $contents; + } + + /** + * Load messages from a json file. + * @param string $filename Directory of the json file. + * @return array + */ + protected static function loadI18nFile( $filename ) { + $contents = file_get_contents( $filename ); + + return json_decode( $contents, true ); + } +} diff --git a/includes/ResourceLoaderULSJsonMessageModule.php b/includes/ResourceLoaderULSJsonMessageModule.php new file mode 100644 index 0000000..ca1c40f --- /dev/null +++ b/includes/ResourceLoaderULSJsonMessageModule.php @@ -0,0 +1,63 @@ +<?php +/** + * ResourceLoaderModule subclass for loading the json + * based localization to client-side code. + * + * @file + * @ingroup Extensions + * @author Santhosh Thottingal + */ + +/** + * Packages a remote schema as a JavaScript ResourceLoader module. + * @since 2013.11 + */ +class ResourceLoaderULSJsonMessageModule extends ResourceLoaderModule { + /** + * Part of the ResourceLoader module interface. + * Declares the core ext.uls.i18n module as a dependency. + * @return string[] Module names. + */ + function getDependencies() { + return array( 'ext.uls.i18n' ); + } + + /** + * Gets the last modified timestamp of this module. + * The last modified timestamp controls caching. + * @param ResourceLoaderContext $context + * @return int Unix timestamp. + */ + function getModifiedTime( ResourceLoaderContext $context ) { + $code = $context->getLanguage(); + if ( !Language::isValidCode( $code ) ) { + $code = 'en'; + } + + $mtimes = array_map( + 'filemtime', + JsonMessageLoader::getFilenames( $code ) + ); + // Make sure we have at least one entry + $mtimes[] = 1; + + return max( $mtimes ); + } + + /** + * Get the message strings for the current UI language. Uses + * mw.uls.loadLocalization to register them on the frontend. + * @param ResourceLoaderContext $context + * @return string JavaScript code. + */ + function getScript( ResourceLoaderContext $context ) { + $code = $context->getLanguage(); + if ( !Language::isValidCode( $code ) ) { + $code = 'en'; + } + + $params = array( $code, JsonMessageLoader::getMessages( $code ) ); + + return Xml::encodeJsCall( 'mw.uls.loadLocalization', $params ); + } +} diff --git a/ResourceLoaderULSModule.php b/includes/ResourceLoaderULSModule.php similarity index 100% rename from ResourceLoaderULSModule.php rename to includes/ResourceLoaderULSModule.php diff --git a/resources/js/ext.uls.i18n.js b/resources/js/ext.uls.i18n.js index 72ba46f..c9f5164 100644 --- a/resources/js/ext.uls.i18n.js +++ b/resources/js/ext.uls.i18n.js @@ -34,10 +34,22 @@ // ApiULSLocalization handles fallback in ULS $.i18n.fallbacks = {}; - mw.uls.loadLocalization = function ( locale ) { + /** + * Load localization messags for a locale to the jquery.i18n + * messagestore. + * Also called by RL module ResourceLoaderULSJsonMessageModule + * @param {string} locale the language code + * @param {Object} [messages] + * @return {jQuery.Deferred} + */ + mw.uls.loadLocalization = function ( locale, messages ) { var i18n = $.i18n(); i18n.locale = locale; + if ( messages ) { + i18n.load( messages, locale ); + return $.Deferred().resolve(); + } if ( i18n.messageStore.messages[locale] ) { return $.Deferred().resolve(); } -- To view, visit https://gerrit.wikimedia.org/r/93447 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ic39dec1c484982fb07edd167e83794c0b5f470ee Gerrit-PatchSet: 5 Gerrit-Project: mediawiki/extensions/UniversalLanguageSelector Gerrit-Branch: master Gerrit-Owner: Santhosh <santhosh.thottin...@gmail.com> Gerrit-Reviewer: Amire80 <amir.ahar...@mail.huji.ac.il> Gerrit-Reviewer: Krinkle <krinklem...@gmail.com> Gerrit-Reviewer: Nikerabbit <niklas.laxst...@gmail.com> Gerrit-Reviewer: Ori.livneh <o...@wikimedia.org> Gerrit-Reviewer: Santhosh <santhosh.thottin...@gmail.com> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits