Update jquery.i18n from upstream, use its default message store
In Iddef0805ab a custom message store was introduced for ULS, but there were updates in jquery.i18n that allows us to use default message store again. (see https://github.com/wikimedia/jquery.i18n/pull/40) Change-Id: I86cb7a44efa83e5811824cd1104c6be11b1e2925
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* jQuery Internationalization library Message loading , parsing, retrieving utilities
|
||||
* jQuery Internationalization library - Message Store
|
||||
*
|
||||
* Copyright (C) 2012 Santhosh Thottingal
|
||||
*
|
||||
@@ -18,44 +18,20 @@
|
||||
var MessageStore = function () {
|
||||
this.messages = {};
|
||||
this.sources = {};
|
||||
this.locale = String.locale;
|
||||
};
|
||||
|
||||
/**
|
||||
* See https://github.com/wikimedia/jquery.i18n/wiki/Specification#wiki-Message_File_Loading
|
||||
*/
|
||||
MessageStore.prototype = {
|
||||
|
||||
/**
|
||||
* See https://github.com/wikimedia/jquery.i18n/wiki/Specification#wiki-Message_File_Loading
|
||||
*
|
||||
* @param locale
|
||||
*/
|
||||
init: function ( locale ) {
|
||||
var messageStore = this;
|
||||
|
||||
messageStore.locale = locale;
|
||||
messageStore.log( 'initializing for ' + locale );
|
||||
|
||||
$( 'link' ).each( function ( index, element ) {
|
||||
var $link = $( element ),
|
||||
rel = ( $link.attr( 'rel' ) || '' ).toLowerCase().split( /\s+/ );
|
||||
|
||||
if ( $.inArray( 'localizations', rel ) !== -1 ) {
|
||||
// multiple localizations
|
||||
messageStore.load( $link.attr( 'href' ) );
|
||||
} else if ( $.inArray( 'localization', rel ) !== -1 ) {
|
||||
// single localization
|
||||
messageStore.queue( ( $link.attr( 'hreflang' ) || '' ).toLowerCase(),
|
||||
$link.attr( 'href' ) );
|
||||
}
|
||||
} );
|
||||
},
|
||||
|
||||
/**
|
||||
* General message loading API This can take a URL string for
|
||||
* the json formatted messages.
|
||||
* <code>load('path/to/all_localizations.json');</code>
|
||||
*
|
||||
* This can also load a localization file for a locale <code>
|
||||
* load('path/to/de-messages.json', 'de' );
|
||||
* load( 'path/to/de-messages.json', 'de' );
|
||||
* </code>
|
||||
* A data object containing message key- message translation mappings
|
||||
* can also be passed Eg:
|
||||
@@ -65,168 +41,54 @@
|
||||
* null/undefined/false,
|
||||
* all cached messages for the i18n instance will get reset.
|
||||
*
|
||||
* @param {String|Object|null} data
|
||||
* @param {String|Object} source
|
||||
* @param {String} locale Language tag
|
||||
* @return {jQuery.Promise}
|
||||
*/
|
||||
load: function ( data, locale ) {
|
||||
load: function ( source, locale ) {
|
||||
var key = null,
|
||||
messageStore = this,
|
||||
hasOwn = Object.prototype.hasOwnProperty;
|
||||
deferred = null,
|
||||
deferreds = [],
|
||||
messageStore = this;
|
||||
|
||||
if ( !data ) {
|
||||
// reset all localizations
|
||||
messageStore.log( 'Resetting for locale ' + locale );
|
||||
messageStore.messages = {};
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ( typeof data === 'string' ) {
|
||||
if ( typeof source === 'string' ) {
|
||||
// This is a URL to the messages file.
|
||||
messageStore.log( 'Loading messages from: ' + data );
|
||||
$.i18n.log( 'Loading messages from: ' + source );
|
||||
deferred = jsonMessageLoader( source )
|
||||
.done( function ( localization ) {
|
||||
messageStore.set( locale, localization );
|
||||
} );
|
||||
|
||||
messageStore.jsonMessageLoader( data ).done( function ( localization, textStatus ) {
|
||||
messageStore.load( localization, locale );
|
||||
messageStore.queue( locale, data );
|
||||
messageStore.markLoaded( locale, data );
|
||||
} );
|
||||
return deferred.promise();
|
||||
}
|
||||
|
||||
if ( locale ) {
|
||||
// source is an key-value pair of messages for given locale
|
||||
messageStore.set( locale, source );
|
||||
|
||||
return $.Deferred().resolve();
|
||||
} else {
|
||||
// Data is either a group of messages for {locale},
|
||||
// or a group of languages with groups of messages inside.
|
||||
for ( key in data ) {
|
||||
if ( hasOwn.call( data, key ) ) {
|
||||
if ( locale ) {
|
||||
|
||||
// Lazy-init the object
|
||||
if ( !messageStore.messages[locale] ) {
|
||||
messageStore.messages[locale] = {};
|
||||
}
|
||||
|
||||
// Update message object keys,
|
||||
// don't overwrite the entire object.
|
||||
messageStore.messages[locale][key] = data[key];
|
||||
|
||||
messageStore.log(
|
||||
'[' + locale + '][' + key + '] : ' + data[key]
|
||||
);
|
||||
|
||||
// No {locale} given, assume data is a group of languages,
|
||||
// call this function again for each langauge.
|
||||
} else {
|
||||
messageStore.load( data[key], key );
|
||||
}
|
||||
// source is a key-value pair of locales and their source
|
||||
for ( key in source ) {
|
||||
if ( Object.prototype.hasOwnProperty.call( source, key ) ) {
|
||||
locale = key;
|
||||
// No {locale} given, assume data is a group of languages,
|
||||
// call this function again for each language.
|
||||
deferreds.push( messageStore.load( source[key], locale ) );
|
||||
}
|
||||
}
|
||||
return $.when.apply( $, deferreds );
|
||||
}
|
||||
},
|
||||
|
||||
log: function ( /* arguments */ ) {
|
||||
if ( window.console && $.i18n.debug ) {
|
||||
window.console.log.apply( window.console, arguments );
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Mark a message Location for a locale loaded
|
||||
*
|
||||
* Set messages
|
||||
* @param locale
|
||||
* @param messageLocation
|
||||
* @param messages
|
||||
*/
|
||||
markLoaded: function ( locale, messageLocation ) {
|
||||
var i,
|
||||
queue = this.sources[locale];
|
||||
|
||||
if ( !queue ) {
|
||||
this.queue( locale, messageLocation );
|
||||
queue = this.sources[locale];
|
||||
}
|
||||
|
||||
this.sources[locale] = this.sources[locale] || [];
|
||||
|
||||
for ( i = 0; i < queue.length; i++ ) {
|
||||
if ( queue[i].source.url === messageLocation ) {
|
||||
queue[i].source.loaded = true;
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Register the message location for a locale, will be loaded when required
|
||||
*
|
||||
* @param locale
|
||||
* @param messageLocation
|
||||
*/
|
||||
queue: function ( locale, messageLocation ) {
|
||||
var i,
|
||||
queue = this.sources[locale];
|
||||
|
||||
this.sources[locale] = this.sources[locale] || [];
|
||||
|
||||
if ( queue ) {
|
||||
for ( i = 0; i < queue.length; i++ ) {
|
||||
if ( queue[i].source.url === messageLocation ) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.log( 'Source for: ' + locale + ' is ' + messageLocation + ' registered' );
|
||||
this.sources[locale].push( {
|
||||
source: {
|
||||
url: messageLocation,
|
||||
loaded: false
|
||||
}
|
||||
} );
|
||||
},
|
||||
|
||||
/**
|
||||
* Load the messages from the source queue for the locale
|
||||
*
|
||||
* @param {String} locale
|
||||
*/
|
||||
loadFromQueue: function ( locale ) {
|
||||
var i,
|
||||
queue = this.sources[locale];
|
||||
|
||||
if ( queue ) {
|
||||
for ( i = 0; i < queue.length; i++ ) {
|
||||
if ( !queue[i].source.loaded ) {
|
||||
this.load( queue[i].source.url, locale );
|
||||
this.sources[locale][i].source.loaded = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
isLoaded: function ( locale, messageLocation ) {
|
||||
var i,
|
||||
sources = this.sources[locale],
|
||||
result = false;
|
||||
|
||||
if ( sources ) {
|
||||
for ( i = 0; i < sources.length; i++ ) {
|
||||
if ( sources[i].source.url === messageLocation ) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
|
||||
jsonMessageLoader: function ( url ) {
|
||||
var messageStore = this;
|
||||
|
||||
return $.ajax( {
|
||||
url: url,
|
||||
dataType: 'json',
|
||||
//async: false
|
||||
// This is unfortunate.
|
||||
} ).fail( function ( jqxhr, settings, exception ) {
|
||||
messageStore.log( 'Error in loading messages from ' + url + ' Exception: ' + exception );
|
||||
} );
|
||||
set: function( locale, messages ) {
|
||||
this.messages[locale] = messages;
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -236,15 +98,16 @@
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
get: function ( locale, messageKey ) {
|
||||
// load locale if not loaded
|
||||
if ( !this.messages[locale] ) {
|
||||
this.loadFromQueue( locale );
|
||||
}
|
||||
|
||||
return this.messages[locale] && this.messages[locale][messageKey];
|
||||
}
|
||||
};
|
||||
|
||||
function jsonMessageLoader( url ) {
|
||||
return $.getJSON( url ).fail( function ( jqxhr, settings, exception ) {
|
||||
$.i18n.log( 'Error in loading messages from ' + url + ' Exception: ' + exception );
|
||||
} );
|
||||
}
|
||||
|
||||
$.extend( $.i18n.messageStore, new MessageStore() );
|
||||
|
||||
}( jQuery, window ) );
|
||||
|
||||
Reference in New Issue
Block a user