Support UI language change when global preferences are present

Notable changes:
* First query global preferences to detect if global language setting
  is in use. If there is no global language setting, or if GlobalPreferences
  extension is not installed, it will fall back to changing the language
  as usual. If global language setting is found, it will add an override
  instead.
* If a local override is added, the undo tooltip is different and links to
  the global preferences page. The task design shows mw.notify style popup
  located on a bottom right corner (LTR). I deviate from the design and
  re-use the old undo tooltip with a different message instead, for
  consistency. The message is chosen depending on whether local storage
  value `uls-gp` is set to '1' (set in mw.uls.changeLanguage).
* I removed one use of deprecated mediawiki.api.options module. One other
  use still remains.
* I changed tooltip text generation from html acrobatics to use
  mw.message.parseDom. Because of that I also had to move the click handler
  to avoid buildup of click handlers.
* In message documentation fixed acronym -> autonym.

Bug: T198206
Change-Id: Ie2ed792e222be919522bd1cdea98042515a0619d
This commit is contained in:
Niklas Laxström
2018-08-14 09:46:37 +02:00
committed by jenkins-bot
parent 2e5e57d92c
commit fa9cea4627
5 changed files with 70 additions and 42 deletions

View File

@@ -168,11 +168,11 @@
"vector": "css/ext.uls-vector.less"
},
"dependencies": [
"jquery.uls.data",
"mediawiki.api",
"mediawiki.api.options",
"mediawiki.cookie",
"mediawiki.user",
"jquery.uls.data"
"mediawiki.storage",
"mediawiki.user"
],
"localBasePath": "resources",
"remoteExtPath": "UniversalLanguageSelector/resources"
@@ -215,6 +215,7 @@
"uls-plang-title-languages",
"ext-uls-select-language-settings-icon-tooltip",
"ext-uls-undo-language-tooltip-text",
"ext-uls-undo-language-tooltip-text-local",
"ext-uls-language-settings-preferences-link"
],
"localBasePath": "resources",

View File

@@ -14,6 +14,7 @@
"uls-ime-helppage": "https://www.mediawiki.org/wiki/Special:MyLanguage/Help:Extension:UniversalLanguageSelector/Input_methods/$1",
"ext-uls-select-language-settings-icon-tooltip": "Language settings",
"ext-uls-undo-language-tooltip-text": "Language changed from $1",
"ext-uls-undo-language-tooltip-text-local": "Language changed for this wiki. Check your [[Special:GlobalPreferences|global preferences]] to change language for all wikis.",
"ext-uls-language-settings-preferences-link": "More language settings",
"uls-betafeature-label": "Compact language links",
"uls-betafeature-desc": "Show a shorter version of the language list, with just the languages that are more relevant to you.",

View File

@@ -17,7 +17,8 @@
"uls-plang-title-languages": "A title for the are in the sidebar in which the interlanguage links are supposed to appear.\nThis title is shown when there are no interlanguage links there, but an icon that enables the ULS is shown.\n{{Identical|Language}}",
"uls-ime-helppage": "Target page for ime helps. Parameters:\n* $1 - ime id. Intended for wiki local customization. e.g. cyrl-palochka",
"ext-uls-select-language-settings-icon-tooltip": "A tooltip for the icon that shows the language selector.\n{{Identical|Language settings}}",
"ext-uls-undo-language-tooltip-text": "Text for the tooltip appearing when language is changed. Parameters:\n* $1 - the previous language acronym",
"ext-uls-undo-language-tooltip-text": "Text for the tooltip appearing when language is changed. Parameters:\n* $1 - the previous language autonym",
"ext-uls-undo-language-tooltip-text-local": "Text for the tooltip appearing when language is changed locally for one wiki when global language setting is in use. Parameters:\n* $1 - the previous language autonym",
"ext-uls-language-settings-preferences-link": "Text for the link showin in user preference screen",
"uls-betafeature-label": "Used as checkbox label for beta feature. \"Compact\" is an adjective and \"language links\" is a short name for the interlanguage links, also known as interwiki (links) in the sidebar of a page.\n\nThe description for this label is {{msg-mw|Uls-betafeature-desc}}.",
"uls-betafeature-desc": "Description for the [[mw:Universal Language Selector/Design/Interlanguage links|compact interlanguage links beta feature]]. The \"language list\" is the \"{{int:otherlanguages}}\" box in the sidebar, also known as interwikis.\n\nThis description is for the checkbox label {{msg-mw|Uls-betafeature-label}}.",

