1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 * Fetch and render language strings.
18 * Hooks into the old M.str global - but can also fetch missing strings on the fly.
23 * @copyright 2015 Damyon Wiese <damyon@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 define(['jquery', 'core/ajax', 'core/localstorage'], function($, ajax, storage) {
30 return /** @alias module:core/str */ {
31 // Public variables and functions.
33 * Return a promise object that will be resolved into a string eventually (maybe immediately).
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.
42 get_string: function(key, component, param, lang) {
43 var deferred = $.Deferred();
45 if (typeof M.str[component] !== "undefined" &&
46 typeof M.str[component][key] !== "undefined") {
47 deferred.resolve(M.util.get_string(key, component, param));
48 return deferred.promise();
51 // Try from local storage. If it's there - put it in M.str and resolve it.
52 var cached = storage.get('core_str/' + key + '/' + component + '/' + lang);
54 if (typeof M.str[component] === "undefined") {
55 M.str[component] = [];
57 M.str[component][key] = cached;
58 deferred.resolve(M.util.get_string(key, component, param));
59 return deferred.promise();
62 var request = this.get_strings([{
69 request.done(function(results) {
70 storage.set('core_str/' + key + '/' + component + '/' + lang, results[0]);
71 deferred.resolve(results[0]);
72 }).fail(function(ex) {
76 return deferred.promise();
80 * Make a batch request to load a set of strings
83 * @param {Object[]} requests Array of { key: key, component: component, param: param, lang: lang };
84 * See get_string for more info on these args.
87 get_strings: function(requests) {
89 var deferred = $.Deferred();
94 // Try from local storage. If it's there - put it in M.str and resolve it.
96 for (i = 0; i < requests.length; i++) {
97 request = requests[i];
98 if (typeof request.lang === "undefined") {
99 request.lang = $('html').attr('lang');
101 if (typeof M.str[request.component] === "undefined" ||
102 typeof M.str[request.component][request.key] === "undefined") {
103 // Try and revive it from local storage.
104 var cached = storage.get('core_str/' + request.key + '/' + request.component + '/' + request.lang);
106 if (typeof M.str[request.component] === "undefined") {
107 M.str[request.component] = [];
109 M.str[request.component][request.key] = cached;
111 // It's really not here.
118 // We have all the strings already.
119 for (i = 0; i < requests.length; i++) {
120 request = requests[i];
122 results[i] = M.util.get_string(request.key, request.component, request.param);
124 deferred.resolve(results);
126 // Something is missing, we might as well load them all.
127 var ajaxrequests = [];
129 for (i = 0; i < requests.length; i++) {
130 request = requests[i];
133 methodname: 'core_get_string',
135 stringid: request.key,
136 component: request.component,
143 var deferreds = ajax.call(ajaxrequests, true, false);
144 $.when.apply(null, deferreds).done(
146 // Turn the list of arguments (unknown length) into a real array.
148 for (i = 0; i < arguments.length; i++) {
149 request = requests[i];
150 // Cache all the string templates.
151 if (typeof M.str[request.component] === "undefined") {
152 M.str[request.component] = [];
154 M.str[request.component][request.key] = arguments[i];
155 storage.set('core_str/' + request.key + '/' + request.component + '/' + request.lang, arguments[i]);
156 // And set the results.
157 results[i] = M.util.get_string(request.key, request.component, request.param).trim();
159 deferred.resolve(results);
168 return deferred.promise();