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:
@@ -250,41 +250,6 @@ class UniversalLanguageSelectorHooks {
|
|||||||
$vars['wgULSPosition'] = $wgULSPosition;
|
$vars['wgULSPosition'] = $wgULSPosition;
|
||||||
$vars['wgULSAnonCanChangeLanguage'] = $wgULSAnonCanChangeLanguage;
|
$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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,22 +34,25 @@ class ApiULSLocalization extends ApiBase {
|
|||||||
$this->dieUsage( 'Invalid language', 'invalidlanguage' );
|
$this->dieUsage( 'Invalid language', 'invalidlanguage' );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $namespace === 'uls' ) {
|
$contents = array();
|
||||||
$filename = "lib/jquery.uls/i18n/$language.json";
|
// jQuery.uls localization
|
||||||
} else {
|
if ( !$namespace || $namespace === 'uls' ) {
|
||||||
$filename = "i18n/$language.json";
|
$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 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$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
|
// Output the file's contents raw
|
||||||
$this->getResult()->addValue( null, 'text', $contents );
|
$this->getResult()->addValue( null, 'text', json_encode( $contents ) );
|
||||||
$this->getResult()->addValue( null, 'mime', 'application/json' );
|
$this->getResult()->addValue( null, 'mime', 'application/json' );
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCustomPrinter() {
|
public function getCustomPrinter() {
|
||||||
@@ -68,7 +71,6 @@ class ApiULSLocalization extends ApiBase {
|
|||||||
'namespace' => array(
|
'namespace' => array(
|
||||||
ApiBase::PARAM_TYPE => 'string',
|
ApiBase::PARAM_TYPE => 'string',
|
||||||
ApiBase::PARAM_REQUIRED => false,
|
ApiBase::PARAM_REQUIRED => false,
|
||||||
ApiBase::PARAM_DFLT => 'ext-uls',
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -76,7 +78,7 @@ class ApiULSLocalization extends ApiBase {
|
|||||||
public function getParamDescription() {
|
public function getParamDescription() {
|
||||||
return array(
|
return array(
|
||||||
'language' => 'Language string',
|
'language' => 'Language string',
|
||||||
'namespace' => 'Namespace string.',
|
'namespace' => 'Namespace string. If not given loads messages for all namespaces known',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -120,31 +120,16 @@
|
|||||||
* i18n initialization
|
* i18n initialization
|
||||||
*/
|
*/
|
||||||
function i18nInit() {
|
function i18nInit() {
|
||||||
var jsonLoader, locales, i18n;
|
var jsonLoader = mw.util.wikiScript( 'api' ) + '?action=ulslocalization&language=';
|
||||||
|
|
||||||
jsonLoader = mw.util.wikiScript( 'api' ) + '?action=ulslocalization&language=';
|
$.i18n( {
|
||||||
locales = mw.config.get( 'wgULSi18nLocales' );
|
|
||||||
i18n = $.i18n( {
|
|
||||||
locale: currentLang,
|
locale: currentLang,
|
||||||
messageLocationResolver: function ( locale, messageKey ) {
|
messageLocationResolver: function ( locale ) {
|
||||||
// Namespaces are not available in jquery.i18n yet. Developers prefix
|
return jsonLoader + locale;
|
||||||
// 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;
|
// Preload i18n for current language.
|
||||||
}
|
.load( jsonLoader + currentLang, currentLang );
|
||||||
if ( messageKey.indexOf( 'ext-uls' ) === 0 ) {
|
|
||||||
if ( $.inArray( locale, locales['ext-uls'] ) >= 0 ) {
|
|
||||||
return jsonLoader + locale + '&namespace=ext-uls';
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$( document ).ready( function () {
|
$( document ).ready( function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user