View File

@@ -50,23 +50,49 @@
}
deferred.done( function () {
var api;
var api = new mw.Api();
if ( mw.user.isAnon() ) {
changeLanguageAnon();
return;
}
api = new mw.Api();
api.saveOption( 'language', language )
.done( function () {
location.reload();
} )
.fail( function () {
// Set options failed. Maybe the user has logged off.
// Continue like anonymous user and set cookie.
changeLanguageAnon();
// TODO We can avoid doing this query if we know global preferences are not enabled
api.get( {
action: 'query',
meta: 'globalpreferences',
gprprop: 'preferences'
} ).then( function ( res ) {
// Check whether global preferences are in use. If they are not, `res.query` is
// an empty object. `res` will also contain warnings about unknown parameters.
try {
return !!res.query.globalpreferences.preferences.language;
} catch ( e ) {
return false;
}
} ).then( function ( hasGlobalPreference ) {
var apiModule;
if ( hasGlobalPreference ) {
apiModule = 'globalpreferenceoverrides';
mw.storage.set( 'uls-gp', '1' );
} else {
apiModule = 'options';
mw.storage.remove( 'uls-gp' );
}
return api.postWithToken( 'csrf', {
action: apiModule,
optionname: 'language',
optionvalue: language
} );
} ).done( function () {
location.reload();
} ).fail( function () {
// Setting the option failed. Maybe the user has logged off.
// Continue like anonymous user and set cookie.
changeLanguageAnon();
} );
} );
mw.hook( 'mw.uls.interface.language.change' ).fire( language, deferred );

View File

@@ -157,24 +157,6 @@
// hide the tooltip when clicked on it
$( '.uls-tipsy' ).on( 'click', hideTipsy );
// Event handler for links in the tooltip.
// It looks like the tipsy is always created from scratch so that
// there wont be multiple event handlers bound to same click.
$( 'a.uls-prevlang-link' ).on( 'click.ulstipsy', function ( event ) {
var deferred = $.Deferred();
event.preventDefault();
deferred.done( function () {
mw.uls.changeLanguage( event.target.lang );
} );
mw.hook( 'mw.uls.language.revert' ).fire( deferred );
// Delay is zero if event logging is not enabled
window.setTimeout( function () {
deferred.resolve();
}, mw.config.get( 'wgULSEventLogging' ) * 500 );
} );
tipsyTimer = window.setTimeout( hideTipsy, timeout );
}
@@ -203,9 +185,12 @@
$floatableContainer: $ulsTrigger,
position: ulsPopupPosition,
$content: ( function () {
var link = $( '<a>' ).text( previousAutonym )
.attr( {
href: '#',
var messageKey, $link;
$link = $( '<a>' )
.text( previousAutonym )
.prop( {
href: '',
'class': 'uls-prevlang-link',
lang: previousLang,
// We could get dir from uls.data,
@@ -213,16 +198,30 @@
// and 'auto' is safe enough in this context.
// T130390: must use attr
dir: 'auto'
} )
.on( 'click', function ( event ) {
var deferred = $.Deferred();
event.preventDefault();
deferred.done( function () {
mw.uls.changeLanguage( event.target.lang );
} );
mw.hook( 'mw.uls.language.revert' ).fire( deferred );
// Delay is zero if event logging is not enabled
window.setTimeout( function () {
deferred.resolve();
}, mw.config.get( 'wgULSEventLogging' ) * 500 );
} );
// Get the html of the link by wrapping it in div first
link = $( '<div>' ).html( link ).html();
if ( mw.storage.get( 'uls-gp' ) === '1' ) {
messageKey = 'ext-uls-undo-language-tooltip-text-local';
} else {
messageKey = 'ext-uls-undo-language-tooltip-text';
}
return $( '<p>' )
.html(
mw.message( 'ext-uls-undo-language-tooltip-text', '$1' )
.escaped().replace( '$1', link )
);
return $( '<p>' ).append( mw.message( messageKey, $link ).parseDom() );
}() )
} );