Retrieving language strings from Moodle
Moodle makes extensive use of language strings to support full multilingual internationalization. In practice, this means that strings which are used within the interface are held in language-specific files. For example, the string "Submit assignment" may be held in the relevant English language file, and this string may be referred to indirectly via a short name key.
This makes it trivial to support additional languages by creating files for those languages. As the code refers to the strings via their short name keys, it is easy to simply switch the set of language files, and the code will pick up the strings in the new preferred language. This happens automatically when a user changes their preferred language settings.
When providing textual feedback to the user from our JavaScript code, we should make use of Moodle's language string system. This ensures our code is inherently multilingual and makes it easy for a non-developer to provide a language translation of our module.
Getting ready
In this example, we will retrieve the built-in Moodle language string course
and show that it is available from our JavaScript code by displaying it with the alert function.
We start once again with a basic Moodle page and associated .js
file:
<?php require_once(dirname(__FILE__) . '/../config.php'); $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM)); $PAGE->set_url('/cook/requirejs_init_lang.php'); $PAGE->requires->js('/cook/requirejs_init_lang.js'); $PAGE->requires->string_for_js('course', 'moodle'); $PAGE->requires->js_init_call('lang'); echo $OUTPUT->header(); echo $OUTPUT->footer(); ?>
As you can see, this code registers a call to the function lang
which has been defined in the associated .js
file:
function lang(Y) { alert(M.str.moodle.course); }
Now when we load the page, we see that our JavaScript alert is executed, displaying the language string value we set up, as seen in the following screenshot:
How to do it...
We have included our .js
with the method now familiar — $PAGE->requires->js.
After this line comes a new feature of the Page Requirements Manager, the string_for_js
function:
$PAGE->requires->string_for_js('course', 'moodle');
Finally, we refer to this language string from our JavaScript code:
alert(M.str.moodle.course);
How it works...
We call the string_for_js
method with two parameters: the name of the string we wish to retrieve and the location of this string. In this example, we are retrieving the language string for course
from the core Moodle language file.
Now this string is made available to us as part of Moodle's global JavaScript namespace (M) in the format, M.str.<module name>.<string name>
. In our example, this is M.str.moodle.course
.
Using this method, the strings we have set up will be available to all subsequent JavaScript code. If we had simply passed this string as a parameter to the JavaScript function, it would only be available inside that function. If we required it to be available within additional functions, we would have to repeat the process, making copies of the string and passing those to the additional functions resulting in unnecessarily inefficient code.