ULS i18n performance fixes

* Since there is an API to load the i18n json files, avoid sending
  list of localizations to client. Previously this list was used to
  avoid 404s for json files. Now API takes care of it. Remove the
  caching for this list too.

* Preload the i18n files on document ready. This avoids delay in
  initializing IME or ULS window after fetch i18n from server.

* ApiULSLocalization can serve messages for all namespaces known if
  namespace parameter is not set

Bug: 50391
Change-Id: I6b848da35f57353790bf460983cdf19a11bb238a
This commit is contained in:
Santhosh Thottingal
2013-06-30 18:08:19 +05:30
parent 45adf17538
commit 2776a28d58
3 changed files with 25 additions and 73 deletions

View File

@@ -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;
}

View File

@@ -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',
);
}

View File

@@ -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 () {