[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/amd/src/ -> str.js (source)

   1  // This file is part of Moodle - http://moodle.org/
   2  //
   3  // Moodle is free software: you can redistribute it and/or modify
   4  // it under the terms of the GNU General Public License as published by
   5  // the Free Software Foundation, either version 3 of the License, or
   6  // (at your option) any later version.
   7  //
   8  // Moodle is distributed in the hope that it will be useful,
   9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11  // GNU General Public License for more details.
  12  //
  13  // You should have received a copy of the GNU General Public License
  14  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  15  
  16  /**
  17   * Fetch and render language strings.
  18   * Hooks into the old M.str global - but can also fetch missing strings on the fly.
  19   *
  20   * @module     core/str
  21   * @class      str
  22   * @package    core
  23   * @copyright  2015 Damyon Wiese <damyon@moodle.com>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   * @since      2.9
  26   */
  27  define(['jquery', 'core/ajax', 'core/localstorage'], function($, ajax, storage) {
  28  
  29  
  30      return /** @alias module:core/str */ {
  31          // Public variables and functions.
  32          /**
  33           * Return a promise object that will be resolved into a string eventually (maybe immediately).
  34           *
  35           * @method get_string
  36           * @param {string} key The language string key
  37           * @param {string} component The language string component
  38           * @param {string} param The param for variable expansion in the string.
  39           * @param {string} lang The users language - if not passed it is deduced.
  40           * @return {Promise}
  41           */
  42           // eslint-disable-next-line camelcase
  43          get_string: function(key, component, param, lang) {
  44              var request = this.get_strings([{
  45                  key: key,
  46                  component: component,
  47                  param: param,
  48                  lang: lang
  49              }]);
  50  
  51              return request.then(function(results) {
  52                  return results[0];
  53              });
  54          },
  55  
  56          /**
  57           * Make a batch request to load a set of strings
  58           *
  59           * @method get_strings
  60           * @param {Object[]} requests Array of { key: key, component: component, param: param, lang: lang };
  61           *                                      See get_string for more info on these args.
  62           * @return {Promise}
  63           */
  64           // eslint-disable-next-line camelcase
  65          get_strings: function(requests) {
  66  
  67              var deferred = $.Deferred();
  68              var results = [];
  69              var i = 0;
  70              var missing = false;
  71              var request;
  72              // Try from local storage. If it's there - put it in M.str and resolve it.
  73  
  74              for (i = 0; i < requests.length; i++) {
  75                  request = requests[i];
  76                  if (typeof request.lang === "undefined") {
  77                      request.lang = $('html').attr('lang').replace('-', '_');
  78                  }
  79                  if (typeof M.str[request.component] === "undefined" ||
  80                          typeof M.str[request.component][request.key] === "undefined") {
  81                      // Try and revive it from local storage.
  82                      var cached = storage.get('core_str/' + request.key + '/' + request.component + '/' + request.lang);
  83                      if (cached) {
  84                          if (typeof M.str[request.component] === "undefined") {
  85                              M.str[request.component] = [];
  86                          }
  87                          M.str[request.component][request.key] = cached;
  88                      } else {
  89                          // It's really not here.
  90                          missing = true;
  91                      }
  92                  }
  93              }
  94  
  95              if (!missing) {
  96                  // We have all the strings already.
  97                  for (i = 0; i < requests.length; i++) {
  98                      request = requests[i];
  99  
 100                      results[i] = M.util.get_string(request.key, request.component, request.param);
 101                  }
 102                  deferred.resolve(results);
 103              } else {
 104                  // Something is missing, we might as well load them all.
 105                  var ajaxrequests = [];
 106  
 107                  for (i = 0; i < requests.length; i++) {
 108                      request = requests[i];
 109  
 110                      ajaxrequests.push({
 111                          methodname: 'core_get_string',
 112                          args: {
 113                              stringid: request.key,
 114                              component: request.component,
 115                              lang: request.lang,
 116                              stringparams: []
 117                          }
 118                      });
 119                  }
 120  
 121                  var deferreds = ajax.call(ajaxrequests, true, false);
 122                  $.when.apply(null, deferreds).done(
 123                      function() {
 124                          // Turn the list of arguments (unknown length) into a real array.
 125                          var i = 0;
 126                          for (i = 0; i < arguments.length; i++) {
 127                              request = requests[i];
 128                              // Cache all the string templates.
 129                              if (typeof M.str[request.component] === "undefined") {
 130                                  M.str[request.component] = [];
 131                              }
 132                              M.str[request.component][request.key] = arguments[i];
 133                              storage.set('core_str/' + request.key + '/' + request.component + '/' + request.lang, arguments[i]);
 134                              // And set the results.
 135                              results[i] = M.util.get_string(request.key, request.component, request.param).trim();
 136                          }
 137                          deferred.resolve(results);
 138                      }
 139                  ).fail(
 140                      function(ex) {
 141                          deferred.reject(ex);
 142                      }
 143                  );
 144              }
 145  
 146              return deferred.promise();
 147          }
 148      };
 149  });


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1