diff --git a/UniversalLanguageSelector.hooks.php b/UniversalLanguageSelector.hooks.php index 6486120b..cbd043ea 100644 --- a/UniversalLanguageSelector.hooks.php +++ b/UniversalLanguageSelector.hooks.php @@ -250,41 +250,6 @@ class UniversalLanguageSelectorHooks { $vars['wgULSPosition'] = $wgULSPosition; $vars['wgULSAnonCanChangeLanguage'] = $wgULSAnonCanChangeLanguage; - // ULS is localized using jquery.i18n library. Unless it knows - // the localized locales, it can create 404 response. To avoid that, - // send the locales available at server. Also avoid directory scanning - // in each request by putting the locale list in cache. - $cache = wfGetCache( CACHE_ANYTHING ); - $key = wfMemcKey( 'uls', 'i18n', 'locales' ); - $result = $cache->get( $key ); - - if ( $result ) { - $vars['wgULSi18nLocales'] = $result; - } else { - $mwULSL10nFiles = glob( __DIR__ . '/i18n/*.json' ); - - $mwULSL10nLocales = array(); - foreach ( $mwULSL10nFiles as $localeFile ) { - $mwULSL10nLocales[] = basename( $localeFile, '.json' ); - } - - $mwULSL10nFiles = glob( __DIR__ . '/lib/jquery.uls/i18n/*.json' ); - - $jqueryULSL10nLocales = array(); - foreach ( $mwULSL10nFiles as $localeFile ) { - $jqueryULSL10nLocales[] = basename( $localeFile, '.json' ); - } - - $vars['wgULSi18nLocales'] = array( - // Locales to which jQuery ULS is localized. - 'uls' => $jqueryULSL10nLocales, - // Locales to which Mediawiki ULS is localized. - 'ext-uls' => $mwULSL10nLocales, - ); - - // Cache it for 1 hour - $cache->set( $key, $vars['wgULSi18nLocales'], 3600 ); - } return true; } diff --git a/api/ApiULSLocalization.php b/api/ApiULSLocalization.php index f03cf78e..07685889 100644 --- a/api/ApiULSLocalization.php +++ b/api/ApiULSLocalization.php @@ -34,22 +34,25 @@ class ApiULSLocalization extends ApiBase { $this->dieUsage( 'Invalid language', 'invalidlanguage' ); } - if ( $namespace === 'uls' ) { - $filename = "lib/jquery.uls/i18n/$language.json"; - } else { - $filename = "i18n/$language.json"; + $contents = array(); + // jQuery.uls localization + if ( !$namespace || $namespace === 'uls' ) { + $filename = __DIR__ . "/../lib/jquery.uls/i18n/$language.json"; + if ( file_exists( $filename ) ) { + $contents += json_decode( file_get_contents( $filename ), true ); + } } + // mediaWiki.uls localization + if ( !$namespace || $namespace === 'ext-uls' ) { + $filename = __DIR__ . "/../i18n/$language.json"; + if ( file_exists( $filename ) ) { + $contents += json_decode( file_get_contents( $filename ), true ); + } + } + // Output the file's contents raw + $this->getResult()->addValue( null, 'text', json_encode( $contents ) ); + $this->getResult()->addValue( null, 'mime', 'application/json' ); - $localPath = __DIR__ . "/../$filename"; - if ( !file_exists( $localPath ) ) { - $this->getResult()->addValue( null, 'text', '{}' ); - $this->getResult()->addValue( null, 'mime', 'application/json' ); - } else { - $contents = file_get_contents( $localPath ); - // Output the file's contents raw - $this->getResult()->addValue( null, 'text', $contents ); - $this->getResult()->addValue( null, 'mime', 'application/json' ); - } } public function getCustomPrinter() { @@ -68,7 +71,6 @@ class ApiULSLocalization extends ApiBase { 'namespace' => array( ApiBase::PARAM_TYPE => 'string', ApiBase::PARAM_REQUIRED => false, - ApiBase::PARAM_DFLT => 'ext-uls', ), ); } @@ -76,7 +78,7 @@ class ApiULSLocalization extends ApiBase { public function getParamDescription() { return array( 'language' => 'Language string', - 'namespace' => 'Namespace string.', + 'namespace' => 'Namespace string. If not given loads messages for all namespaces known', ); } diff --git a/resources/js/ext.uls.init.js b/resources/js/ext.uls.init.js index 083711ff..1ecccd78 100644 --- a/resources/js/ext.uls.init.js +++ b/resources/js/ext.uls.init.js @@ -120,31 +120,16 @@ * i18n initialization */ function i18nInit() { - var jsonLoader, locales, i18n; + var jsonLoader = mw.util.wikiScript( 'api' ) + '?action=ulslocalization&language='; - jsonLoader = mw.util.wikiScript( 'api' ) + '?action=ulslocalization&language='; - locales = mw.config.get( 'wgULSi18nLocales' ); - i18n = $.i18n( { + $.i18n( { locale: currentLang, - messageLocationResolver: function ( locale, messageKey ) { - // Namespaces are not available in jquery.i18n yet. Developers prefix - // the message key with a unique namespace like ext-uls-* - if ( messageKey.indexOf( 'uls' ) === 0 ) { - if ( $.inArray( locale, locales.uls ) >= 0 ) { - return jsonLoader + locale + '&namespace=uls'; - } - - return false; - } - if ( messageKey.indexOf( 'ext-uls' ) === 0 ) { - if ( $.inArray( locale, locales['ext-uls'] ) >= 0 ) { - return jsonLoader + locale + '&namespace=ext-uls'; - } - - return false; - } + messageLocationResolver: function ( locale ) { + return jsonLoader + locale; } - } ); + } ) + // Preload i18n for current language. + .load( jsonLoader + currentLang, currentLang ); } $( document ).ready( function () {