| 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 | deferreds = [], |
| 65 | messageStore = this; |
| 66 | |
| 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 ); |
| 73 | } ); |
| 74 | } |
| 75 | |
| 76 | if ( locale ) { |
| 77 | // source is an key-value pair of messages for given locale |
| 78 | messageStore.set( locale, source ); |
| 79 | |
| 80 | return $.Deferred().resolve(); |
| 81 | } else { |
| 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 ) ) { |
| 85 | locale = 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 ) ); |
| 89 | } |
| 90 | } |
| 91 | return $.when.apply( $, deferreds ); |
| 92 | } |
| 93 | |
| 94 | }, |
| 95 | |
| 96 | /** |
| 97 | * Set messages to the given locale. |
| 98 | * If locale exists, add messages to the locale. |
| 99 | * |
| 100 | * @param {string} locale |
| 101 | * @param {Object} messages |
| 102 | */ |
| 103 | set: function ( locale, messages ) { |
| 104 | if ( !this.messages[ locale ] ) { |
| 105 | this.messages[ locale ] = messages; |
| 106 | } else { |
| 107 | this.messages[ locale ] = $.extend( this.messages[ locale ], messages ); |
| 108 | } |
| 109 | }, |
| 110 | |
| 111 | /** |
| 112 | * |
| 113 | * @param {string} locale |
| 114 | * @param {string} messageKey |
| 115 | * @return {boolean} |
| 116 | */ |
| 117 | get: function ( locale, messageKey ) { |
| 118 | return this.messages[ locale ] && this.messages[ locale ][ messageKey ]; |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | $.extend( $.i18n.messageStore, new MessageStore() ); |
| 123 | }( jQuery ) ); |