2 * jQuery Internationalization library - Message Store
4 * Copyright (C) 2012 Santhosh Thottingal
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.
11 * @licence GNU General Public Licence 2.0 or later
12 * @licence MIT License
15 ( function ( $, window
, undefined ) {
18 var MessageStore = function () {
24 * See https://github.com/wikimedia/jquery.i18n/wiki/Specification#wiki-Message_File_Loading
26 MessageStore
.prototype = {
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>
33 * This can also load a localization file for a locale <code>
34 * load( 'path/to/de-messages.json', 'de' );
36 * A data object containing message key- message translation mappings
37 * can also be passed Eg:
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.
44 * @param {String|Object} source
45 * @param {String} locale Language tag
46 * @return {jQuery.Promise}
48 load: function ( source
, locale
) {
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
);
62 return deferred
.promise();
66 // source is an key-value pair of messages for given locale
67 messageStore
.set( locale
, source
);
69 return $.Deferred().resolve();
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
) ) {
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
) );
80 return $.when
.apply( $, deferreds
);
86 * Set messages to the given locale.
87 * If locale exists, add messages to the locale.
91 set: function ( locale
, messages
) {
92 if ( !this.messages
[locale
] ) {
93 this.messages
[locale
] = messages
;
95 this.messages
[locale
] = $.extend( this.messages
[locale
], messages
);
105 get: function ( locale
, messageKey
) {
106 return this.messages
[locale
] && this.messages
[locale
][messageKey
];
110 function jsonMessageLoader( url
) {
111 var deferred
= $.Deferred();
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
121 return deferred
.promise();
124 $.extend( $.i18n
.messageStore
, new MessageStore() );
125 }( jQuery
, window
) );