<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.moodle.org/dev/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Davidknu</id>
	<title>MoodleDocs - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.moodle.org/dev/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Davidknu"/>
	<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/Special:Contributions/Davidknu"/>
	<updated>2026-06-13T04:03:24Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=String_API&amp;diff=52195</id>
		<title>String API</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=String_API&amp;diff=52195"/>
		<updated>2017-04-26T10:19:49Z</updated>

		<summary type="html">&lt;p&gt;Davidknu: /* get_string() */ using different identifiers for the two examples to help users to distinguish between them&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
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. 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.&lt;br /&gt;
&lt;br /&gt;
Moodle also provide general string functions like substr, strlen etc. for multibyte safe, string operations. It uses mbstring or iconv for UTF-8 strings and falls back to typo3.&lt;br /&gt;
&lt;br /&gt;
==Basic concepts==&lt;br /&gt;
When it is required to lookup a string, two basic items of information are required. &lt;br /&gt;
# Name of the plugin language file in which it can be found. &lt;br /&gt;
# Name of the string. For example, &amp;lt;tt&amp;gt;get_string(&#039;editingquiz&#039;, &#039;quiz&#039;)&amp;lt;/tt&amp;gt; returns &amp;quot;Editing quiz&amp;quot; in the current language.&lt;br /&gt;
===Adding language file to plugin===&lt;br /&gt;
Language support for plugin(s) is added by creating a &#039;&#039;&#039;lang&#039;&#039;&#039; subdirectory in the plugin directory. The structure of the &#039;&#039;&#039;lang&#039;&#039;&#039; directory is then the same as the &amp;quot;main&amp;quot; language directory.&lt;br /&gt;
&lt;br /&gt;
Plugin language file name needs to start with the generic type for the plugin. For example all questiontype plugins &#039;&#039;&#039;must&#039;&#039;&#039; be prefixed &#039;&#039;&#039;qtype_&#039;&#039;&#039;, all authentication plugins &#039;&#039;&#039;auth_&#039;&#039;&#039;. Moodle uses this prefix to identify the [https://docs.moodle.org/dev/Places_to_search_for_lang_strings#Defining_the_search_path.28s.29 language search path.]&lt;br /&gt;
&lt;br /&gt;
Example for adding language support file for Drag &amp;amp; Drop optional question type&lt;br /&gt;
    Description: Drag &amp;amp; Drop optional question type&lt;br /&gt;
    Module type: qtype_&lt;br /&gt;
    Module name: qtype_dragdrop&lt;br /&gt;
    Language file location (English): contrib/plugins/question/type/dragdrop/lang/en/qtype_dragdrop.php&lt;br /&gt;
