Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

String API: Difference between revisions

From MoodleDocs
(Added AMOS and Language links)
(Updated files to include textlib)
Line 32: Line 32:
Note: Help string name should be suffixed with '''_help'''. Look at [[Help strings]] for more details.
Note: Help string name should be suffixed with '''_help'''. Look at [[Help strings]] for more details.
==Files==
==Files==
String functions are defined in lib/moodlelib.php.
String functions are defined in  
# lib/moodlelib.php - locale related functions
# lib/textlib.class.php - general string functions (substr, strlen etc.)
 
==Functions and examples==
==Functions and examples==
There are three main functions, used in moodle for getting/displaying the localised string, based on user preferred language.
There are three main functions, used in moodle for getting/displaying the localised string, based on user preferred language.

Revision as of 03:01, 19 January 2012

Overview

The String API is how you get language text strings to use in the user interface. It handles internationalisation issues, and will use a number of settings and environment variables to present the best text to every user. Moodle has a mechanism that allows a number of places to be searched (in order) to find language strings or help files. This enables language strings to be packaged with plugins and avoids the step of having to copy the language files over to the language directory when a plugin is installed.

Basic concepts

When it is required to lookup a string or point to a help file, two basic items of information are required.

  1. Name of the plugin language file in which it can be found.
  2. Name of the string (or the help filename). For example, get_string('editingquiz','quiz_numerical') returns "Editing Quiz" in the current language.

Adding language file to plugin

Language support for plugin(s) is added by creating a lang subdirectory in the plugin directory. The structure of the lang directory is then the same as the "main" language directory with one exception - the help directory name should not have the type_ part of the module name (this might be a bug really!).

Plugin language file name needs to start with the generic type for the plugin. For example all questiontype plugins must be prefixed qtype_, all authentication plugins auth_. Moodle uses this prefix to identify the language search path.

Example for adding language support file for Drag & Drop optional question type

   Description: Drag & Drop optional question type
   Module type: qtype_
   Module name: qtype_dragdrop
   Language file location (English): contrib/plugins/question/type/dragdrop/lang/en/qtype_dragdrop.php
   Help file directory (English): contrib/plugins/question/type/dragdrop/lang/en/help/dragdrop/

Note:

  • The help file directory is called dragdrop in the plugin. Compare this with the multichoice question type in core where the help folder is qtype_multichoice.
  • Module names cannot have numbers in them (only A-Z, a-z and underscore). This might be an issue to consider if you are moving from an existing architecture.

Adding name of string in language file

Name of the string is added to a pre-defined array $string. If you want to a string "Editing Quiz" with string name "editingquiz" in Drag & Drop plugin then add the following in qtype_dragdrop.php language file.

<?php
//string name added for 'Editing Quiz'
$string['editingquiz'] = 'Editing Quiz';
//Help string for 'Editing Quiz' (optional)
$string['editingquiz_help'] = 'Help for editing quiz';
?>

Note: Help string name should be suffixed with _help. Look at Help strings for more details.

Files

String functions are defined in

  1. lib/moodlelib.php - locale related functions
  2. lib/textlib.class.php - general string functions (substr, strlen etc.)

Functions and examples

There are three main functions, used in moodle for getting/displaying the localised string, based on user preferred language.

get_string()

Returns a localised string for current user.

Example for displaying string "This is my plug-in" in any language that supports on your site, then you need to use the following identifier in language file (located in appropriate lang directory)

  $string['plugintitle'] = 'This is my plug-in';

If you want to display this string in any supported language, then you would use this function.

  echo get_string('module_pluginlangfilename', 'plugintitle');

If you want to substitute value in the language string then use {$a} for substituting value. $a is an object, string or number that can be used within translation strings. For example, if you want to display answer number in Drag & drop plugin then add

  $string['answerno'] = 'Answer {$a}'; //Substituting string/integer
  $string['answerno'] = 'Answer {$a->name}'; //Substituting object member

in qtype_dragdrop.php (Drag & Drop language file). And call it using

  //Get string by substituting integer. 
  get_string('answerno', 'qtype_dragdrop', $number);
  //Get string by substituting object member integer
  $user->number = 10;
  get_string('answerno', 'qtype_dragdrop', $user);

In Moodle 2.3 there is a new argument to this function $lazyload. Setting $lazyload to true causes get_string to return a lang_string object rather than the string itself.

  $stringobject = get_string('answerno', 'qtype_dragdrop', $number, true);

The fetching of the string is then put off until the string object is first used. The object can be used by calling it's out method or by casting the object to a string, either directly e.g.

  (string)$stringobject 

or indirectly by using the string within another string or echoing it out e.g.

  echo $stringobject;

return "

{$stringobject}

";

Note: using $lazyload and attempting to use the string as an array key will cause a fatal error as objects cannot be used as array keys.

get_strings()

Converts an array of string names to localised strings for a specific plugin. lazy loading is not supported in this function.

  $txt = get_strings(array('enable', 'disable', 'up', 'down', 'none'), 'qtype_dragdrop');
  echo $txt->up;  //Display localised string for up
  echo $txt->down //Display localised string for down

print_string()

Prints out a translated string by using get_string() function

lang_string class

In Moodle 2.3 a special class (lang_string) is used to create an object representation of a string request. In this case string processing doesn't occur until the object is first used. The class was created especially to aid performance in areas where strings were required to be generated but were not necessarily used. As an example the admin navigation tree when generated uses over 1500 strings, of which normally only 1/3 are ever actually printed at any time. The performance advantage is achieved by not actually processing strings that aren't being used, as such reducing the processing required for the page.

lang_string class can be used in two ways

  1. Setting $lazyload (forth argument of the get_string function), to true.
  $string = get_string('yes', 'qtype_dragdrop', null, true);
  1. Direct instantiation
  $string = new lang_string('yes', 'qtype_dragdrop', null, 'en');

FAQ

When should I use a lang_string object?

The lang_string object is designed to be used in any situation where a string may not be needed, but needs to be generated. The admin navigation tree is a good example of where lang_string objects should be used. A more practical example would be any class that requries strings that may not be printed (after all classes get renderer by renderers and who knows what they will do ;))

When should I not use a lang_string object?

Don't use lang_strings when you are going to use a string immediately. There is no need as it will be processed immediately and there will be no advantage, and in fact perhaps a negative hit as a class has to be instantiated for a lang_string object, however get_string won't require that.

Limitation of lang_string

lang_string object cannot be used as an array offset. Doing so will result in PHP throwing an error. (You can use it as an object property!)

See also