2021-05-01 18:05:45 +02:00
|
|
|
/*!
|
2016-12-04 19:32:23 +01:00
|
|
|
* jQuery Internationalization library - Message Store
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Santhosh Thottingal
|
|
|
|
*
|
|
|
|
* jquery.i18n is dual licensed GPLv2 or later and MIT. You don't have to do anything special to
|
|
|
|
* choose one license or the other and you don't have to notify anyone which license you are using.
|
|
|
|
* You are free to use UniversalLanguageSelector in commercial projects as long as the copyright
|
|
|
|
* header is left intact. See files GPL-LICENSE and MIT-LICENSE for details.
|
|
|
|
*
|
|
|
|
* @licence GNU General Public Licence 2.0 or later
|
|
|
|
* @licence MIT License
|
|
|
|
*/
|
|
|
|
|
2021-05-01 18:05:45 +02:00
|
|
|
( function ( $ ) {
|
2016-12-04 19:32:23 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var MessageStore = function () {
|
|
|
|
this.messages = {};
|
|
|
|
this.sources = {};
|
|
|
|
};
|
|
|
|
|
2021-05-01 18:05:45 +02:00
|
|
|
function jsonMessageLoader( url ) {
|
|
|
|
var deferred = $.Deferred();
|
|
|
|
|
|
|
|
$.getJSON( url )
|
|
|
|
.done( deferred.resolve )
|
|
|
|
.fail( function ( jqxhr, settings, exception ) {
|
|
|
|
$.i18n.log( 'Error in loading messages from ' + url + ' Exception: ' + exception );
|
|
|
|
// Ignore 404 exception, because we are handling fallabacks explicitly
|
|
|
|
deferred.resolve();
|
|
|
|
} );
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
}
|
|
|
|
|
2016-12-04 19:32:23 +01:00
|
|
|
/**
|
|
|
|
* See https://github.com/wikimedia/jquery.i18n/wiki/Specification#wiki-Message_File_Loading
|
|
|
|
*/
|
|
|
|
MessageStore.prototype = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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' );
|
|
|
|
* </code>
|
|
|
|
* A data object containing message key- message translation mappings
|
|
|
|
* can also be passed Eg:
|
|
|
|
* <code>
|
|
|
|
* load( { 'hello' : 'Hello' }, optionalLocale );
|
|
|
|
* </code> If the data argument is
|
|
|
|
* null/undefined/false,
|
|
|
|
* all cached messages for the i18n instance will get reset.
|
|
|
|
*
|
2021-05-01 18:05:45 +02:00
|
|
|
* @param {string|Object} source
|
|
|
|
* @param {string} locale Language tag
|
2016-12-04 19:32:23 +01:00
|
|
|
* @return {jQuery.Promise}
|
|
|
|
*/
|
|
|
|
load: function ( source, locale ) {
|
|
|
|
var key = null,
|
|
|
|
deferreds = [],
|
|
|
|
messageStore = this;
|
|
|
|
|
|
|
|
if ( typeof source === 'string' ) {
|
|
|
|
// This is a URL to the messages file.
|
|
|
|
$.i18n.log( 'Loading messages from: ' + source );
|
2021-05-01 18:05:45 +02:00
|
|
|
return jsonMessageLoader( source )
|
|
|
|
.then( function ( localization ) {
|
|
|
|
return messageStore.load( localization, locale );
|
2016-12-04 19:32:23 +01:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( locale ) {
|
|
|
|
// source is an key-value pair of messages for given locale
|
|
|
|
messageStore.set( locale, source );
|
|
|
|
|
|
|
|
return $.Deferred().resolve();
|
|
|
|
} else {
|
|
|
|
// 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.
|
2021-05-01 18:05:45 +02:00
|
|
|
deferreds.push( messageStore.load( source[ key ], locale ) );
|
2016-12-04 19:32:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $.when.apply( $, deferreds );
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set messages to the given locale.
|
|
|
|
* If locale exists, add messages to the locale.
|
2021-05-01 18:05:45 +02:00
|
|
|
*
|
|
|
|
* @param {string} locale
|
|
|
|
* @param {Object} messages
|
2016-12-04 19:32:23 +01:00
|
|
|
*/
|
|
|
|
set: function ( locale, messages ) {
|
2021-05-01 18:05:45 +02:00
|
|
|
if ( !this.messages[ locale ] ) {
|
|
|
|
this.messages[ locale ] = messages;
|
2016-12-04 19:32:23 +01:00
|
|
|
} else {
|
2021-05-01 18:05:45 +02:00
|
|
|
this.messages[ locale ] = $.extend( this.messages[ locale ], messages );
|
2016-12-04 19:32:23 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2021-05-01 18:05:45 +02:00
|
|
|
* @param {string} locale
|
|
|
|
* @param {string} messageKey
|
|
|
|
* @return {boolean}
|
2016-12-04 19:32:23 +01:00
|
|
|
*/
|
|
|
|
get: function ( locale, messageKey ) {
|
2021-05-01 18:05:45 +02:00
|
|
|
return this.messages[ locale ] && this.messages[ locale ][ messageKey ];
|
2016-12-04 19:32:23 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$.extend( $.i18n.messageStore, new MessageStore() );
|
2021-05-01 18:05:45 +02:00
|
|
|
}( jQuery ) );
|