&lt;br /&gt;
Note: &lt;br /&gt;
* 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. &lt;br /&gt;
===Adding name of string in language file===&lt;br /&gt;
Name of the string is added to a pre-defined array $string. If you want to a string &amp;quot;Editing Quiz&amp;quot; with string name &amp;quot;editingquiz&amp;quot; in Drag &amp;amp; Drop plugin then add the following in qtype_dragdrop.php language file.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
//string name added for &#039;Editing Quiz&#039;&lt;br /&gt;
$string[&#039;editingquiz&#039;] = &#039;Editing Quiz&#039;;&lt;br /&gt;
//Help string for &#039;Editing Quiz&#039; (optional)&lt;br /&gt;
$string[&#039;editingquiz_help&#039;] = &#039;Help for editing quiz&#039;;&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: &lt;br /&gt;
* Help string name should be suffixed with &#039;&#039;&#039;_help&#039;&#039;&#039;. Look at [[Help strings]] for more details.&lt;br /&gt;
&lt;br /&gt;
==Files==&lt;br /&gt;
String functions are defined in &lt;br /&gt;
# lib/moodlelib.php - locale related functions&lt;br /&gt;
# lib/textlib.class.php - general string functions (substr, strlen etc.)&lt;br /&gt;
&lt;br /&gt;
==Functions and examples==&lt;br /&gt;
There are three main functions, used in moodle for getting/displaying the localised string, based on user preferred language.&lt;br /&gt;
===get_string()===&lt;br /&gt;
Returns a localised string for current user.&lt;br /&gt;
&lt;br /&gt;
Example for displaying string &amp;quot;This is my plug-in&amp;quot; in any language that supports on your site, then you need to use the following identifier in language file (located in appropriate lang directory)&lt;br /&gt;
   $string[&#039;plugintitle&#039;] = &#039;This is my plug-in&#039;;&lt;br /&gt;
If you want to display this string in any supported language, then you would use this function.&lt;br /&gt;
   echo get_string(&#039;plugintitle&#039;, &#039;module_pluginlangfilename&#039;);&lt;br /&gt;
&lt;br /&gt;
If you want to substitute value in the language string then use &#039;&#039;&#039;{$a}&#039;&#039;&#039; for substituting value. $a is an object, string or number that can be used within translation strings. The variable has to be $a, and it has to be in single quotes. For example, if you want to display answer number in Drag &amp;amp; drop plugin then add&lt;br /&gt;
   $string[&#039;answerno1&#039;] = &#039;Answer {$a}&#039;; //Substituting string/integer&lt;br /&gt;
   $string[&#039;answerno2&#039;] = &#039;Answer {$a-&amp;gt;name}&#039;; //Substituting object member&lt;br /&gt;
in qtype_dragdrop.php (Drag &amp;amp; Drop language file). And call it using&lt;br /&gt;
   //Get string by substituting integer. &lt;br /&gt;
   get_string(&#039;answerno1&#039;, &#039;qtype_dragdrop&#039;, $number);&lt;br /&gt;
   //Get string by substituting object member integer&lt;br /&gt;
   $user-&amp;gt;number = 10;&lt;br /&gt;
   get_string(&#039;answerno2&#039;, &#039;qtype_dragdrop&#039;, $user);&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
   $stringobject = get_string(&#039;answerno&#039;, &#039;qtype_dragdrop&#039;, $number, true);&lt;br /&gt;
&lt;br /&gt;
The fetching of the string is then put off until the string object is first used. The object can be used by calling it&#039;s out method or by casting the object to a string, either directly e.g. &lt;br /&gt;
   (string)$stringobject &lt;br /&gt;
or indirectly by using the string within another string or echoing it out e.g.&lt;br /&gt;
   echo $stringobject;&lt;br /&gt;
   return &amp;quot;&amp;lt;p&amp;gt;{$stringobject}&amp;lt;/p&amp;gt;&amp;quot;;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
===get_strings()===&lt;br /&gt;
Converts an array of string names to localised strings for a specific plugin. lazy loading is not supported in this function.&lt;br /&gt;
   $txt = get_strings(array(&#039;enable&#039;, &#039;disable&#039;, &#039;up&#039;, &#039;down&#039;, &#039;none&#039;), &#039;qtype_dragdrop&#039;);&lt;br /&gt;
   echo $txt-&amp;gt;up;  //Display localised string for up&lt;br /&gt;
   echo $txt-&amp;gt;down //Display localised string for down&lt;br /&gt;
===print_string()===&lt;br /&gt;
Prints out a translated string by using &#039;&#039;&#039;get_string()&#039;&#039;&#039; function&lt;br /&gt;
&lt;br /&gt;
===lang_string class===&lt;br /&gt;
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&#039;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&#039;t being used, as such reducing the processing required for the page.&lt;br /&gt;
&lt;br /&gt;
lang_string class can be used in two ways&lt;br /&gt;
# Setting $lazyload (forth argument of the get_string function), to true.&lt;br /&gt;
   $string = get_string(&#039;yes&#039;, &#039;qtype_dragdrop&#039;, null, true);&lt;br /&gt;
# Direct instantiation&lt;br /&gt;
   $string = new lang_string(&#039;yes&#039;, &#039;qtype_dragdrop&#039;, null, &#039;en&#039;);&lt;br /&gt;
&lt;br /&gt;
== String manager ==&lt;br /&gt;
&lt;br /&gt;
Most of function listed above are just handful wrappers for the methods provided by the string manager class. Some strings related functionality is available only via directly calling the manager&#039;s methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$stringman = get_string_manager();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The factory function get_string_manager() returns singleton instance of &amp;lt;tt&amp;gt;core_string_manager_install&amp;lt;/tt&amp;gt; in early stages of the site installation, or instance of &amp;lt;tt&amp;gt;core_string_manager_standard&amp;lt;/tt&amp;gt; in all normal situations. These managers implement interface &amp;lt;tt&amp;gt;core_string_manager&amp;lt;/tt&amp;gt;. Methods provided by the interface are&lt;br /&gt;
&lt;br /&gt;
;get_string():Returns the given component string localised in the given language. Can be used to obtain the translation of the string in the other language than the current user&#039;s language.&lt;br /&gt;
;string_exists():Does the given string actually exist? This is typically checked by a code like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
if (get_string_manager()-&amp;gt;string_exists(&#039;stringidentifier&#039;, &#039;component_name&#039;)) {&lt;br /&gt;
    // Do something.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
;string_deprecated():Has the string been deprecated? See [[String deprecation]] for details.&lt;br /&gt;
;get_list_of_countries():Returns a localised list of all country names, sorted by country keys.&lt;br /&gt;
;get_list_of_languages():Returns a localised list of languages, sorted by code keys.&lt;br /&gt;
;translation_exists():Checks if the translation exists for the given language.&lt;br /&gt;
;get_list_of_translations():Returns localised list of installed language packs.&lt;br /&gt;
;get_list_of_currencies():Returns localised list of known currencies.&lt;br /&gt;
;load_component_strings():Loads all strings for one component.&lt;br /&gt;
;reset_caches():Invalidates all caches, should the manager use some.&lt;br /&gt;
;get_revision():Returns string revision counter.&lt;br /&gt;
&lt;br /&gt;
=== Custom string managers ===&lt;br /&gt;
&lt;br /&gt;
{{Moodle 2.9}}Plugins can provide custom implementation of the string manager. This is supposed to be used in experimental and/or development scenarios only, not in typical production environment. See MDL-49361 for use cases and implementation details. An example of such an implementation can be found at [https://github.com/mudrd8mz/moodle-local_stringman moodle-local_stringman.git] repository.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
// Custom string manager class can be defined in the main config.php file.&lt;br /&gt;
$CFG-&amp;gt;customstringmanager = &#039;\local_stringman\dummy_string_manager&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==textlib (core_text) class==&lt;br /&gt;
textlib class provide pool of safe functions to operate on UTF-8 text. textlib provide set of static functions to operate on strings and gets included in setup.php&lt;br /&gt;
&lt;br /&gt;
{{Moodle 2.6}}In Moodle 2.6 the textlib class was renamed to &#039;&#039;&#039;core_text&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
===asort()===&lt;br /&gt;
Locale aware sorting, the key associations are kept, values are sorted alphabetically.&lt;br /&gt;
===code2utf8===&lt;br /&gt;
Returns the utf8 string corresponding to the unicode value&lt;br /&gt;
===convert===&lt;br /&gt;
Converts the text between different encodings. It uses iconv extension with //TRANSLIT parameter, fall back to typo3&lt;br /&gt;
===encode_mimeheader===&lt;br /&gt;
Generate a correct base64 encoded header to be used in MIME mail messages.&lt;br /&gt;
===entities_to_utf8===&lt;br /&gt;
Converts all the numeric entities &amp;amp;#nnnn; or &amp;amp;#xnnn; to UTF-8&lt;br /&gt;
===specialtoascii===&lt;br /&gt;
Converts upper unicode characters to plain ascii, the returned string may contain unconverted unicode characters.&lt;br /&gt;
===strlen===&lt;br /&gt;
Multibyte safe strlen() function, uses iconv for utf-8, falls back to typo3.&lt;br /&gt;
===strpos===&lt;br /&gt;
Find the position of the first occurrence of a substring in a string. UTF-8 ONLY safe strpos(), uses iconv.&lt;br /&gt;
===strrpos===&lt;br /&gt;
Find the position of the last occurrence of a substring in a string. UTF-8 ONLY safe strrpos(), uses iconv.&lt;br /&gt;
===strtolower===&lt;br /&gt;
Multibyte safe strtolower() function, uses mbstring, falls back to typo3.&lt;br /&gt;
===strtotitle===&lt;br /&gt;
Makes first letter of each word capital - words must be separated by spaces.&lt;br /&gt;
===strtoupper===&lt;br /&gt;
Multibyte safe strtoupper() function, uses mbstring, falls back to typo3.&lt;br /&gt;
===substr===&lt;br /&gt;
Multibyte safe substr() function, uses iconv for utf-8, falls back to typo3.&lt;br /&gt;
===trim_utf8_bom===&lt;br /&gt;
Removes the BOM from unicode string. [http://unicode.org/faq/utf_bom.html more info] &lt;br /&gt;
===utf8_to_entities===&lt;br /&gt;
Converts all Unicode chars &amp;gt; 127 to numeric entities &amp;amp;#nnnn; or &amp;amp;#xnnn;&lt;br /&gt;
&lt;br /&gt;
==FAQ==&lt;br /&gt;
===When should I use a lang_string object?===&lt;br /&gt;
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 ;))&lt;br /&gt;
===When should I not use a lang_string object?===&lt;br /&gt;
Don&#039;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&#039;t require that.&lt;br /&gt;
===Limitation of lang_string===&lt;br /&gt;
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!)&lt;br /&gt;
===How to compare strings properties in two object===&lt;br /&gt;
collatorlib_property_comparison class can be used to compare properties of two objects&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[Core APIs]]&lt;br /&gt;
* [[Languages]]&lt;br /&gt;
* [[Languages/AMOS]]&lt;br /&gt;
* [[Places to search for lang strings]]&lt;br /&gt;
* [[Help strings]]&lt;br /&gt;
&lt;br /&gt;
[[Category:API]]&lt;/div&gt;</summary>
		<author><name>Davidknu</name></author>
	</entry>
</feed>