]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - resources/lib/jquery.i18n/src/jquery.i18n.messagestore.js
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / resources / lib / jquery.i18n / src / jquery.i18n.messagestore.js
1 /**
2  * jQuery Internationalization library - Message Store
3  *
4  * Copyright (C) 2012 Santhosh Thottingal
5  *
6  * jquery.i18n is dual licensed GPLv2 or later and MIT. You don't have to do anything special to
7  * choose one license or the other and you don't have to notify anyone which license you are using.
8  * You are free to use UniversalLanguageSelector in commercial projects as long as the copyright
9  * header is left intact. See files GPL-LICENSE and MIT-LICENSE for details.
10  *
11  * @licence GNU General Public Licence 2.0 or later
12  * @licence MIT License
13  */
14
15 ( function ( $, window, undefined ) {
16         'use strict';
17
18         var MessageStore = function () {
19                 this.messages = {};
20                 this.sources = {};
21         };
22
23         /**
24          * See https://github.com/wikimedia/jquery.i18n/wiki/Specification#wiki-Message_File_Loading
25          */
26         MessageStore.prototype = {
27
28                 /**
29                  * General message loading API This can take a URL string for
30                  * the json formatted messages.
31                  * <code>load('path/to/all_localizations.json');</code>
32                  *
33                  * This can also load a localization file for a locale <code>
34                  * load( 'path/to/de-messages.json', 'de' );
35                  * </code>
36                  * A data object containing message key- message translation mappings
37                  * can also be passed Eg:
38                  * <code>
39                  * load( { 'hello' : 'Hello' }, optionalLocale );
40                  * </code> If the data argument is
41                  * null/undefined/false,
42                  * all cached messages for the i18n instance will get reset.
43                  *
44                  * @param {String|Object} source
45                  * @param {String} locale Language tag
46                  * @return {jQuery.Promise}
47                  */
48                 load: function ( source, locale ) {
49                         var key = null,
50                                 deferred = null,
51                                 deferreds = [],
52                                 messageStore = this;
53
54                         if ( typeof source === 'string' ) {
55                                 // This is a URL to the messages file.
56                                 $.i18n.log( 'Loading messages from: ' + source );
57                                 deferred = jsonMessageLoader( source )
58                                         .done( function ( localization ) {
59                                                 messageStore.set( locale, localization );
60                                         } );
61
62                                 return deferred.promise();
63                         }
64
65                         if ( locale ) {
66                                 // source is an key-value pair of messages for given locale
67                                 messageStore.set( locale, source );
68
69                                 return $.Deferred().resolve();
70                         } else {
71                                 // source is a key-value pair of locales and their source
72                                 for ( key in source ) {
73                                         if ( Object.prototype.hasOwnProperty.call( source, key ) ) {
74                                                 locale = key;
75                                                 // No {locale} given, assume data is a group of languages,
76                                                 // call this function again for each language.
77                                                 deferreds.push( messageStore.load( source[key], locale ) );
78                                         }
79                                 }
80                                 return $.when.apply( $, deferreds );
81                         }
82
83                 },
84
85                 /**
86                  * Set messages to the given locale.
87                  * If locale exists, add messages to the locale.
88                  * @param locale
89                  * @param messages
90                  */
91                 set: function ( locale, messages ) {
92                         if ( !this.messages[locale] ) {
93                                 this.messages[locale] = messages;
94                         } else {
95                                 this.messages[locale] = $.extend( this.messages[locale], messages );
96                         }
97                 },
98
99                 /**
100                  *
101                  * @param locale
102                  * @param messageKey
103                  * @returns {Boolean}
104                  */
105                 get: function ( locale, messageKey ) {
106                         return this.messages[locale] && this.messages[locale][messageKey];
107                 }
108         };
109
110         function jsonMessageLoader( url ) {
111                 var deferred = $.Deferred();
112
113                 $.getJSON( url )
114                         .done( deferred.resolve )
115                         .fail( function ( jqxhr, settings, exception ) {
116                                 $.i18n.log( 'Error in loading messages from ' + url + ' Exception: ' + exception );
117                                 // Ignore 404 exception, because we are handling fallabacks explicitly
118                                 deferred.resolve();
119                         } );
120
121                 return deferred.promise();
122         }
123
124         $.extend( $.i18n.messageStore, new MessageStore() );
125 }( jQuery, window ) );