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
18 var MessageStore = function () {
23 function jsonMessageLoader( url
) {
24 var deferred
= $.Deferred();
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
34 return deferred
.promise();
38 * See https://github.com/wikimedia/jquery.i18n/wiki/Specification#wiki-Message_File_Loading
40 MessageStore
.prototype = {
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>
47 * This can also load a localization file for a locale <code>
48 * load( 'path/to/de-messages.json', 'de' );
50 * A data object containing message key- message translation mappings
51 * can also be passed Eg:
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.
58 * @param {string|Object} source
59 * @param {string} locale Language tag
60 * @return {jQuery.Promise}
62 load: function ( source
, locale
) {
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
);
76 return deferred
.promise();
80 // source is an key-value pair of messages for given locale
81 messageStore
.set( locale
, source
);
83 return $.Deferred().resolve();
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
) ) {
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
) );
94 return $.when
.apply( $, deferreds
);
100 * Set messages to the given locale.
101 * If locale exists, add messages to the locale.
103 * @param {string} locale
104 * @param {Object} messages
106 set: function ( locale
, messages
) {
107 if ( !this.messages
[ locale
] ) {
108 this.messages
[ locale
] = messages
;
110 this.messages
[ locale
] = $.extend( this.messages
[ locale
], messages
);
116 * @param {string} locale
117 * @param {string} messageKey
120 get: function ( locale
, messageKey
) {
121 return this.messages
[ locale
] && this.messages
[ locale
][ messageKey
];
125 $.extend( $.i18n
.messageStore
, new MessageStore() );