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 ) {
67 if ( typeof source === 'string' ) {
68 // This is a URL to the messages file.
69 $.i18n.log( 'Loading messages from: ' + source );
70 return jsonMessageLoader( source )
71 .then( function ( localization ) {
72 return messageStore.load( localization, locale );
77 // source is an key-value pair of messages for given locale
78 messageStore.set( locale, source );
80 return $.Deferred().resolve();
82 // source is a key-value pair of locales and their source
83 for ( key in source ) {
84 if ( Object.prototype.hasOwnProperty.call( source, key ) ) {
86 // No {locale} given, assume data is a group of languages,
87 // call this function again for each language.
88 deferreds.push( messageStore.load( source[ key ], locale ) );
91 return $.when.apply( $, deferreds );
97 * Set messages to the given locale.
98 * If locale exists, add messages to the locale.
100 * @param {string} locale
101 * @param {Object} messages
103 set: function ( locale, messages ) {
104 if ( !this.messages[ locale ] ) {
105 this.messages[ locale ] = messages;
107 this.messages[ locale ] = $.extend( this.messages[ locale ], messages );
113 * @param {string} locale
114 * @param {string} messageKey
117 get: function ( locale, messageKey ) {
118 return this.messages[ locale ] && this.messages[ locale ][ messageKey ];
122 $.extend( $.i18n.messageStore, new MessageStore() );