Engine/Internationalisation
From Elgg Documentation
Contents |
Internationalisation
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.
Echoing text
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)
Adding language files to plugins (and best practices)
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:
- Define a
languagesfolder (eg/mod/yourplugin/languages/) - Call
register_translations('/full/path/to/your/language/directory') - All language files, eg
en.php,nl.php,de.phpetc, will be registered appropriately to their language. Language files are named after their ISO 639-1 short code.
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).
Anatomy of a language file
A language file contains two components:
- An array of the form
$language_array = array('shortcode1' => 'Full text 1', ..., 'shortcodeN' => 'Full text n') - A call to
add_translation('language_short_code', $language_array)
See the system's /languages/en.php for an example.
