Santhosh has uploaded a new change for review. https://gerrit.wikimedia.org/r/93447
Change subject: RL for json messages ...................................................................... RL for json messages Change-Id: Ic39dec1c484982fb07edd167e83794c0b5f470ee --- A ResourceLoaderULSJsonMessageModule.php M Resources.php M UniversalLanguageSelector.php M api/ApiULSLocalization.php M resources/js/ext.uls.i18n.js 5 files changed, 76 insertions(+), 9 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/UniversalLanguageSelector refs/changes/47/93447/1 diff --git a/ResourceLoaderULSJsonMessageModule.php b/ResourceLoaderULSJsonMessageModule.php new file mode 100644 index 0000000..0f2160b --- /dev/null +++ b/ResourceLoaderULSJsonMessageModule.php @@ -0,0 +1,50 @@ +<?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. + */ +class ResourceLoaderULSJsonMessageModule extends ResourceLoaderModule { + + protected $targets = array( 'desktop', 'mobile' ); + + /** + * Part of the ResourceLoader module interface. + * Declares the core ext.uls.i18n module as a dependency. + * @return array: 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 integer: Unix timestamp. + */ + function getModifiedTime( ResourceLoaderContext $context ) { + return filemtime( __DIR__ . '/i18n/' . $context->getLanguage() . '.json' ); + } + + /** + * Ge the message strings for the current UI language, and pass to the client + * side mw.uls.loadLocalization call. + * @param ResourceLoaderContext $context + * @return string: JavaScript code. + */ + function getScript( ResourceLoaderContext $context ) { + $language = $context->getLanguage(); + $params = array( $language, ApiULSLocalization::getMessages( $language ) ); + return Xml::encodeJsCall( 'mw.uls.loadLocalization', $params ); + } +} 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..61f8504 100644 --- a/UniversalLanguageSelector.php +++ b/UniversalLanguageSelector.php @@ -153,6 +153,7 @@ $wgAutoloadClasses += array( 'UniversalLanguageSelectorHooks' => __DIR__ . '/UniversalLanguageSelector.hooks.php', 'ResourceLoaderULSModule' => __DIR__ . '/ResourceLoaderULSModule.php', + 'ResourceLoaderULSJsonMessageModule' => __DIR__ . '/ResourceLoaderULSJsonMessageModule.php', 'ApiLanguageSearch' => __DIR__ . '/api/ApiLanguageSearch.php', 'ApiULSLocalization' => __DIR__ . '/api/ApiULSLocalization.php', 'LanguageNameSearch' => __DIR__ . '/data/LanguageNameSearch.php', diff --git a/api/ApiULSLocalization.php b/api/ApiULSLocalization.php index 17504ac..e00ae07 100644 --- a/api/ApiULSLocalization.php +++ b/api/ApiULSLocalization.php @@ -32,19 +32,27 @@ 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 = self::getMessages( $language ); // Output the file's contents raw $this->getResult()->addValue( null, 'text', json_encode( $contents ) ); $this->getResult()->addValue( null, 'mime', 'application/json' ); } /** + * Get messages for the given language + * @param string $language Language code. + * @return array + */ + public static function getMessages( $language ) { + $contents = array(); + // jQuery.uls localization + $contents += self::loadI18nFile( __DIR__ . '/../lib/jquery.uls/i18n', $language ); + // mediaWiki.uls localization + $contents += self::loadI18nFile( __DIR__ . '/../i18n', $language ); + return $contents; + } + + /** * Load messages from the jquery.i18n json file and from * fallback languages. * @param string $dir Directory of the json file. diff --git a/resources/js/ext.uls.i18n.js b/resources/js/ext.uls.i18n.js index 72ba46f..84367e2 100644 --- a/resources/js/ext.uls.i18n.js +++ b/resources/js/ext.uls.i18n.js @@ -34,13 +34,17 @@ // ApiULSLocalization handles fallback in ULS $.i18n.fallbacks = {}; - mw.uls.loadLocalization = function ( locale ) { + mw.uls.loadLocalization = function ( locale, messages ) { var i18n = $.i18n(); i18n.locale = locale; if ( i18n.messageStore.messages[locale] ) { return $.Deferred().resolve(); } + if ( messages ) { + i18n.load( messages, locale ); + return $.Deferred().resolve(); + } return i18n.load( mw.util.wikiScript( 'api' ) + '?action=ulslocalization&language=' + locale, locale -- To view, visit https://gerrit.wikimedia.org/r/93447 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ic39dec1c484982fb07edd167e83794c0b5f470ee Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/UniversalLanguageSelector Gerrit-Branch: master Gerrit-Owner: Santhosh <santhosh.thottin...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits