Translations in an extension
Part 3, chapter 25. Exponential runs in many languages, and an extension's texts should follow the user's language like the kernel's do. This chapter shows how to mark texts, where translations live, and how they are found.
Mark every text
In templates, the i18n operator with a context:
{'Read more'|i18n( 'extension/mysite_hello' )}
{'Hello %name'|i18n( 'extension/mysite_hello',, hash( '%name', $name|wash ) )}The second line, rendered by the template engine with Ada, prints Hello Ada. The empty second argument is the
optional comment, a hint for translators.
In PHP, ezpI18n::tr( $context, $source, $comment = null, $arguments = null ) (kernel/common/ezpi18n.php):
$text = ezpI18n::tr( 'extension/mysite_hello', 'There are %count greetings.', null, array( '%count' => $count ) );| Rule | Why |
|---|---|
One context per extension, or per area of it: extension/mysite_hello, extension/mysite_hello/admin | translators see the texts together |
| Whole sentences with placeholders, never glued pieces | word order differs between languages |
Placeholders start with % | %name, %count |
| American English as the source text | the project's source language; license, color |
| Escape placeholders that come from people | hash( '%name', $name|wash ) |
The files
extension/mysite_hello/translations/
untranslated/translation.ts every source text, no translations (a template for translators)
eng-US/translation.ts English, when it differs from the source
ger-DE/translation.ts
fre-FR/translation.tsA .ts file is Qt Linguist XML. From the expqueue extension's German file:
<TS version="2.0" language="de_DE">
<context>
<name>extension/expqueue</name>
<message>
<source>Job queue</source>
<translation>Auftragswarteschlange</translation>
</message>
</context>
</TS>Register them
extension/mysite_hello/settings/site.ini.append.php:
/* #?ini charset="utf-8"?
[RegionalSettings]
TranslationExtensions[]=mysite_hello
*/ expqueue registers itself exactly this way. Then clear the translation cache:
php bin/php/ezcache.php --clear-id=translationFallbacks
A locale without its own translation can fall back to a related one (Austrian German to German, for example). The
chains are in i18n.ini [TranslationSettings]; the console shows them and how much of the kernel and of each
extension is covered:
./console exp:i18n fallbacks --locale=ger-AT
./console exp:i18n checkSee Translations and languages and Languages.
Producing the files
- By hand: for a small extension, writing the
.tsfiles directly is fine; keepuntranslated/complete. - ezlupdate: the kernel ships the source of
ezlupdate(a Qt based tool that extractsi18ntexts from templates and PHP) insupport/ezlupdate-qt4.5/; itsREADME.txtexplains how to build it. The.tsfiles can then be edited in Qt Linguist or any XML editor.
Check
./console exp:ezchecktranslation ger-DEshows statistics for one translation. Switch the administration to another language (My account, or a siteaccess
with another Locale) and look at your pages: any English text left is a text not marked with i18n, or missing from
the file, or a cache not cleared.
Related pages
- Previous: Ajax with ezjscore server functions
- Next: Persistent objects and your own tables
- By hand: translating an extension
- Translations and languages
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.