Moodle supports localization out of the box. Your own plugins should provide at least an English language file, for example /mod/yourmod/lang/en/yourmod.php.

WARNING: After adding or changing language strings, don't forget to:

  • Purge the cache
  • Restart Apache (if you're using php caching)

Strings And Translations

Output String in Arbitrary Language

To output a translation string in an arbitrary language (instead of the current default language), you can use the string_manager class' get_string function. This is not a static function, however, so you need an instance first. Here's a oneliner to call the function properly:

echo get_string_manager()->get_string('invitation', 'yourmod', $data_object, $arbitrary_language_code);

If you don't have any data to replace variables like {$a→firstname}, simply call the same function with NULL as the 3rd argument.

The language string in $arbitrary_language_code should consist of a two character code (e.g. nl or en). Moodle uses these codes in mdl_course.lang and mdl_user.lang.

Output Formatted Date in Current Locale

echo userdate($currentcourse->startdate, get_string('strftimedate', 'langconfig'));

Output Dates in Arbitrary Locale

In addition to selecting a specific language, sometimes you also need to switch to the 'locale' of the user. For instance, you may want to output the date in the proper format, with the correct strings for the names of the months and days of the week.

$old_locale = setlocale(LC_ALL, 0);
setlocale(LC_ALL,  get_string_manager()->get_string("locale", "langconfig", NULL, $arbitrary_language_code) );
echo userdate($start_date, get_string_manager()->get_string("strftimedaydate", "langconfig", NULL, $arbitrary_language_code));
setlocale(LC_ALL, $old_locale);

This code will switch to the locale of the string retrieved from the moodledata/lang/{$arbitrary_language_code}/langconfig.php file (for the key locale). Then, a date is properly formatted and printed. And finally, we switch back to the original locale setting.

Please note that this will only work if your server actually supports the locale you're switching to (but that is a Moodle requirement anyway). To install a new locale under Ubuntu:

    sudo locale-gen nl_NL
    sudo locale-gen nl_NL.UTF-8
    sudo update-locale

Correct Database Settings for UTF-8

Moodle uses UTF-8 character encoding. Make sure that your database is configured to use UTF-8 everywhere.

mysql> show variables like '%character%';
| Variable_name            | Value                      |
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
8 rows in set (0.00 sec)

mysql> show variables like '%collat%';
| Variable_name        | Value           |
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
3 rows in set (0.01 sec)

If you're not seeing utf8, edit your /etc/mysql/my.cnf file.

## This only works in the [client] section:

character-set-server = utf8

Save Language Files in UTF-8 Encoding

If you've got language files (e.g. mod/rainmaker/lang/fr/rainmaker.php), make sure they're in UTF-8 character encoding. In vim, use :bomb and :set fileencoding=utf-8 (and then save the file).

Personal Tools