FauxRequest: don’t override getValues()
[lhc/web/wiklou.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 ( $ ) {
16 'use strict';
17
18 var MessageStore = function () {
19 this.messages = {};
20 this.sources = {};
21 };
22
23 function jsonMessageLoader( url ) {
24 var deferred = $.Deferred();
25
26 $.getJSON( url )
27 .done( deferred.resolve )
28 .fail( function ( jqxhr, settings, exception ) {
29 $.i18n.log( 'Error in loading messages from ' + url + ' Exception: ' + exception );
30 // Ignore 404 exception, because we are handling fallabacks explicitly
31 deferred.resolve();
32 } );
33
34 return deferred.promise();
35 }
36
37 /**
38 * See https://github.com/wikimedia/jquery.i18n/wiki/Specification#wiki-Message_File_Loading
39 */
40 MessageStore.prototype = {
41
42 /**
43 * General message loading API This can take a URL string for
44 * the json formatted messages.
45 * <code>load('path/to/all_localizations.json');</code>
46 *
47 * This can also load a localization file for a locale <code>
48 * load( 'path/to/de-messages.json', 'de' );
49 * </code>
50 * A data object containing message key- message translation mappings
51 * can also be passed Eg:
52 * <code>
53 * load( { 'hello' : 'Hello' }, optionalLocale );
54 * </code> If the data argument is
55 * null/undefined/false,
56 * all cached messages for the i18n instance will get reset.
57 *
58 * @param {string|Object} source
59 * @param {string} locale Language tag
60 * @return {jQuery.Promise}
61 */
62 load: function ( source, locale ) {
63 var key = null,
64 deferred = null,
65 deferreds = [],
66 messageStore = this;
67
68 if ( typeof source === 'string' ) {
69 // This is a URL to the messages file.
70 $.i18n.log( 'Loading messages from: ' + source );
71 deferred = jsonMessageLoader( source )
72 .done( function ( localization ) {
73 messageStore.set( locale, localization );
74 } );
75
76 return deferred.promise();
77 }
78
79 if ( locale ) {
80 // source is an key-value pair of messages for given locale
81 messageStore.set( locale, source );
82
83 return $.Deferred().resolve();
84 } else {
85 // source is a key-value pair of locales and their source
86 for ( key in source ) {
87 if ( Object.prototype.hasOwnProperty.call( source, key ) ) {
88 locale = key;
89 // No {locale} given, assume data is a group of languages,
90 // call this function again for each language.
91 deferreds.push( messageStore.load( source[ key ], locale ) );
92 }
93 }
94 return $.when.apply( $, deferreds );
95 }
96
97 },
98
99 /**
100 * Set messages to the given locale.
101 * If locale exists, add messages to the locale.
102 *
103 * @param {string} locale
104 * @param {Object} messages
105 */
106 set: function ( locale, messages ) {
107 if ( !this.messages[ locale ] ) {
108 this.messages[ locale ] = messages;
109 } else {
110 this.messages[ locale ] = $.extend( this.messages[ locale ], messages );
111 }
112 },
113
114 /**
115 *
116 * @param {string} locale
117 * @param {string} messageKey
118 * @return {boolean}
119 */
120 get: function ( locale, messageKey ) {
121 return this.messages[ locale ] && this.messages[ locale ][ messageKey ];
122 }
123 };
124
125 $.extend( $.i18n.messageStore, new MessageStore() );
126 }( jQuery ) );