Contents |
Elgg 1.0+ departs from previous versions in that it uses a custom text array rather than gettext. This improves system performance and reliability of the translation system. The default language is "en", for English, but other languages can get added easily. The /languages directory will contain several files, with a {language-code}.php file for each language.
When a piece of functionality wants to output text, it uses the elgg_echo() function, using the short codes listed here. For example, to output the text "Description" use echo elgg_echo('description') which will output "Description" in the correct language throughout the system.
If you need to incorporate variables in the text, you can combine this with a call to `sprintf()`. For example, your string might be defined as:
'welcome' => “Welcome to %s, %s!”,
Subsequently you can call this as sprintf(elgg_echo('welcome'),$CONFIG->sitename, $_SESSION['user']->name)
It's more than likely that any plugin you write will need to add extra text definitions (or redefine some existing ones). This is easy:
languages folder (eg /mod/yourplugin/languages/)
en.php, nl.php, de.php etc, will be registered appropriately to their language. Language files are named after their ISO 639-1 short code and will be loaded automatically by the framework.
It's good practice for the short code for your language strings to start with your plugin name, for example yourplugin:success, yourplugin:title etc. This is because all translations have global scope (so that you can redefine system strings if you need to).
A language file contains two components:
$language_array = array('shortcode1' => 'Full text 1', ..., 'shortcodeN' => 'Full text n')
add_translation('language_short_code', $language_array)
See the system's /languages/en.php for an example.