Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This will introduce developers how to do string localisation in ExamSys, for now it is a place holder page.

Strings in PHP code

This section will describe how to do localisation in PHP code

Strings in JavaScript

This section will describe how to use localised strings in our JavaScriptWe 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:

Code Block
languagephp
$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:

Code Block
languagephp
$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 templates:

Code Block
languagenone
<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.

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:

Code Block
languagejs
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