Outputting strings to end users
We want the user interface to be fully localisable this means we expect to to be possible to translate all strings that will be displayed to a user in a browser.
Strings in PHP code
When on a base ExamSys page the language strings for the page should be automatically available via the $string
array.
If you are in a function or method there are a couple of approaches that can be used to get settings:
require that the calling code passes in the appropriate strings
get a language string using
$string = LangUtils::loadlangfile($file, $string)
.$file
should be the path within the languages directory of the string containing the file.$strings
is an optional array of language strings, if this is not passed you will modify the global strings object.
Parameters in strings
Sometimes we will want to include data in strings for this we use sprintf(), for example if we have the following string defined in a language file:
$string['furtherassistance'] = 'For further assistance contact: <a href="mailto:%s">%s</a>';
then when outputting it we would need to pass two values to it:
$email = 'user@example.com';
$name = 'Joe Bloggs';
$furtherassitance = sprintf($string['furtherassistance'], $email, $name);
Strings in JavaScript
We have a JavaScript module that can be used to localise strings that are going to be output in JavaScript.
To use a string in JavaScript we require that it is loaded into the final page for example to load the strings required by the Hotspot question type we have the following included in one of the Twig templates:
<script>
requirejs(['lang'], function(lang) {
lang.set_strings({
{% for id, string in lang %}
'{{id}}': '{{string|e('js')}}',
{% endfor %}
}, 'html5');
});
</script>
This ensures that all of the Hotspot strings are loaded using html5
as the component.
We do not currently have any mechanism in ExamSys to load strings on demand, via a web service.
After we have loaded the strings to use them we need to include the lang
module in our script. We need to do something like:
define(['lang'], function(Lang) {
let email = 'user@example.com';
let name = 'Joe Bloggs';
alert(Lang.get_string('furtherassistance', 'default', [email, name]));
});
This is assuming that we loaded the furtherassistance
string into the default
component.